- How to Find Files Containing Specific Text String in Linux
- On this page
- Search Specific Text in Linux Using Catfish GUI Tool
- Install Catfish Search Tool on Linux
- Search File Contents Using Catfish
- Search Specific Text in Linux Using Grep Command
- Search for Particular Text in Files
- Find File Names That Contains a Given String
- Print File Names and Line Numbers That Contains a Given String
- Search for Specific Text in Specific File Types
- Search Specific Text In Linux Using MC (Midnight Commander)
- Install Midnight Commander on Linux
- Search File Contents Using Midnigh Commander
- 6 Best CLI Tools to Search Plain-Text Data Using Regular Expressions
- 1. Grep Command
- 2. sed Command
- 3. Ack Command
- 4. Awk Command
- 5. Silver Searcher
- 6. Ripgrep
How to Find Files Containing Specific Text String in Linux
Almost all file managers for Linux like Nemo and Thunar by default provide an option to search for files. But if you want to search for a string inside a file’s content using file manager, the majority of them do not let you do so.
In this article, I’ll discuss different ways that you can use on any Linux distribution to find all files containing specific text strings or words by recursively digging through all sub-directories.
On this page
Search Specific Text in Linux Using Catfish GUI Tool
I’ll start with the easiest way that can work for all including beginners to advance Linux users. Catfish is a simple and lightweight GUI-based file search tool for Linux desktops. Along with searching for files on your system, you can also use it to find all files that contain a particular word.
Install Catfish Search Tool on Linux
The Catfish package is already available on the primary Debian and Ubuntu repositories. Hence, If you’re using Debian or Ubuntu-based distributions, you can simply install it by running the command:
$ sudo apt install catfish [On Debian/Ubuntu & Mint]
On other Linux distributions, you can install it from the default repositories using your package manager.
$ sudo yum install catfish [On CentOS/RHEL 7] $ sudo dnf install catfish [On CentOS/RHEL 8 & Fedora] $ sudo pacman -S catfish [On Arch Linux] $ sudo pkg_add -v catfish [On FreeBSD]
If the package is not available, you can download the latest release file, extract the downloaded tar.bz2 file, and run the following command:
$ sudo python3 setup.py install
Search File Contents Using Catfish
Once installed, you only need to do is enable “Search file contents”, select directories from the top-left dropdown option, and type the text you want to search for. It will list down all files containing text along with file size and location.
You can also use file type and modified date filter from the left panel to reduce the search scope. You can even add a directory path where you don’t want to search.
Search Specific Text in Linux Using Grep Command
Beside the GUI way, grep is one of the popular command line tools that can be used to search inside file content. Since almost all unix-like operating systems ship grep utility by default, you don’t need to install it.
Search for Particular Text in Files
Now to search and find all files for a given text string in a Linux terminal, you can run the following command. Here, the ‘-r’ or ‘-R’ flag recursively searches through the all subdirectories inside the specified directory.
$ grep -r “linuxshelltips” /home/sarvottam/
Find File Names That Contains a Given String
If you want to print only file names and hide the text from the output, you can use the ‘-l’ flag.
$ grep -rl “linuxshelltips” /home/sarvottam/
Print File Names and Line Numbers That Contains a Given String
Furthermore, you can also tweak the output using the following options available for grep:
$ sudo grep -rnwi “linuxshelltips” /home/sarvottam/
It is also worth mentioning that if you search through directories that require root permissions, you need to use the sudo command.
Search for Specific Text in Specific File Types
To further reduce the search scope, you can also specifically mention the type of file and directories to only look for while searching. “—include” , “—exclude” , and “—exclude-dir” are the options available to add file type and directory filter.
$ grep --include=\*.txt --exclude=\*. --exclude-dir=\bin -rnwi "linuxshelltips" /home/sarvottam/
Search Specific Text In Linux Using MC (Midnight Commander)
If you live in a terminal and still want to search text using GUI way, you can also try Terminal User Interface (TUI) based file manager tool, mc (midnight commander) – is a visual file manager that is used to search for files.
Install Midnight Commander on Linux
The Midnight Commander is available to install from the default repositories in the most of the Linux distributions.
$ sudo apt install mc [On Debian/Ubuntu & Mint] $ sudo yum install mc [On CentOS/RHEL 7] $ sudo dnf install mc [On CentOS/RHEL 8 & Fedora] $ sudo pacman -S mc [On Arch Linux] $ sudo pkg_add -v mc [On FreeBSD]
Search File Contents Using Midnigh Commander
Inside the terminal, mc provides a visual representation of the filesystem in which you can navigate either through keyboard or mouse. To search file content, you can open a search dialog using ALT+SHIFT+? and enter the text in the “Content:” section.
As you can see in the above picture, you can also use filters like ignore case, whole words, and regular expression by just checking and unchecking it.
Conclusion
All the above-mentioned applications are beginner’s friendly, free-to-use and open source to add your own enhancements. Alternatively, you can also try other free tools like Ack, The Silver Searcher, Ripgrep, and find command.
6 Best CLI Tools to Search Plain-Text Data Using Regular Expressions
This guide takes a tour of some of the best command-line tools that are used for searching matching strings or patterns in text files. These tools are usually used alongside regular expressions – shortened as REGEX – which are unique strings for describing a search pattern.
Without much further ado, let’s dive in.
1. Grep Command
Coming in the first place is the grep utility tool – is an acronym for Global Regular Expression Print, is a powerful command-line tool that comes in handy when searching for a specific string or a pattern in a file.
Grep ships with modern Linux distributions by default and gives you the flexibility to return various search results. With grep, you can perform a vast array of functioning such as:
- Search for strings or matching patterns in a file.
- Search for strings or matching patterns in Gzipped files.
- Count the number of string matches.
- Print the line numbers that contain the string or pattern.
- Search recursively for the string in directories.
- Perform a reverse search ( i.e. Display results of strings not matching the search criteria).
- Ignore case sensitivity when searching for strings.
The syntax for using the grep command is quite simple:
For example, to search for the string ‘Linux‘ in a file, say, hello.txt while ignoring case sensitivity, run the command:
To get more options that you can use with grep, simply read our article that examples more advanced grep command examples.
2. sed Command
Sed – short for Stream Editor – is another useful command-line tool for manipulation text in a text file. Sed searches, filters and replaces strings in a given file in a non-interactive manner.
By default, sed command prints the output to STDOUT (Standard Out), implying that the result of the execution is printed on the terminal instead of being saved in a file.
Sed command is invoked as follows:
$ sed -OPTIONS command [ file to be edited ]
For example, to replace all instances of ‘Unix‘ with ‘Linux‘, invoke the command:
If you want to redirect output instead of printing it on the terminal, use the redirection sign ( > ) as shown.
$ sed 's/Unix/Linux' hello.txt > output.txt
The output of the command is saved to the output.txt file instead of being printed on the screen.
To check out more options that can be used, once again check out the man pages.
3. Ack Command
Ack is a fast and portable command-line tool written in Perl. Ack is considered a friendly replacement for grep utility and outputs results in a visually appealing manner.
Ack command searches the file or directory for the lines that contain the match for the search criteria. It then highlights the matching string in the lines.
Ack has the capacity to distinguish files based on their file extensions, and to a certain extent, the content in the files.
$ ack [options] PATTERN [FILE. ] $ ack -f [options] [DIRECTORY. ]
For example, to check for the search term Linux, run:
The search tool is quite intelligent and If no file or directory is provided by the user, it searches the current directory and subdirectories for the search pattern.
In the example below, no file or directory has been provided, but ack has automatically detected the available file and searched for the matching pattern provided.
To install ack on your system run the command:
$ sudo apt install ack-grep [On Debian/Ubuntu] $ sudo dnf install ack-grep [On CentOS/RHEL]
4. Awk Command
Awk is a fully-fledged scripting language and also a text processing and data manipulation tool. It searches files or programs that contain the search pattern. When the string or pattern is found, awk takes action on the match or line and prints the results on STDOUT.
The AWK pattern is enclosed between curly braces while the entire program is enclosed in single quotes.
Let’s take the simplest example. Let’s assume you are printing the date of your system as shown:
Suppose you only want to print out the first value, which is the day of the week. In that case, pipe the output into awk as shown:
To display subsequent values, separate them using a comma as shown:
The command above will display the day of the week and the date of the month.
To get more options that you can use with awk, simply read our awk command series.
5. Silver Searcher
The silver searcher is a cross-platform and opensource code searching tool similar to ack but with an emphasis on speed. It makes it easy for you to search for a specific string within files in the shortest time possible:
$ ag OPTIONS search_pattern /path/to/file
For example, to search for the string ‘Linux‘ in a file hello.txt invoke the command:
For additional options, visit the man pages:
6. Ripgrep
Lastly, we have the ripgrep command-line tool. Ripgrep is a cross-platform utility for searching regex patterns. It’s much faster than all of the earlier-mentioned search tools and recursively searches directories for matching patterns. In terms of speed and performance, no other tool stands out that Ripgrep.
By default, ripgrep will skip binary files/hidden files and directories. Also, be advised that by default it won’t search for files that are ignored by .gitignore/.ignore/.rgignore files.
Ripgrep also allows you to search for specific file types. For example, to limit your search to Javascript files run:
The syntax for using ripgrep is quite easy:
For example. To search for instances of the string ‘Linux’ in files located inside the current directory, run the command:
To install ripgrep on your system run the following commands:
$ sudo apt install ripgrep [On Debian/Ubuntu] $ sudo pacman -S ripgrep [On Arch Linux] $ sudo zypper install ripgrep [On OpenSuse] $ sudo dnf install ripgrep [On CentOS/RHEL/Fedora]
For additional options, visit the man pages:
These are some of the most widely used command-line tools for searching, filtering, and manipulating text in Linux. If you have other tools you feel we have left out, do let us know in the comment section.