If you use Linux long enough, you are going to wind up getting to know (and using) the command line. And if you use the command line long enough, you are going to find yourself using the grep tool. Grep is one of the most useful linux utilities in that it will search WITHIN a text file for a string of characters. Grep is such a useful tool that it is often used in shell scripts and much, much more.
In this article I am going to introduce you to the grep command and how it is used. Once you know this command, your life with the Linux command line will be made much easier.
Although grep is a very versatile command to know, it does have its limitations. One of the biggest limitations it has is that it can not search within binary files. That's all fine, because your typical binary file would be searchable with the application that created said binary file.
Grep also has a line limit of 2048 lines. This means if your file is beyond that, grep will stop searching at the line limit.
You are in luck. Grep is a tool that comes pre-installed with all Linux distributions. So no installation is required.
The basic usage of grep is:
grep [OPTIONS] [PATTERN] file
Their are numerous options to use with the Grep command. The more helpful of these options are:
-E Interpret the PATTERN section as an extended regular expression.
-P Interpret the PATTERN section as a Perl regular expression.
-e Use PATTERN as the pattern. If searching a single PATTERN you do not need the -e option. But this option allows you to search for multiple search patterns.
-f Obtain patterns from a file, one per line.
-i Ignore case.
-c Suppress normal output and only output count of matching lines.
--color Display the matched strings in color.
-n Display the line number associated with the matching entry.
Let's take a look at a very basic example first. You want to search the file /etc/test.conf for the string input. The grep command for this would be:
grep input /etc/test.conf
Now, let's say that test.conf is a larger file and you need to know the line number the string input is on. For this the command would look like:
grep -n input /etc/test.conf
The above output would then include the line number associated with each matching entry. Now, let's say you wanted to search the same file for both input and output and you want to know the line numbers associated with each. For this the command would look like:
grep -n -e input -e output /etc/test.conf
But what if you only want to know how many times the string input is found in the file /etc/test.conf. For this you could use the command like so:
grep -c input /etc/test.conf
Grep is one of those commands that you will use time and again, in many, various ways. I always tell new users that, once you reach the point where you begin using the command line, grep is one of the first commands you should master.Advertisement
Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.
We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats or subscription fees.
If you like our content, and would like to help, please consider making a contribution:
Ghacks is a technology news blog that was founded in 2005 by Martin Brinkmann. It has since then become one of the most popular tech news sites on the Internet with five authors and regular contributions from freelance writers.