Magnifying glass

How to find all files containing a specific text on Linux

This article describes the different methods that you can use to search for a specific content inside of any file. This may be useful if you need to find some word or sentence in a large group of files.

Please keep on reading this guide, we will show various commands that are available on a Linux operating system.

"grep" command

Let's start with the grep command. The very popular use case is when you need to search for a text recursively inside the current folder. We'll do it with the help of i flag, which makes the search case insensitive, and the l flag, which tells the command to only list the files.

Please see the sample below:

grep -lir "text to find" ./*.txt

It's also possible to specify the full path to the directory with files:

grep -lir "text to find" /home/username/files

To search for text in case-sensitive mode, please remove the i flag. See the example:

grep -lr "text to find" ./*.txt

Previously we've used the r flag, which enabled the recursive search inside the inner folders. However, you can search in the current folder only. See how the command looks in that case below:

grep -l "text to find" ./*.txt

You may also pass the single file as an argument into the grep command and look for content only inside of that file:

grep -l "text to find" ./file.txt

To search for content inside of multiple files, you have to list them one by one. Below is the example:

grep -l "text to find" ./file1.txt ./file2.txt

Popular grep options

Earlier in this guide, we covered only the basic options. However, the grep command is very powerful and contains much more advanced options.

To see the full description of this command, please run in console:

man grep

Below, we'll describe some of the additional flags that you can use with the grep utility.

  • -l - print only the name of the file that contains the content that you're looking for.
  • -i - search for a text in case-insensitive mode.
  • -r - recursively search for files. However, symbolic links will be followed only if they are specified in the command.
  • -R - similar to the above option. Specifies that the search must be recursive, with the only difference that all the symbolic links must be followed.
  • -w - this option instructs to find the whole words only, either in the middle, beginning or at the end of the line. This flag will not work if the x flag is set.
  • -x - find strings that match the whole line.
  • -c - disables the regular behavior and instructs to print the number of lines for each file.
  • -q - this option enables the quiet mode.
  • -n - this flag can be given to the grep command if you want to see the line numbers
  • -h - if set, then the file name will not be added to the output
  • -H - this flag specifies that the fill name has to be added to the output

Search for a pattern

It's also possible to search for a pattern inside of any file. The very basic example may look like the following:

grep -rw ./ -e "pattern"

The good thing is that you may specify multiple patterns by providing additional -e flags, see how it looks below:

grep -e "pattern1" -e "pattern2" filename

Search for a pattern inside of files with specific extension

The grep utility provides a feature that allows searching for content inside of files with specific extensions. To do that, you have to use the --include option and list the file extensions. Please see our example below:

grep --include=\*.{txt,info} -rw ./ -e "pattern"

"find" and "grep" commands

In very specific situations you may want to use the find command to search for the files first. Then the result may be passed into and filtered by the grep command.

Such a solution may be useful sometimes, as the find utility has a different set of command line options.

Please see the example below. We search for a file inside of the current folder. Then the grep command prints file names.

find ./ -type f -exec grep -l "string-to-search" {} \;

ack command

The ack tool is another option that you can use to search for content inside of a file. But the issue is that it may not be available on all systems.

If you really want to use this utility, you can install it. Following are the commands that will get you this tool for the Ubuntu Linux.

sudo apt-get update
sudo apt-get install ack

For the Fedora operating system, the installation process is very simple as well, and may look like the below command:

sudo dnf install ack

For the other linux versions, you may try to find instructions online on how to do that.

Now, after the tool is on your system, the next step is to try to find something. The basic example looks like the following:

ack 'content-to-find'

It will find the content inside of the current folder.

To search inside of a different location, you may explicitly provide a path to the ack command. See our example below:

ack 'content-to-find' /

But please keep in mind, that by default, the ack will search inside of text files. However, you may use the -a flag that tells ack to find text inside of all files. But some directories or files still may be skipped, like backups, etc.

Conclusion

As you see, it's very easy to find specific content inside of a file on the Linux operating system. You only have to choose the desired command and pass the options that are suitable for you. The rest of the work will be done by the relevant command.

However, this guide covers only the basics of searching for content. To get a more deeper knowledge, you may want to study the relevant man pages and/or read additional resources and Linux commands.

Related Articles

Computer terminal

How to copy directory in Linux

Computer on table

How to change user in Linux terminal

Comments

Leave a Reply

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