Understanding Linux Commands: The grep command

Page content

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:


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.

  1. Linux Command Line: Introduction
  2. Linux Command Line: ls
  3. Linux Command Line: cd
  4. Linux Command Line: mkdir
  5. Linux Command Line: df
  6. Linux Command Line: ln
  7. Linux Command Line: top
  8. Linux Command Line: mount/umount
  9. Linux Command Line: Cron/Crontab
  10. Linux Command Line: chmod
  11. Linux Command Line: wget
  12. Linux Command Line: cat
  13. Linux Command Line: grep
  14. Linux Command Line: dd
  15. Linux Command Line: sudo
  16. Linux Command Line: startx
  17. Linux Command Line: adduser
  18. Linux Command Line: at
  19. Linux Command Line: aterm
  20. Linux Command Line: nano
  21. Linux Command Line: hostname