- 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
- Find All Files With Given Text Using SearchMonkey GUI Tool In Ubuntu
- SearchMonkey is your desktop search engine
- Install SearchMonkey in Ubuntu 16.04
- How to find a text file which contains a specific word inside (not in its name)
- 7 Answers 7
- Simple grep
- find + grep
- Perl
- poor-mans recursive grep in recursive bash script
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.
Find All Files With Given Text Using SearchMonkey GUI Tool In Ubuntu
At times we need to search files which contain some particular text or words in them. This often happens if you are a developer or programmer. Now, one can always use Linux commands to find all files containing specific text, but not everyone is command line fan. I have seen excellent programmers that rely on GUI tool than the command line.
If you too prefer GUI tools, than SearchMonkey is one perfect application for you.
SearchMonkey is your desktop search engine
SearchMonkey searches the directories recursively so that you can sit and relax while SearchMonkey does his work. Users can search for file names and contents using powerful regular expressions. This enables SearchMonkey to be much more precise when it returns hits.
In addition, SearchMonkey doesn’t just show you a list of files that might contain what you are looking for, it helpfully displays the content with the matches highlighted.
Install SearchMonkey in Ubuntu 16.04
SearchMonkey is available in Ubuntu 16.04 (and other versions as well) repository. You can install it via Ubuntu Software Center or by just typing the following command in terminal:
sudo apt-get install searchmonkey
Once installed, you can launch SearchMonkey from Unity Dash. The interface is easy to understand and easy to use.
SearchMonkey finds the files you need quickly, and without building a drive map first. Each search takes place in real time, and results are displayed in context as soon as they are found! And above all, it’s a free and open source software.
How to find a text file which contains a specific word inside (not in its name)
I want to find a text file in my hard disk which contains a specific word. Prior to Ubuntu 12.4 I used to start in the dash an application, I think it was called «Search for file. «, whose icon was a magnifying glass.I can’t find that simple application any more.
7 Answers 7
You can use the grep command from terminal:
This command will find all occurrences of «word» in all the files under the current directory (or subdrectories).
if grep is called from a shell script, then the search keyword may not print highlighted, but the —color=auto flag can solve that.
sudo apt-get install gnome-search-tool
Open Search for files select Select More Options and
As suggested here, use mate-search-tool instead which seems to offer identical functionality. Install via sudo apt install mate-utils .
Here’s an overview of different methods that one can use for searching files for specific strings of text, with a few options added specifically to work only with text files, and ignore binary/application files.
One should note,however,that searching for word can get a little complex, because most line-matching tools will try to find a word anywhere on the line. If we’re talking about a word as string that could appear in the beginning or end of line, or alone on the line, or surrounded by spaces and/or punctuation — that’s when we’ll need regular expressions, and especially those that come from Perl. Here, for example, we can use -P in grep to make use of Perl regular expressions to surround it.
$ printf "A-well-a don't you know about the bird?\nWell, everybody knows that the bird is a word" | grep -noP '\bbird\b' 1:bird 2:bird
Simple grep
- -r for recursive search down from current directory
- -I to ignore binary files
- -H to output filename where match is found
Suitable for searching only.
find + grep
$ find -type f -exec grep -IH 'word' <> \;
- find does the recursive search part
- -I option is to ignore binary files
- -H to output filename where line is found
- good approach for combining with other commands within subshell, like:
$ find -type f -exec sh -c 'grep -IHq "word" "$1" && echo "Found in $1"' sh <> \;
Perl
#!/usr/bin/env perl use File::Find; use strict; use warnings; sub find_word< return unless -f; if (open(my $fh, $File::Find::name))< while(my $line = ) < if ($line =~ /\bword\b/)< printf "%s\n", $File::Find::name; close($fh); return; >> > > # this assumes we're going down from current working directory find( < wanted =>\&find_word, no_chdir => 1 >,".")
poor-mans recursive grep in recursive bash script
This is the «bash way». Not ideal, probably no good reason to use this when you have grep or perl installed.
#!/usr/bin/env bash shopt -s globstar #set -x grep_line() < # note that this is simple pattern matching # If we wanted to search for whole words, we could use # word|word\ |\ word|\ word\ ) # although when we consider punctuation characters as well - it gets more # complex case "$1" in *word*) printf "%s\n" "$2";; esac >readlines() < # line count variable can be used to output on which line match occured #line_count=1 while IFS= read -r line; do grep_line "$line" "$filename" #line_count=$(($line_count+1)) done < "$1" >is_text_file() < # alternatively, mimetype command could be used # with *\ text\/* as pattern in case statement case "$(file -b --mime-type "$1")" in text\/*) return 0;; *) return 1;; esac >main() < for filename in ./**/* do if [ -f "$filename" ] && is_text_file "$filename" then readlines "$filename" fi done >main "$@"