New Linux users are often bewildered by the strange names of some of the most popular commands. Of course, they all have some history behind them, and even if the implementation has completely changed over the years, the names remain. One of the most prevalent and ubiquitous commands is called "grep". New users are most likely introduced to it while searching for advice on the forums and someone asks them to use a grep command to show them the output.
A particularity of the grep command is that it's often used to filter or modify the output of other commands using a "pipe". Results are sent to grep using the symbol "|" on the same line. This is easily one of the most common uses. Another typical scenario is searching a file for a specific pattern. In this tutorial, I'll show you a couple of the most common uses of grep and how to learn more.
Searching Folders for Specific Files and Directories
I grew up with MSDOS and always thought that the "dir" command was the most intuitive in the world. For example, if I wanted to search my current folder for all files and directories containing the word "documents", I would use this:
And here's the output on my emulated DOS console:
It's simple, and it gives a nice output. However, we can achieve the same thing in Linux using grep. Here's a command searching the current "bin" directory for all files and folders containing the term "sql":
ls | grep sql
And this is the output:
Personally, I prefer the presentation of grep with its minimalistic output and highlighting of the matching string. But let's break down what happens in the command to understand how grep works.
The first command simply lists the contents of the current directory with "ls". But instead of printing and sending the output to the screen, we use the pipe symbol (|) to send it to grep. Grep simple accepts this and applies the pattern specified - in this case "sql". Then it just gives us the lines where that pattern has been found and we see the results in the screenshot.
This ability of grep to accept the output from other commands is truly amazing. We can use it with any command that sends text to the screen and which we want to filter in some way or the other. We can use it to filter yum's output, and much, much more. The uses of grep are only limited by your imagination.
Searching Within Files
The second most common usage of grep is searching within files for something specific. For example, if you want to quickly check the memory limit of your php.ini file, you can do so without opening it in the "vi" editor by issuing this command:
grep memory_limit /etc/php.ini
Since I know that the memory limit is stored in the variable "memory_limit", I search for that pattern and grip simply prints the lines that contain it. Here's the output:
It's that simple! In one single command I get the information I'm looking for without needing to open the file and search manually. Once you get used to the idea of grep, you'll find yourself taking all kinds of shortcuts.
Printing Line Numbers
The "-n" option of grep also prints the line numbers of the lines containing the matching pattern. For example, using the same command above if I use the "-n" option, I get this:
It's line number 405. Unfortunately, there's no easy way to print a specific line number using grep. For that, it's best to use "sed" instead. For example, to print the specific line number 405 in php.ini, I would use the following sed command:
sed -n 405p /etc/php.ini
There are tons of other options in grep and I haven't even scratched the surface of regular expressions. You can see the complete GNU grep documentation here. But even the basic functionality outlined in this article will help speed up your day to day operations and debugging using one of the most powerful and ubiquitous tools in all of Linux.