How to Find Files containing a string in Linux?

In this article, we will discuss a way to find files containing a specific string in Linux.

Using find and grep command

Suppose you are using a Command Line Terminal in Linux, and you need to find the files which contains specific text. You can use the find command along with the grep command to search for files containing a text.

Syntex of the command is as follows,

find DIRECTORY -type f -exec grep -l "STRING" {} \;

Here the directory is a path of the folder, in which you need to search for files containing specific “STRING” recursively.

The find command fill look for all the files in the specified folder and the grep command will search for the text in the file and -l option in the grep command will tell us it to print the path of files containing the text.

Let us see some examples,

Example 1: Find files containing specific string in a folder recursively

Find files in a folder “/home/varun/work/”, which contains the text “Summary”,

find /home/varun/work/ -type f -exec grep -l "Summary" {} \;

Output:

/home/varun/work/test1.xml
/home/varun/work/projects/data.xml
/home/varun/work/projects/first/results.xml

It will give us the path of the files which contain the text “Summary” in the folder “/home/varun/work/”.

If we want to look for files containing specific text in the current folder only then you do not need to provide the absolute path you can just provide the.in the directory option of the find command and then use the grep command with it to find all the files in the current folder which contains the text let us see the example

Example 2: Find files containing specific string in current folder recursively

To find files containing the text “Summary”, in current folder only, we don’t need to provide the full path of folder. We can just provide the “.” i.e. the current directory.

find /home/varun/work/ -type f -exec grep -l "Summary" {} \;

Output:

./work/test1.xml
./work/projects/data.xml
./work/projects/first/results.xml

Using the grep command

We can also use the grep command to search for a specific text in a folder recursively. It will go through all the files in the folder one by one and all its subdirectories, and give us the paths of files which contains the specific text along with the line numbers at which match is found.

Example 1: Find files containing specific text in a folder recursively

Find files in a folder “/home/varun/work/”, which contains the text “Summary”,

grep -r "Summary" /home/varun/work/*

Output:

/home/varun/work/test1.xml:<h2 id="summary">Summary</h2>
/home/varun/work/projects/data.xml:<li><a href="#summary">Summary</a></li>
/home/varun/work/projects/first/results.xml:<h2 id="summary">Summary</h2>

The grep command will search for the specified text recursively in all files in the given directory and its subdirectories, and display the filepaths and the lines where the text is found.

Example 2: Find files containing specific text in current directory recursively

To search for the files with text in the current directory only, you don’t need to provide the full path of folder. You can just provide a “.” to denote the current folder. For example, find files in current folder, which contains the text “Summary”,

grep -r "Summary" .

The grep command will search for the specified text recursively in all files in the current directory and its subdirectories, and display the relative path of files and the lines where the text is found.

Summary

We learned how to find the files containing a text in Linux.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top