Linux How to Find All Files with a Specific Text (String)

To find all the text files with specific text strings on Linux, use grep by running this command in your Terminal:

grep -rnw '/path/to/the/files' -e 'pattern'

Where:

  • -r means recursive search (use capital -R to search the whole tree).
  • -n reveals the line number.
  • -w tells to match with the whole word.
  • -e specifies the string (or pattern) you’re searching.

Note (for beginners): You need to keep the quotation marks!

Example Use

Image credit: undraw.co

If you’re well-versed in Linux and are already familiar with grep, I’m sure the above answer is more than enough for you.

But in case you’re new to Linux or grep and aren’t quite sure what I’m talking about, make sure to check this example out.

In this example, I’m using a setup where I have a folder called “example” on my desktop. In the example folder, there are three text files I want to perform a search for.

The files have a sentence or two for demonstration:

file1.txt, file2.txt, and file3.txt

Now, let’s use grep to find in which file there’s a word “Thanks”.

To do this:

  1. Open up a Terminal window.
  2. Change the directory to the folder on the desktop (for example, cd Desktop/example).
  3. Run grep -rnw ‘.’ -e ‘Thanks’. (‘.’ refers to the path of the folder you are in)

As a result, you can see the file path, row number, and the context where it says the word “Thanks“. In this case, it’s the third file, file3.txt, that has the word “Thanks” in it. If there are no files with the word you’re searching for, the command will run without an output.

Notice that the above example scanned through the entire folder Desktop/example, that is, all the files file1, file2, and file3. This is because you specified the path to be ‘.’ which refers to the entire Desktop/example folder you’re in right now.

To restrict the search to a particular file, you can provide a path to the file. For example, let’s check if the word “Thanks” exists in the file2.txt.

When looking from inside the Desktop/example folder, the path to the file2.txt is just “file2.txt”. (If you were on the Desktop folder, the path would be “example/file2.txt”).

Let’s run grep -rnw ‘file2.txt’ -e ‘Thanks’ in the Terminal to restrict the search to the file2.txt only:

Now the result is blank because there’s no word “Thanks” in the file2.txt.

Are you lost with file paths? Make sure to check this quick guide to understanding file paths and relative paths within computers.

Restricting Searches

In the previous section, you learned how to use grep to find all the files behind a path that matches a piece of text.

More often than not, your folders may contain files with millions of rows of text you want to exclude from the search. These files can be of a specific type or live behind a particular directory.

Being able to exclude specific file types or directories from the search can make your job so much easier.

In the following sections, you learn how to restrict the search to specific file types and how to exclude folders/files from the search.

1. Search Files with Specific Extensions

To limit the search to only files with specific extensions, you can specify the –include flag in the grep call.

For example, let’s only include files with extensions .py or .js:

grep --include=\*.{py,js} -rnw '/path/to/the/files' -e "pattern"

This can be useful if you don’t want to search every single file but rather particular types of files.

2. Exclude Specific Files with Extensions

Similar to how you can narrow down the search to particular file types only, you can choose to exclude some types of files from the search.

To exclude files by file type, you can specify the –exclude flag.

For example, let’s search through all files except for those whose extension is .json:

grep --exclude=\*.json -rnw '/path/to/the/files' -e "pattern"

3. Exclude Specific Directories

Sometimes you might also want to leave out particular directories from the search.

To do this, you set the –exclude-dir flag in the grep call.

For example, let’s exclude directories example1 and example2 from the search:

grep --exclude-dir={example1,example2} -rnw '/path/to/search/' -e "pattern"

This command rigorously inspects all the files and folders behind ‘/path/to/search/’ except for the folders example1 and example2.

But What Is Grep?

In the above examples, you learned how to find text in files using Terminal with a grep command.

But if you have no background in Linux and pattern matching, you might wonder what that grep thing even is.

To put it short, grep is a Unix program for pattern matching. You can find grep on Linux as well as on Mac and BSD. To run grep, you need to use the Terminal by running grep followed by the specifications of the pattern you’re trying to match.

Grep is a program that can read any type of text and open any type of file.

For example, you can use grep to find files with particular names, and text content, and more. Grep shows the results in the terminal window after the command.

You can find the full manual pages of grep by running man grep in Terminal.

This shows the manual pages of grep. For instance, you can find a description and some options you can run the program with.

Notice that reading this manual from start to end will most likely make no sense because you’ll probably use the program only once in a while. A more efficient way to use grep is by watching a beginner guide, playing with some examples, and knowing that such a tool exists. When facing a difficult pattern-matching task, you will most likely have to use Google no matter how good a grep user you are.

Summary

Today you learned how to find all the files with a specific text on Linux.

To take home, use the built-in grep program to find files with particular strings.

Thanks for reading. I hope you found it useful!