What On Earth is Grep
As a tool "grep" is just one of those commands that once you begin to use you just can't do without, nothing in it name suggests what this command does. Grep Wikipedia documents grep as a command line text search utility originally written for Unix. The name is taken from the first letters in global / regular expression / print, a series of instructions for the ed text editor. The "grep" command searches a file, files or standard input globally through the lines for matching a given regular expression, and prints them to the program's standard output.
So in english using the given format of
command searchname filename.txt
We can, using the above formula search any regular text file for any letter, number or word (now why didn't they say that in the first place). The above is the syntax used is not the actual command itself, to search a file for a given expression we need use the actual command which would be something like the following.
The command syntax followed is command = grep, searchname = choice, filename.txt = menuui.txt together when this command is run from a terminal or console window, provided you have downloaded the file attachment on this page, this is only an example document the file itself can be any file consisting of text.
Two lines here are returned meaning that two instances of the word "choice" have been found, obviously the word being searched for can be anything you may want and additionally the name of the file being searched can also be changed provided the file is a text or script file and the command is run in the same directory that the file is located.
Now lets use the same command but use "-i" to ignore the case of the characters being searched unfortunately the word "choice" does not have any uppercase characters within the text supplied so we need to change the word to look for within this script. So we will change searchname to "menu and use the -i option" as this text word appears in both uppercase and lowercase.
This command runs exactly the same as the first the only difference is the choice of word being searched and we have used the "-i" option to ignore case and flag all instances of word being searched for.
As you can see from the above example four lines have been returned and the case letter "M" in the word "menu" has been ignored, just what we wanted it has also found the word part of "menu" in "menus" if this is what you where looking for then grep has done it's job.
The option -w when used in the above command limits the search to an exact match of the word being searched for.
Expanding the search to look at more than one file or directory of files we need to incorporate the path of these files into the syntax and use a wildcard "*"
command searchname pathname/*.txt
This does little to explain why grep is such a powerful tool as we have only used standard text files or scripts with limited content. However when you realise that many problems encountered are recorded in logs it makes sense to look through the logs this is largely independent of Operating System types now by that we mean it matters not which OS you choose all use logs which can be searched. Logs can be a valuable resource of both information and security.
Your logs are just as accessible as the ones provided as an example attachment on this page and usually located in the directory at least for Linux OS, "/var/log" all these logs should be readable unless sensitive security information is contained in which case you will need root permissions to view them.
Say for example we had a problem maybe with installation or running of a program we look in /var/log and immediately confronted with a vast array of different logs monitoring who knows what where the hell do we start manual search of each file could take hours. We know the application that is causing the problem how, because when this program is run it fails to connect to the service. The application we have a problem with is call "wengophone" whilst it runs it does not connect to the service provider so let us examine the logs and see what it is that is displayed.
First the commands we need to use:
You will notice we now have three options we have not used before "*" this is a wildcard and effectively means look at all files in this directory, the "|"is addressed as "PIPE" and redirects or channels data collated to yet another command in this case called "less" for what of a better description this is a viewer like "cat" or "more" the advantage using less is that we are able to examine the contents piped to it by the "|" symbol either backward or forward using the up and down arrow keys, this can be very useful in pinpointing a problem. So what this does is to look through all the files defined by "*" the wildcard and process each instance of the word being search for to the display.
As can been seen from the logs it seems the installation might not have been as complete as we where lead to believe as a point of information this warrants further investigation. This readily demonstrates some of the untapped power behind grep and indeed that of command console this is well worth your investment in time and effort so go one give it a try.