📃 Cheatsheet
Search for a keyword in a file:
grep keyword file.txt
Search using a regular expression:
grep -E 'regex' file.txt
Search for a keyword in a directory:
grep -r keyword ./directory/path
Combine multiple flags/options to search through a directory, display line numbers, and use regex:
grep -Enr 'regex' ./directory/path
🔎 What is grep?
Grep is a CLI utility for plain text searches with regex (regular expressions). It’s like a Ctrl/Command + F but through a bunch of files and in the command line.
It’s another one of those ancient utilities that have proliferated through all Linux and Unixy distributions. Similar to Rsync and tar that I’ve talked about in the past.
Fortunately, unlike rsync or tar, grep requires a lot less flags and knowledge of the tool to be very productive with it.
Note: Are you on Windows? Check out my article on “how to ‘grep’ in Powershell”
📄 Searching for a keyword in a file
If you’re in the command line and you don’t necessarily want to open a file, or you’re dealing with an enormous file, it can be very handy to look for just one line of text using grep.
grep searchkeyword file.txt
Grep will return all the lines containing the text. Depending on your grep variant, you might get line numbers, the keyword highlighted, and other fun information.
🔬 Searching using regular expressions with the -E
flag
Need to use a regular expression because you’re a pro at those? Wrapping a regex in quotes will do it most of the time; however, some syntax isn’t supported this way so you have to use the -E
flag. For example:
grep '^2020-0[0-4]' file.txt
This will work just fine, the []
syntax is supported in basic regex mode; however, specifying occurrences with {}
won’t work so I recommend defaulting to the -E
flag for any regex so you can do neat stuff like this:
grep -E '[0-9]{4}-[0-9]{2}-[0-9]{2}' logfile.txt
This regex will fail in basic mode but the extended mode will match any yyyy-mm-dd
date anywhere within your text.
📂 Searching through multiple files/directory
Grep lets you also recursively search through many files and directories.
grep -r searchkeyword directoryName
Grep will return all of the matching lines and tell you in which files they were.
🏁 More flags
Grep has a great deal of options. Their manual page is very long and I suggest giving it a look when you feel like grep’s basic capabilities just aren’t enough for you.
Here’s the syntax to add or combine flags when using grep:
grep -flags searchkeyword directoryOrFilename
grep -ir antonin ./documents
In the example, -ir
combines both the i
and the r
flag. Similarly, -Er
would let us recursively search with extended regex.
Quick run down of some of these useful flags:
i
- case insensitive flagn
- show line numbers in the matcheso
- print only the matches, and not the lines (particularly useful with regex)