Grep is a small Unix program for finding matching patterns. Begun as a Unix program, it can be found on Linux as well as Mac and BSD. It can read just about any text, meaning it can read input from another commands, or it can open and look through files directly. Grep is insanely useful, especially for looking through directories from the command line.
With that, why not try one of the most common uses for Grep, finding a file in a directory. Look through your “~/Downloads” folder for any “jpg” images.
ls ~/Downloads | grep .jpg
Grep will list them all out and highlight the “.jpg” part since it’s what you searched for.
You pipe the output (with the command “|”) of just about any command into grep
. If you have a plain text file laying around, cat
it out and pipe the result into Grep to find a specific word.
cat file.txt | grep word
Grep will print out any lines in the file containing the word that you told it to look for.
Forget the Case
Unix-like systems are case sensitive, meaning that “Ubuntu” is completely different than “ubuntu.” That can be a pain when searching for something. Grep has a flag to eliminate the problem. Add the -i
flag to your search, and Grep will ignore case.
ls ~/Downloads | grep -i Ubuntu
Search in a File
It wouldn’t be all that convenient to need cat
every time you wanted to search a file. Actually, you don’t. Grep can search through files itself. Pass it the path to the file, and Grep will do the rest.
grep -i word /path/to/file.txt
Recursive Searches
Grep can search through more than one file or directory at the same time. Be warned, if you’re looking for file names, Grep will search through files by default, too. The -r
flag tells Grep to search recursively.
ls ~/Downloads | grep -r .deb
You can pair this with other flags, too. You may want to include the -I
flag when doing recursive searches to keep Grep from looking through binary files.
ls ~/Downloads | grep -iIr .deb
Find the Opposite
You can also tell Grep to look for everything not containing the pattern specified. This would be good in instances where you need to find an error or you have a directory with loads of files of one or two types.
grep -rv '192.168.1.110' /etc/nginx
Words and Lines
It also might be useful to tell Grep to search for complete words or lines, instead of anything containing a certain pattern. Imagine you’re looking for a string of characters that’s common as part of a word, but you need it to be separate. For example, the word “it.” Obviously, there wouldn’t be many times you’d actually want to look for a pattern like “it,” but it proves the point.
cat textfile.txt | grep -w it
Instead of printing out every word that contains the pattern, Grep will only print the word by itself. It does the same thing for entire lines with the -x
flag, so if you’re looking for a phrase or a single line in a configuration file, that can really help.
If you’re interested in investigating everything Grep can do, run man grep
in a terminal for the complete documentation. This article covered the basics and everything that you’ll normally need from this powerful tool. Share with us if you know of any powerful uses of the Grep command.
Image credit: Shell script points of interest to coordinates
3 comments
Comments are closed.
“cat file.txt | grep word”
“cat textfile.txt | grep -w it”
These are BAD examples which. newbies reading this article will follow, because of the needless use of cat.
Just do grep {flags} “pattern” file_name
Some other features of grep should have been included:
-n to show line number for where the pattern matches
-B, –before-context=NUM print NUM lines of leading context
-A, –after-context=NUM print NUM lines of trailing context
which very useful for when you want understand why a particular match is present in a file.
When searching multiple files to find the one which is missing a pattern
grep -L “pattern” file1 file2 file3
And when trying to find a file or files buried in a directory tree containing a particular string
find {dir_path} -type f -exec grep “some string” {} /dev/null ;
Never forget the saying:
“grep and the whole world greps with you, awk and you awk alone.”
The semi-colon after the /dev/null needs to be escaped with a backslash which did not show when the comment was submitted (probably due to text input sanitizing security feature).
Thanks to Grepper for adding the info about listing lines before and after the match for context, which was one of the things I thought was missing from a nice article for beginners. However, please note that something autocorrected what should have been — (two hyphens) into an en dash before before-context and after-context. In other words, you should use one of the following for three lines of context before a match:
-B 3
–before-context=3
A few more things everyone using grep (or any other *nix command) should know:
You can specify the current directory with a dot, as in: grep -Iir texttosearchfor . (which will search for the specified text in the current directory and subdirectories)
You can save the output of your command to a file; e.g. grep -Iir texttosearchfor . > results.txt (which would do the same as the first example but write the results into a file named results.txt in the current directory)
To put it all together, I ran this command on my ebooks folder:
grep -Iir barrister . -B 3 > results.txt
The result was a text file containing four lines of text from Bleak House:
./Bleak House (Dickens).txt-She could look at it, she said, in the night, especially in the
./Bleak House (Dickens).txt-moonshine. Her room was clean, but very, very bare. I noticed the
./Bleak House (Dickens).txt-scantiest necessaries in the way of furniture; a few old prints from
./Bleak House (Dickens).txt:books, of Chancellors and barristers, wafered against the wall; and
Hope this helps.