By default, most search tools look at file names, not file contents. However, the most famous GNU search program, grep
, will look inside files with the correct flags. Here we show you how you can find specific word(s) in a file on Linux.
Also read: How to Find Large Files in Linux
Using grep to Find a Specific Word in a File
By default, grep searches through the contents of files as well as their file names. It’s included on the majority of Linux systems and is generally identical across distros. Programmers looking through the file contents of their projects might prefer to run a different command, like ack
.
Depending on how the file is encoded, grep
may not always be able to look inside. But for most text-based formats, grep
can scan the text of the file for the specified pattern.
grep -rw '/path/to/search/' -e 'pattern'
- The
-r
flag sets grep to recursive mode, navigating through all the directories contained within the specified directory. - The
-w
flag searches for whole word matches. This means that “red” will match only the actual word “red” rather than any string of letters containing it, like “redundant” or “tired.” It does this by looking only for “red” surrounded by what is known as non-word constituent characters (i.e., anything that isn’t a letter, number or underscore). - The
-e
flag prefaces the pattern to search for. It supports regular expressions by default.
To speed up grep, you can use the --exclude
and --include
flags to limit the search to certain types of files. For example, --exclude='*.csv'
will not search within any files with the .csv extension. --include='*.txt'
, on the other hand, will only search within files with the .txt extension. The flag can be added immediately after the grep command as shown below:
grep --exclude='*.csv' -Rw '/path/to/search' -e 'pattern'
You can also exclude specified directories by following the format below:
grep --exclude-dir='{dir1,dir2,*_old}' -Rw '/path/to/search' -e 'pattern'
This command will not search in any directories in the present working directory named dir1, dir2, or matching the pattern *_old, eliminating them from the search process. It will execute the specified recursive, full-word match search on all other files in the present working directory.
Interesting Flags You Can Use With grep
Now that you have been properly acquainted with grep and how it works, here are some useful flags you can attach to your command:
-i
– Makes grep do a non-case-sensitive search. For example, searching for “Kraken” will return a result when grep finds matches for “kraken” or “kRaken”.-n
– Show line numbers next to matches. This is especially useful for programmers or people looking through large config files.-R
– As with-r
, grep will do a recursive search through all subfolders, but this specific flag will follow symbolic links (shortcuts).-s
– Suppresses error messages. If you’re getting many distracting errors about files that don’t exist, can’t be read, or have inappropriate permissions, pass this so that grep can stick to showing you matches it finds.
Also read: How to Find a File in Linux Using the Find Command
Using find to Find a Specific Word in a File
While the find
command’s syntax is more complicated than grep, some prefer it.
find . -name "*.php" -exec grep "pattern" {} ;
This command will use find’s -exec
flag to pass the found files to grep for searching. With a clever arrangement of syntax, you can use find
‘s faster file-system search to locate the specific file types you want to search within, then pipe them to grep
to search inside the files.
Note that find
only looks at filenames, not contents. That’s why grep is required to search file text and contents. The normal grep flags should be fully operational from within the -exec
flag.
Using ack to Find a Specific Word in a File
The ack command is likely the fastest searching tool, but it’s not as popular as grep
. The command below will search within the current directory.
ack 'pattern'
If you want to search within a specific file or directory, you can append that file or fully-qualified pathname to your search.
ack 'pattern' /path/to/file.txt
Also read: How to Install and Manage Python Versions in Linux
Want a GUI? Use Catfish
If you’re allergic to terminals, all the popular Linux distros offer Catfish, a versatile GUI application that allows you to search files and their contents for a specific string. It isn’t as advanced as the commands discussed here, but it provides a valuable service to people who would prefer a graphical interface.
To search file contents for a specific word, click the menu on the top-right corner of Catfish and click on “Search file contents.”
After you’ve set it up, type what you want to search for in the top bar and press Enter. That’s it!
Also read: The Differences between Su, Sudo Su, Sudo -s and Sudo -i
Frequently Asked Questions
How do I stop a grep search before it's done?
To stop any ongoing process in your terminal, simply press Ctrl + C on your keyboard. This sends a signal to terminate the running process. It’s also why you can’t use normal copy/paste shortcuts from within a terminal emulator.
Are there any file managers that search file contents?
If you use the KDE Plasma desktop environment, its default file manager (Dolphin) already has this capability. When you click the search button to look for files, there’s an option to choose between file names and file content underneath the search bar.
Just click on “Content” if you want to search for words within files. It won’t be as advanced as using grep, but it does the job.
Are there other uses for grep?
Grep isn’t just for searching your file system! Interestingly enough, you can pipe it through almost any command to search through its output instead of having to manually find something yourself.
Grep can be used to filter command output to show you things that are more relevant to you. Experiment enough with it and you’ll eventually master the art of diagnosing and getting to know your computer!
All screenshots by Miguel Leiva-Gomez.
Our latest tutorials delivered straight to your inbox