in ,

How-to Use the Most Popular Command in Unix - 'grep'

Everyone needs to know about grep if they are using Unix

'grep' is by far the most popular command that exists in Unix. Though some may argue about that, but once you begin using grep, it would always be present in all your complex commands that you think of executing at the shell prompt. grep stands for 'global regular expression printer'. Which makes no sense to most.. In sensible words, grep extracts those lines from a given text that match the conditions set by the user.

Basically, grep lets you enter a pattern of text and then it searches for this pattern within then text that you provide it. It would return all the lines that have the given pattern in them. grep can be used in two ways: either on its own or along with pipes.

Using grep on its own

grep '12.00' /home/david/backup/log.txt

This command basically shows how you can use grep to extract lines containing a particular string from a text file (text files need not necessarily end in .txt).

The above command searches for the string "12.00" in the text file specified in the command, and displays all the lines that have this string in them. The command could be used to find out all the backups that took place at 12.00 (in case you have a log.txt file in that directory with a list of all the timings for the backups that you have made).

grep -v '12.00' /home/david/backup/log.txt

The above command would now show you all the lines in the text file except those that have the string 12.00 in them.

grep -l 'delay' /code/*.c

The above command searches for those files that end with a '.c' (within the /code directory) and in which the text 'delay' is present. It only returns the names of these files and not the lines where it found the string.

grep -w '\<bay' * $ grep -w 'watch\>' *

The above commands search for text in a more refined way. The first command searches for those lines where any word in that line begins with the letters 'bay' and the second command searches for those lines where any word in that line ends with the letter 'watch'

Using grep with pipes

ls -l | grep rwxrwxrwx

As you must know, 'ls -l' displays the directory listing for any directory. The grep rwxrwxrwx part of the command extracts only those lines which display the files having their read, write, execute permissions set for user, group and others also. Thus, instead of getting a listing of all the files in the directory, you would only see those files that have their r,w,x permissions set for all everybody.

The output of grep can also be piped to another program as follows:

du | grep 'mp3' | more

You should be able to figure out what the above command does...

[/hell]grep '^#' /home/david/script1 | more[/shell]

The above command would display those lines (from the file /home/david/script1) that begin with a '#'. The term '^#' means that # should be present as the first character on a line. The more part of the command should be known to you. If not, more basically displays the output a page at a time incase the output exceeds one page.

grep -v '^[0-9]' /home/david/backup/log.txt | more

This command searches for lines having any of the numbers from 0-9 in them as the first character on the line. It then prints all the lines except the ones it found initially.

Important: it's necessary to enclose patterns (as used in the above 2 commands) in single quotes so that the shell understands it correctly. Otherwise, the shell may interpret it in another method.

Some extra options for grep

  • -v: Reverses the normal behaviour of the grep command - Instead of selecting lines, it rejects the lines that match the given criteria.
  • -c: It suppresses the normal output and only prints the total count of matching lines instead of the actual lines.
  • -i: Ignores the case of the text when matching the given pattern.
  • -w: Checks if the given pattern is a word by itself and not a part of another word. Thus if you search for 'bay' and the word 'baywatch' is present in a file, the particular line containing that word would not be returned in the result.
  • -l: Only gives the names of the files in which the given pattern was found.
  • -r: Checks for the given pattern , recursively within the directory that you specify after the -r option

I hope this tutorial helps you get started with grep. grep is definitely one of the tools that gives Linux the advantage over other Operating Systems. Using grep effectively along with other tools gives the user a lot of power in Unix.

This article was originally written by Kiran Pai.

Written by Matt

I've been running web hosting servers and building websites for myself using Wordpress for 14 years. My network of sites get a TON of traffic and I use various models to monetize them - mainly display ads and affiliate marketing. Along the way I've picked up hundreds of tips and tricks that I think would be useful for anyone looking to make a website and here is where I'm going to share them. Find me on Twitter too.

Leave a Reply

Your email address will not be published. Required fields are marked *

How-to Find the Size of a Directory and Free Disk Space

How and When to Use the dd Command