How to Find All Files Containing Specific Text on Linux

Many recent file managers support file searching just in the file list. Anyway, most of them do not let you search inside a file's contents. Here are some methods you can use to search for file contents on Linux using the command line. In this snippet “grep”, “rigrep”, “ack” and “find” commands are specified.

  1. Use grep tool to find the files containing specific text on Linux. For that follow these steps:
  • Open your favorite terminal app.
  • Navigate (if required) to the folder in which you are going to search files with some specific text.
  • Type the following command:
grep -iRl "your-text-to-find" ./

Here -i stands for ignore case, -R stands for recursive, -l stands for "show the file name, not the result itself" and ./ stands for starting at the root of your machine.

There are other useful parameters you might use with grep:

  • -n: shows the line number.
  • -w: matches the whole word.
  • -h: only outputs the line which matched (not the filename).
  • -s: suppress error messages about nonexistent or unreadable files.
  1. Alternative use for grep is rigrep. If you're working on large projects or big files, you had better use ripgrep. It will look like this:
rg "your-text-to-find" /
  1. Use ack. It is like grep for source code.
ack 'your-text-to-find' /
Recommendation: For rigrep and ack commands use regular expressions to specify the file type.
  1. Use find command.
# find / -exec grep -l “your-text-to-find” {} ;

To search in specific file extension files. Like search ‘your-text’ in all php files only, use the following command.

# find / -name '*.php' -exec grep -l "your-text" {} ;

Do you find this helpful?

Related articles