Anyone who’s administered a Linux server for a certain amount of time is familiar with the “grep” command. With Linux’s philosophy of “Everything is a file”, grep becomes crucial to traversing log files, configuration files, checking for certain errors in output files, and getting the number of matches. In this tutorial, I’ll show you a few advanced uses of grep that can still be pretty useful. These are not hard to find in the man pages, but they’re just an extension of grep’s default functionality.
Table of Contents
Searching for Multiple Words at the Same Time
Normally, the usage for grep goes something like this:
grep 'pattern' filename
Where ‘pattern’ is any given pattern or regular expression to match. However, let’s say you have an error log file and are performing routine maintenance. You want to check the log files for the existence of some keywords that span a variety of sources. Normally, you’d have to use a separate grep expression for each of these phrases.
However, we can use regular expressions easily with the “-E” parameter. To start with, we have a file called “animal-count” with the following contents:
Let’s say we want to search this file for these two phrases:
We can use the following grep expression:
grep -Ew 'sparrow|dogs' animal-count
There are two flags:
- -E means that we’re using extended regular expressions
- -w means that we only want to match whole words
To make it even easier, we can just replace “grep -E” with “egrep”. So the above command becomes:
egrep -w 'sparrow|dogs' animal-count
And here’s the output:
We can match as many words as we need to that way. If you want to remove the “words” requirement and instead just want a straight up string match, then you can drop the “w” parameter.
Searching an Entire Directory for a Match and Showing the File Name
We normally think of grep searching either a file or a specific output from another command. However, we can also use it to search an entire directory of files at the same time.
Let’s say for example, that you have a bunch of log files scattered all around the system. After all, many packages have their own locations for log files. To make things easy for you, you create a new folder containing symbolic links to all the various log files that are of interest to you.
Now you can search all of them together with grep using the “-R” command. The capital “R” tells grep to include symbolic links. The small “r” ignores symbolic links not found on the command line by default.
For example, in the following example, we search the current directory for a string:
grep -r sparrow .
The dot (.) at the end indicates the current directory. We get the following output:
You can see below, that it searches recursively as well with another pattern match:
This is very useful for searching a bunch of files at the same time. The output also shows you the name of the file containing the matching strings! I personally like this solution with “-R” for searching through a set of symbolic links at once, to save time.
Counting the Number of Matching Lines
Sometimes you want to know how many times a certain keyword has appeared in grep. For example, if you’re searching the website files for a certain spider, you want a count of the number of lines containing a specific user agent.
To do that, we simply add in the “-c” parameter. Keep in mind that this removes the regular output of grep and it will no longer show you individual matches. For example, without the “-c” parameter:
grep rr animal-count
We get a simple list of occurrences. With “-c” however:
grep -c rr animal-count
And we get a count of the number of lines:
As you can see, it no longer displays each occurrence.
Grep is an extremely powerful tool, and its functionality has only increased over the years. We can create complicated regular expressions, and even create an expression to format the output of a file to make it easier to read. These examples of advanced grep usage will give you a taste of what’s possible, and will hopefully encourage you to delve into the manual yourself!
If you are one of our fully managed web hosting customers, you don’t have to come up with complex grep commands yourself, you can simply ask our system administrators to help you with grep commands. They are available 24/7 and will take care of your request immediately.
If you liked this post on advanced grep commands, please share it with your friends via social media networks. If you have any questions regarding this tutorial, please write a comment below. Thanks!