You probably already know how to use the grep command to search for a text string in a file on Linux. But what if you want to retrieve a list of files that contain the text string? This is a task best suited for grep or the find command. We'll show you how to do it in this guide.

In this tutorial you will learn:
  • How to use grep and find commands to find all files containing specific text
Find all files containing specific text
Find all files containing specific text
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires given linux commands to be executed as a regular non-privileged user

Search for specific text with grep command


SUBSCRIBE TO NEWSLETTER
Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


Without a doubt, grep is the best command to search a file (or files) for a specific text. By default, it returns all the lines of a file that contain a certain string. This behavior can be changed with the -l option, which instructs grep to only return the file names that contain the specified text.

Now let's see this in action. Use the following syntax in terminal, and specify all the files you want to search by appending their path and name to the end of the command.

$ grep -l example document1.txt document2.txt
Using grep to find which files contain the specified text
Using grep to find which files contain the specified text

The output from grep shows us that our search string "example" only exists in the document1.txt file. Also, don't forget that wildcards are permitted and can help make grep more efficient:

$ grep -l example *.txt

But let's be realistic. If you're only searching a couple of files, you probably wouldn't have wound up reading this guide. Most likely, you have a directory (or multiple directories) full of files that you need to search. That's no problem for grep as long as you include the -r (recursive) option in the command.

$ grep -lr example /path/to/directory1/*.txt /path/to/directory2

Or, to search the current directory and all subdirectories, omit the path at the end of the command.

$ grep -lr example
Using grep to search recursively
Using grep to search recursively


Consider also using the -i option, which makes your search string case insensitive. To learn about the rest of the grep command's functions in detail, check out our introduction to grep guide. The man page also contains helpful information:

$ grep man

Search for specific text with find command

If you would prefer to use the find command, you can use the following command syntax:

$ find /path/to/search -type f -exec grep -l "your-search-string" {} \;
Using the find command to search for files containing the text string
Using the find command to search for files containing the text string

Once again, add -i to the grep portion of the command to ignore case. The find and grep methods both work well. Use whichever one you prefer.

Conclusion

In this guide, we saw how to find all files containing specific text in Linux. We learned two command line methods to accomplish the task, but even more exist.

FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in:



Comments and Discussions