π 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 flag -
n
- show line numbers in the matches -
o
- print only the matches, and not the lines (particularly useful with regex)
Top comments (6)
We can also use some advanced regex like in this example
any word preceded by big
outputs each line that NOT starts with big
The grep below will catch only lines that start with any word but not followed
by house
Now any word followed by house
The final example:
I had an html file with some wallpapers "name" on it, and I wanted to print just the names without full path
And an ASCII table about some of these regex patterns
Are we talking about GNU grep or BSD grep? It is worth noting as they differ in many places. On all GNU Linux distros you get GNU grep by default, while on OpenBSD, FreeBSD, NetBSD and Mac OS you get BSD grep...
Good question! I'm using GNU grep on my machine but after consulting the documentation for BSD grep, I verified that everything mentioned on this page should work identically.
Thank you! Maybe you could add that grep can also be piped to bash for filtering the terminal output. Most of the time I use it this way.
$ command | grep keyword
Will do!
Just what I needed. Thanks for sharing.