The simple command that searches files for a word match: grep

There’s a command line tool that system administrators have lived by for decades, and it can make your life easier if you need to quickly find all the files that contain a certain word in them. It’s called grep, and while it was developed for software developers to search files (something that’s constantly needed in programming) it’s potentially useful for anyone using a computer to search for text matches.

grep searches text files (they must be in plaintext, but can be kind of file from .txt to .html to .js) and then returns a list of the results – which sounds simple, and it can be. Guides to grep tend to be highly technical and jump right into the advanced querying you can do: but grep is the kind of tool that is worth using even for one or two things.

Here’s the use case: you have a folder full of text files, transaction records to various cities. There are hundreds of entries, organized in folders within folders. How could you quickly find the files that mention the city of Chicago?

The following command, issued in the folder with all the files:

grep -rl "Chicago"

Would return a list of files that contain the word “Chicago”:

03-2019/records/entry_6738.txt
04-2019/records/entry_3933.txt
04-2019/records/entry_4785.txt
...

The name of the command, grep, is a loose acronym for “globally search a regular expression and print” – jargon that translates to “look everywhere for a pattern and give me the results back.”

The above version of the command has two options, or flags, on it: the -rl before the text pattern that needs to be searched for in quotation marks. The r is for “recursive” (search nested folders for files) and the l is for “line” (give me the path of the file on a line).

To go deeper, visit this excellent introduction that’s still accessible for non-specialists: Basics of grep, from Stanford’s Computational Journalism Lab.