Understanding Linux Commands: The grep command
What is grep?
The name “grep” is an acronym for Global Regular Expression Print which is a series of instructions for the ed text editor. The grep command searches files or standard input for lines that match a regular expression and prints them to stdout (standard output - the screen). Generally speaking the regular expression is a string of characters (in simple terms - a word).
What you can do with grep
The most useful way to use grep is to search for strings in a text file. This is very handy when you are trying to edit a large configuration file (such at httpd.conf) and need to locate a specific line. Say for instance you need to edit the MaxKeepAlive entry in your httpd.conf. Depending on your installation, that line could be anywhere. By using grep you can locate the precise line number for that entry.
Basic usage
The grep command is quite simple to use. Basic usage is as follows:
grep SEARCH_STRING FILENAME
Where SEARCH_STRING is what you are searching for and FILENAME is the file you will search. Sticking with the httpd.conf example above we’ll run a search for MaxKeepAlive in httpd.conf. Assuming you are within the directory containing your httpd.conf file you would issue the command:
grep MaxKeepAlive httpd.conf
and you would be returned something like this:
# MaxKeepAliveRequests: The maximum number of requests to allow
MaxKeepAliveRequests 100
More helpful usage
The above example does tell you that the line you are searching for exists in the file you are searching - but no more. For our example you want to find out where this line is. To do this you would add the n argument which prints the line number the search string exists on. So our new command looks like:
grep -n MaxKeepAlive httpd.conf
and the results would look like:
80:# MaxKeepAliveRequests: The maximum number of requests to allow
84:MaxKeepAliveRequests 100
Now you know the entry exists AND you know the exact location(s) of the entry.
Let’s say, however, you are unsure of the capitalization of MaxKeepAlive. You can use the i argument to ignore capitalization. So now issuing the command:
grep -ni maxkeepalive httpd.conf
would give you the same output:
80:# MaxKeepAliveRequests: The maximum number of requests to allow
84:MaxKeepAliveRequests 100
Final Thoughts
The grep command will make your command line experience much more efficient. Say hello to grep and say goodbye to the time-consuming line-by-line searching of lengthy configuration files.
This post is part of the series: Linux Command Line
If you ever plan on doing any administration on a Linux machine, you would be well served to get to know the command line interface. In this Bright Hub series you will be introduced to various concepts surrounding one of the most powerful admin tools around.
- Linux Command Line: Introduction
- Linux Command Line: ls
- Linux Command Line: cd
- Linux Command Line: mkdir
- Linux Command Line: df
- Linux Command Line: ln
- Linux Command Line: top
- Linux Command Line: mount/umount
- Linux Command Line: Cron/Crontab
- Linux Command Line: chmod
- Linux Command Line: wget
- Linux Command Line: cat
- Linux Command Line: grep
- Linux Command Line: dd
- Linux Command Line: sudo
- Linux Command Line: startx
- Linux Command Line: adduser
- Linux Command Line: at
- Linux Command Line: aterm
- Linux Command Line: nano
- Linux Command Line: hostname