As you might expect, grep accepts several useful options that are discussed in the table below:
Let's look at a few common examples of how to use grep.
Suppose you changed your email address and wanted to find all the web pages in a given directory that reference the old address so that you could modify them. In the following example, we search for email@example.com and find two files we need to modify. Can you imagine how long it would take to search each file by hand!!!
|Notice that in the above case, two files contained the old email address. But how do we know where in the file, the mailto reference is? Well we can easily get the line number using the -n option. In the following example we find that firstname.lastname@example.org is found on the seventh line of edge.html. A quick use of the "head" utility proves the point.|
|Now watch what happens when we check case insensitively! We find that there is actually another occurrence of the old email address, but it uses all capital letters instead of all lower case letters. It is often useful to use the -i option since UNIX is very fussy with capitalization|
|Another useful way to use grep is to use the output from ls -l to review site permission settings. Consider these examples that search a directory for HTML files that have been set to be world editable and for directories.|
Yet another useful application of grep is to look at your entry in the passwd file. For example, this is an easy way to find out what shell you are using.