HomeLinuxHow to Find Files Containing Specific Text in Linux

How to Find Files Containing Specific Text in Linux

Sometimes, you need to find a file that contains a specific word or phrase. Linux provides powerful tools to help you search for text within files. This guide will show you how to use these tools.

Grep command

How to Find Files Containing Specific Text with Grep Command?

grep is a command-line utility for searching plain-text data for lines that match a regular expression. It’s very powerful and commonly used.
To search for a specific text containing files within the current directory, open your terminal and type:

grep -l 'your_text' *

Replace your_text with the text you’re searching for. This command will list all lines contained your_text in the files within the current directory.

Example: Searching the keyword “five centuries” in the current directory files:

Searching Recursively:
If you want to search through all files in the current directory and all its subdirectories, use the -r (recursive) option:

grep -lr 'your_text' .

The dot . tells grep to start searching from the current directory and go through all subdirectories.

Example: Searching the keyword “five centuries” in the directories recursively:

Ignoring Case:
To ignore case (uppercase or lowercase) while searching, add the -i option:

grep -rli 'your_text' .

Find Command

How to Find Files Containing Specific Text with Find Command?

Sometimes, you might want to search only specific types of files. You can combine find with grep for more targeted searches.
For example, to find all .txt files containing your_text, use:

find . -name "*.txt" -exec grep -il 'your_text' {} \;

This command will search for .txt files in the current directory and all subdirectories, then grep will search within those files for your_text.

Example: Finding the PHP files containing the text “five centuries” in the current directory:

ACK Command

How to Find Files Containing Specific Text with ACK Command?

ack is a tool similar to grep but optimized for searching source code.
You might need to install ack tool first. On Debian-based systems (like Ubuntu), you can install it with:

sudo apt install ack

After installation, search for your_text using ack, just type:

ack -il 'your_text'

This will search for your_text in all files in the current directory and subdirectories.

Example: Searching the keyword “five centuries” in the directories recursively using ack command:

AG Command

How to Find Files Containing Specific Text with AG Command?

ag is another tool similar to ack, and it is known for its speed.
You might need to install ag first. On Debian-based systems, you can install it with:

sudo apt install silversearcher-ag

After installation, search for your_text using ag, just type:

ag -il 'your_text'

This will search for your_text in all files in the current directory and subdirectories.

Example: Searching the keyword “five centuries” in the directories recursively using ag command:


Using these tools, you can quickly find files containing specific text in Linux. Whether you use grep, find, ack, or ag, each has its strengths and is suited for different scenarios.

Scroll to Top