Adverts
Tools such as grep and ps are very well documented but as with a lot of the documentation on Linux (and unicies in general) it is nearly inpenetarble to someone that hasn't got a degree in computer science. This page will hopefully give you just a little flavour of grep.
Once you find grep you will, I am sure, not be able to live without it. Essentially all grep does is read input and if its argument matches on the current line it outputs that line. For instance imagine you have a piece of text thus (this is apparently the worlds funniest joke):
Two hunters are out in the woods when one of them collapses. He doesn't seem to be breathing and his eyes are glazed. The other guy whips out his phone and calls the emergency services. He gasps: "My friend is dead! What can I do?" The operator says: "Calm down, I can help. First, let's make sure he's dead." There is a silence, then a shot is heard. Back on the phone, the guy says: "OK, now what?"
If you grepped for the word "The" you would get this back:
Two hunters are out in the woods when one of them collapses. The other guy whips out his phone and calls the emergency services. The operator says: "Calm down, I can help. First, let's make sure he's dead."
From this is is fairly easy to see what is happening. Each line containing "The" has been picked out and show to you. Notice that it didn't return the last line because that contains "the" with a lower case "t".
Using grep is pretty much this easy, it can get complex because the matches use something called regular expressions which allow for very complex matches to be made. Saying that basic usage of grep is as simple as
grep foo *
If you enter this command at a prompt grep will go through every file in the current directory looking for the word "foo". Now this is useful at times but often what you want to do is search only one file, a particular type of file or maybe all subdirectorys. In that case you can use grep expressions of this form.
To search a single file add the file name (bar.out in this case) to the end of the command thus:
grep foo bar.out
To search a particular type of file (all .out files) use:
grep foo *.out
To search all subdirectorys (recurse) use:
grep -R foo
What is rally clever is that grep can be used with just about any command by combining it with a pipe "|" the pipe takes the output of one command and pipes (sends) it into the next command. There is no depth to the number of pipes you can use but after a couple your brain tends to melt and dribble out your ears! Anyway an example of this usage would be to find a program running on the local machine. There are often many tens if not hundreds of processes running on a machine. Going through them all by hand to find information about one is tedious so we will use grep to find them for us. First run just this command:
ps -A
You no doubt saw pages of process information. Just as an exercise lets find all the applications the contain the letter "x". We do that like this
ps -A | grep x
As you can see the list is now greatly reduced. I am sure you will come to think grep is brilliant tool
Another useful combination of commands using grep is this which is a way to search in only a certain type of file in all subdirectories. In this example you have to use the find command as well. The reason for this is because the shell doesn't expand *.out to include subdirectories and therefore they won't get searched even if you use -R:
find . -name '*.out' -exec grep -H search_for_this {} \;