Find Files Containing Specific Text in Linux

Linux Icon Big 256

Linux, regardless of the distro you use, comes with a number of GUI tools which allow searching for files. Many modern file managers support file searching right in the file list. However, there are a number of situations when you need to use the console, for example, during an SSH session or when the X server doesn't start. Here is how you can find files containing specific text quickly using the terminal.

If you can use the GUI, searching for files is not a problem. In my favorite XFCE desktop environment, the Thunar file manager allows searching for files by typing the file name directly in the file list.

However, none of the file managers I've tried allow searching for file contents.

To bypass this limitation when working in a GUI session, you can use Сatfish. Сatfish is a popular search tool. It allows searching for file contents. You need to click on the gear icon and enable the option Search File Contents.

However, when it comes to the terminal, you cannot use any graphical tool. Also, I find Catfish not useful and prefer classic console tools over it. Here are the methods and tools I use.

To find files containing specific text in Linux, do the following.

  1. Open your favorite terminal app. XFCE4 terminal is my personal preference.
  2. Type the following command:
    find ./ -type f -exec grep -l "text to find" {} \;
  3. This will produce the list of files which contains the required text portion.

The find command will search for files starting from the current directory, "./". You can specify the desired path instead.

The option -type f tells it to search only for files and exclude directories. Also, you can make it search only for a certain file type by adding the option as follows:
find ./ -type f -iname "*.txt" -exec grep -l "text to find" {} \;

See the following screenshot:

The -exec option calls the grep tool for each of the matching files to check their contents. If the text is found in the file, its name will be added to the output, thanks to the -l option.

Alternatives

There are alternatives to the methods described above. First of all, you can use only the grep tool. Try the following command:

grep -iRl "text to find" ./

  • -i stands for ignore text case.
  • -R stands for recursive file processing.
  • -l stands for "show the file name, not the result itself".

As you can see, this method is less flexible than the find command.

Midnight Commander

Another method I often use is Midnight Commander (mc), the console file manager app. Unlike find or grep, mc is not included by default in all Linux distros I've tried. You may need to install it yourself.

To find files containing some specific text using Midnight Commander, start the app and press the following sequence on the keyboard:
Alt + Shift + ?
This will open the search dialog.

Fill in the "File name:" section. You can specify something like "*.txt" to search for your text in text files only.

Now, fill in the Content: box. Type the text which the files contain and hit the Enter key.

It will find all files which match the criteria.

That's it.

Support us

Winaero greatly relies on your support. You can help the site keep bringing you interesting and useful content and software by using these options:

If you like this article, please share it using the buttons below. It won't take a lot from you, but it will help us grow. Thanks for your support!

Author: Sergey Tkachenko

Sergey Tkachenko is a software developer who started Winaero back in 2011. On this blog, Sergey is writing about everything connected to Microsoft, Windows and popular software. Follow him on Telegram, Twitter, and YouTube.

2 thoughts on “Find Files Containing Specific Text in Linux”

Leave a Reply

Your email address will not be published.

Exit mobile version
Using Telegram? Subscribe to the blog channel!
Hello. Add your message here.