How to Find Files Containing Specific Text in Linux

We have two text files, file1.txt and file2.txt, on our system, but we can’t recall which file contains which data. However, we do remember the content of both files:

Learn and Grow on NetworkHunt.

This is not the required File.

In this post, we’ll explore several methods to filter through these files and determine which one contains the respective data.

Method-1: Using grep command

The grep is a powerful utility used to search for specific patterns within files or input provided by the user. The name “grep” stands for Global Regular Expression Print, which reflects its function of searching through text using regular expressions (regex) and printing lines that match the specified pattern.

We can use grep to filter out files containing specific strings in Linux as below-

Using grep with the -l option will return the files containing the exact string within the current working directory.

$> grep -l NetworkHunt file1.txt file2.txt
filter

Use -i option in the above command to return files containing the string ignoring the case within the current working directory.

$> grep -l -i networkhunt file1.txt file2.txt
How to Find Files Containing Specific Text in Linux - screenshot

Use -r option to look for the files containing the string, recursively within the subdirectories of current working directory.

$> grep -l -i -r networkhunt
How to Find Files Containing Specific Text in Linux - screenshot

Note- In the above image you may observe that there are many files on the system other than /Files/file1.txt, that contain the string networkhunt in them.


Method-2: Using find command

The find command is used to search for files and directories in a directory hierarchy based on various criteria, such as name, type, size, and more. It is highly versatile and powerful for locating files on your system. The find command takes path of the directory and file name as argument.

We can use combination of find and grep command to filter out files containing specific string as follows-

$> find . -type f -exec grep -l “NetworkHunt” {} \;

How this command works-

  • find . -type f : Searches for all regular files in the current directory and all subdirectories.
  • -exec grep -l “NetworkHunt” {} : For each file found by find, the grep command is executed which recursively search inside the file for the string “NetworkHunt”. If a match is found, grep prints the name of the file, not the contents of the match.
  • \; : This marks the end of the -exec command.
How to Find Files Containing Specific Text in Linux - screenshot

Method-3: Using mc -> Midnight Commander

Midnight Commander (mc) is a text-based file to provide an easy-to-use, visual, and interactive interface for managing files and directories, much like graphical file managers such as Windows Explorer or macOS Finder, but within a terminal environment.

Step-1: Open Midnight Commander by typing mc in your terminal and pressing Enter.

How to Find Files Containing Specific Text in Linux - screenshot

Step-2: To access the Find File Dialog, press Alt + Shift + ?.This will allow you to search for files based on content or filenames.

How to Find Files Containing Specific Text in Linux - screenshot

Step-3: Set the Starting Path, At the top of the Find File dialog, you’ll see a field labelled ‘Starting’. In this box, type the path where the files are located.

Step-4: In the Content box below, type the string or text you want to search for within the files.

How to Find Files Containing Specific Text in Linux - screenshot

Step-5: After entering the required information, press Enter to start the search. Midnight Commander will display all matching results in the panel.

How to Find Files Containing Specific Text in Linux - screenshot

Method-4: Using ripgrep command

ripgrep (often abbreviated as rg) is a fast, command-line search tool used for searching through files and directories. It is similar to grep, but it is designed to be faster, more efficient, and have more modern features. ripgrep is written in Rust, which is known for its performance and memory safety, making ripgrep one of the fastest search tools available today.

We can use ripgrep to filter out files containing specific strings as below-

$> rg “NetworkHunt”

Note- Make sure you have ripgrep installed on your system, because it is not installed by default on many distros. To install ripgrep use the command below-

$> sudo apt install ripgrep
How to Find Files Containing Specific Text in Linux - screenshot

Method-5: Using ack command

The ack search tool, similar to grep, designed for searching through source code. It is often used by developers and system administrators to quickly find specific strings or patterns within files, directories, or codebases. ack has several advantages over grep, including better default behaviour, performance optimizations, and additional features tailored for programming tasks.

We can use ack to filter out files containing specific strings as below-

$> ack “NetworkHunt”

Note- Make sure you have ack installed on your system, because it is not installed by default on many distros. To install ack use the command below-

$> sudo apt install ack
How to Find Files Containing Specific Text in Linux - screenshot