Pin Me

Linux Command Line: grep

written by: jlwallen•edited by: J. F. Amprimoz•updated: 7/4/2011

The Linux command line has a host of useful tools. One of those tools stands out as one of the most powerful. Grep is a handy way to search for lines matching a pattern. This tool allows you to find the proverbial needle in the haystack. In this Linux Command Line article you will learn how to grep.

  • slide 1 of 5

    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).

  • slide 2 of 5

    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.

  • slide 3 of 5

    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

  • slide 4 of 5

    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

  • slide 5 of 5

    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.