TimCS.Co.Uk Linux Linux Guides Basic guide to the command grep
Basic guide to the command grep
What is grep ?

Grep is a very useful command that can search and find text. It can be used either by itself or and by parsing an output from another command into it.

Using grep by itself

To use grep by itself , it needs two parameters, what to look for and where to look. For instances, to look for the word the in any file ending in .txt the following would suffice :

grep the *.txt
If there is more than one file found ending in .txt with the word the then the display would show as follows:

filename1.txt:text found where the word the is
filename2.txt:text found where the word the is

As long as the file(s) that are being used to find text, are text based themselves, then grep will scan them for the specified word.

Using grep with other commands

Grep can be used to narrow down results from other commands. For instance, to run the ps to list all running processes could produce a very long list, however this can be made to be shorter by using grep. To shorten the ps command by looking for specific process, type the following to look for processes that are named mbd :

ps -ef | grep mbd

Issuing this command will find processes that have mbd as part of their name.

End of this guide

This was just a taster to what grep can do, it gave a fairly good idea on how versatile grep can be. It also has other functionality to it such has counting lines in a text files as well.
 

Powered by Joomla!. Valid XHTML and CSS. Credits go to Gareth Flowers for his help with this site.