How to find all files containing specific text (string) on Linux?

To find all files containing the specific text (string) on Linux is quite easy with different command line utilities. This is useful when users are looking and searching for the file by using some specific text strings. 

This post explains different command-line methods to find the files using the specific text on Linux. 

What are the Methods to Find the Files Containing a Specific Text(Strings) on Linux?

The most common methods to find the files by using the specific text on Linux are:

  • Using the grep Command
  • Using the Combination of find and grep Command
  • Using the rg Command
  • Using the ack Command

Before going through these methods, let us create a file with the nano text editor:

$ nano example.txt

Type some information, for instance, we type the following text:

Welcome Note

Welcome to ItsLinuxFoss

This tutorial is about finding the files by using specific strings

Save the changes with the CTRL+S and exit the text editor.

Method 1: Using the grep Command

The grep command utility is the most common to find the files. To do so, use the following options of the grep command:

  • The option “r” will allow users to search recursively from the directories
  • The option “n” displays the line number of the file at which the word is located
  • The option “w” will show the results after matching with the provided word
  • The option “e” will specify the search string or text

Now run the below-mentioned command to find the files having the “Welcome Note” in it:

$ grep -rnw '/home/itslinuxfoss/' -e 'Welcome Note'

The file has been displayed in the output. 

Method 2: Using the Combination of find Command and grep Command

The combination of the “find” and the “grep” command is used to find the files with the provided text or string. The options used in the below-mentioned command are as:

  • The option “-type f” is used to specify search through the files instead of directories
  • The option “-exec” in the command is used to execute the grep command
  • The option “l” is used to display the file names that contain the provided string

Now this combination can be used to find the “Welcome Note” with the following command:

$ find '/home/itslinuxfoss/' -type f -exec grep -l 'Welcome Note' {} \;

Method 3: Using the ripgrep Command

The ripgrep command is also known as the “rg” and can be used to find the strings as shown below:

$ rg "Welcome Note" /home/itslinuxfoss/

Method 4: Using the ack Command

The last command to find the files using the specific string is by using the “ack” command:

$ ack "Welcome Note" /home/itslinuxfoss/

These are the methods by which Linux users can find the files by providing the specific strings. 

Conclusion

To find all the files containing the specific text or string on Linux, use the grep command with its “rnwe” options, and also, the grep command can be used with the combination of the find command. Some other commands that can be used to find the files with the specific strings are “rg” and “ack” commands. All these mentioned methods have been demonstrated with the example in this guide.