How To Find All Files Containing Specific Text On Linux From The Command Line

This article explains how to find all files containing specific text on Linux. For this we'll use grep, a standard Unix program.

grep is a command-line utility which prints lines that match a given pattern, and should be installed by default.

Let's start simple. Say you want to search for the word text (case-sensitive!) in all the files in the current directory and its subdirectories. To do this, you need to open the terminal, navigate to the folder where you want to perform the search, and run:

grep -r 'text'

This lists all the files in the current folder and subfolders containing text. This includes strings like texting for example, because it contains our search pattern, text. -r stands for recursive, reading all the files in the directory and its subdirectories. If you require following all symbolic links, use -R instead of -r.

If you only want to list the filenames containing the exact whole word text (as opposed to the default partial word matching), and not things like texting, 123text, and so on, you'd need to append the -w (whole words) command line option, like this:

grep -rw 'text'

If you don't want to search in the current folder, but in a specific folder, you can specify the path in which grep should look into, by adding it at the end of the command, like this:

grep -rw 'text' /path/to/search/into

You might also be interested in: How To Repeat A Command Every X Seconds On Linux

Grep has many options, but below I'll only list a few that you might find especially useful when trying to find all files containing specific text on Linux (besides those already mentioned above):

  • -n shows the line numbers. When a match is found, besides the file path in which it was found, grep will also display the line number on which the pattern was found
  • -i performs the search case-insensitive (it's case-sensitive by default). Depending on the number of files, this can slow down the search, so take this into consideration when using it
  • --include=GLOB / --exclude=GLOB includes or excludes certain files
  • --exclude-dir=GLOB is used to exclude folders from being searched

Let's take a look at an example which combines these command line flags. Let's say you want to find all files containing the case-insensitive (-i) text in the ~/Documents folder and its subfolders except for the Private and Personal subfolders (--exclude-dir), and only search in the files that have the .txt and .js extensions (--include). Also, you want to show the line numbers (-n), search recursively and also follow all symbolic links (-R). In that case, the command you'd need to use would be (this is a single command):

grep -Rni --exclude-dir={Private,Personal} --include={*.txt,*.js} 'text' ~/Documents

Example output:

/home/logix/Documents/test/folder/file1.js:7:text
/home/logix/Documents/test/folder/file2.txt:7:text

Here, 7 is the line number on which the pattern we've searched for (text) was found.

For more information and advanced usage, see the grep man page.

You may also want to check out ripgrep, a line-oriented search tool that recursively searches the current directory for a regex pattern that comes with some extra features, and is very fast.

You  might also like: rga: Search Text In PDF, Ebooks, Office Documents, Archives And More (ripgrep Wrapper) and How To Find Files Modified In The Last N Days Or Minutes Using find