The 'grep' command.

The grep command is generally used when you need to search a particular word into files (ex: an IP adress in some huge log files).

Supose you have two files:
"file1.txt" that contains the sentence: bad dog
"file2.txt" that contains the sentence: good dog

To search in all the files that have the name begining with the leters "file"
and have the extension txt for the word "dog" you will write :

grep dog file*.txt

You should have the following output :

file1.txt bad dog
file2.txt good dog

The word "dog" apears in all the files that are begining with the letters "file"

If you search for the word "bad" and write :

grep bad file*.txt

You should have the following output :

file1.txt bad dog

The word "bad" apears only in file1.txt

Now, let's supose that the files do not have the same extension. Supose we have "file1.txt" and "file2.doc". To make a search in all the files, that have the name begining with "file" and have different extensions, for the word "dog" you write :

grep "dog" file*.*

You should have the following output :

file1.txt bad dog
file2.doc good dog

The word "dog" apears in all the files that are begining with the letters
"file", no matter the extensions of the files.

Supose you are loged in as root, and you are in the home folder (/home). Now, you want to see, if the user bob has files in his home folder (/home/bob) that contain the word "dog". From the home folder you write :

grep dog bob/*.*

This will do a search in all the files that are in the folder /bob for the word dog. If the user bob keeps his files in a subfolder (/bob/secret) the search will not return nothing, and to search that folder you have to write :

grep dog bob/secret/*.*

The important thing to remember is the charcter "*". This character (wild card) will stand for a string. You can use is to do a big search : grep dog *.* this will search in all the files, no matter the name or extension, or you can use it to do a specific search, as above.

Also keep in mind, that grep is a very complex search tool. It has many options that you can use to improve your search. This file was written to give a basic understanding. For more information read the manual : man grep