How can one find a specific string from different files, find a string from different types of files in a directory, or exclude certain directories? These are common challenges one might encounter while working on Linux.

To assist Linux beginners, I'll show you how you can quickly perform this task in Linux without requiring any additional tools, just by using the built-in command.

Finding All Files Containing a Specific String on Linux

There are multiple ways to look for a file containing a specific string on Linux. Today in this article, we will focus on the grep command, which easily helps to locate different types of files and perform simple pattern matching to look for our desired text (or string).

For the demonstration, I have already created two text files and will be searching for the occurrence of the string "problem" in both files using the following command:

$ grep -Rnw file1.txt file2.txt -e 'problem'

Output:

finding common text in two files

You can see we were successfully able to find a matching string in both of these files, highlighted in red.

To understand the option we used in the command, check it out below.

  • -r or -R is used to perform a recursive search.
  • -n is used to indicate the line number of the matching string.
  • -w is used to match whole words instead of substrings.
  • -e is the pattern used during the search.

In addition to this option, you have other options to choose from.

  • -l (lower-case "L") can be used to just give the filename of matching files.
  • --include can be used to only search for specific files.
  • --exclude can be used to ignore certain types of files from searching.
  • --exclude-dir can be used to ignore certain directories when searching for files.

Here are some more different command-line examples that you can see to learn more about various ways to search for a specific string in files.

1. Search for pattern strings in all text files present in the current directory.

$ grep -Rnw *.txt -e "pattern"

2. Search for pattern strings in all text files present in the ~/Documents directory.

$ grep -Rnw ~/Documents/*.txt -e "pattern"

3. Search for pattern strings in all PHP files present in the /var/www/html directory.

$ grep -Rnw /var/www/html/*.php -e "pattern"

4. Search for pattern strings in all PHP and text files present in the /var/www/html directory.

$ grep --include=\*.{php,txt} -rnw '/var/www/html' -e "pattern"

5. Search for pattern strings in all files except the text files present in the /var/www/html directory.

$ grep --exclude=\*.txt -rnw '/var/www/html' -e "pattern"

6. Search for the pattern string in all files present in every directory at /var/www/html, except the style and image directories.

$ grep --exclude-dir={style,image} -rnw '/var/www/html/' -e "pattern"

7. Search for pattern strings from all text files present in the current directory, but instead of printing the paragraph, print the filename that has the specified string.

$ grep -Rnwl *.txt -e "pattern"

That's it.