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.

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: You can also exclude specified directories by following the format below: 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.

Using find to Find a Specific Word in a File

While the find command’s syntax is more complicated than grep, some prefer it. 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. If you want to search within a specific file or directory, you can append that file or fully-qualified pathname to your search.

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! All screenshots by Miguel Leiva-Gomez. 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. 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!