Advertisement

Find Files Containing Specific Text in Linux

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, most of them do not allow you to search inside a file's contents. Here are two methods you can use to search for file contents in Linux.

Advertisеment


Probably, there are more methods available. There's Catfish, a popular search tool with a search index, which can find your files really quickly. It comes with an option to search for file contents, but it does not work reliably for me.

Catfish Linux

I would like to share the methods I use myself.
The first method involves the grep utility, which exists in any distro, even in embedded systems built on busybox.

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

  1. Open your favorite terminal app. XFCE4 terminal is my personal preference.
  2. Navigate (if required) to the folder in which you are going to search files with some specific text.
  3. Type the following command:
    grep -iRl "your-text-to-find" ./

    Here are the switches:
    -i - ignore text case
    -R - recursively search files in subdirectories.
    -l - show file names instead of file contents portions.

    ./ - the last parameter is the path to the folder containing files you need to search for your text. In our case, it is the current folder with the file mask. You can change it to the full path of the folder. For example, here is my command

    grep -iRl "linux" /home/user/Documents/winaero

Grep Search File Contents

Note: Other useful switches you might want to use with grep:
-n - show the line number.Grep Search Line Number
-w - match the whole word.

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

Find files containing specific text with mc

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.

Mc Search File Contents

Fill in the "Content:" section and press the Enter key. It will find all files with the required text.

Mc Search Results

You can place these files in the left or right panel using the Panelize option and copy/move/delete/view/do whatever you want them.

Mc Search Results Panelize

Midnight Commander is a very time-saving tool when it comes to search.

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!

Advertisеment

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.

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

  1. The code that you provided helped me. There are also another commands which I cannot remember to find text in files but this one is made it quickly. I have bookmarked this post for further usage. Thank you.

  2. Midnight Commander reminds me of XTree for DOS way, evidently, way way, back in the day!! :-) Anyone else remember!?

Leave a Reply

Your email address will not be published.

css.php
Using Telegram? Subscribe to the blog channel!
Hello. Add your message here.