Search Inside a File From Terminal

How-To Search Inside a File from the Terminal in Windows

Are you one of those people who doesn’t spend too much time naming files appropriately? Do you also forget which file contained the content that you were looking for? Well, if you are one of those people, we have just the right tool for you – a command-line utility that allows you to search for the files that contain the content you are looking for – or the files that do not contain the content you are looking for, depending on your use case.

For this purpose, we are going to use the find command, which can be used to search for file names, command outputs, as well as the content inside the file. In this guide, we are going to demonstrate exactly how you can find which file (or files) contain a certain piece of string in its content. For the other use of find, like searching inside command-line output, you can refer to this guide.

Using Find to Search for Content inside Files

First, press the Start button and search for CMD to open the Windows Command Prompt. Now, we are going to demonstrate the use of the Find command,

find [switch] "[your-search-text]" [path]

The switch parameter can change the way the command performs. Here are some of the switches you can use,

  • /v – Show the lines that don’t contain the search text you specified
  • /i – Ignores the case of the text when searching for it
  • /c – Show how many lines contain your search text
  • /n – Show the line numbers of the lines that have the search text

The search text is the piece of text you want to search for and the path is the path of the file (or files) where you want to search.

For example, if you want to search for the text “maths” inside a file, here is how you can do it (you can also add the /i switch to ignore case),

find /i "maths" "lectures.txt"

Here are some examples with their outputs,