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
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
Our latest tutorials delivered straight to your inbox