by collin seaton on Thu Nov 06, 2008 7:38 pm
[quote="alexa bolas"]Micheal, I really appreciate your response but I am super dense here. The command does not make sense to me. I have no idea what is being redirected; why the grep is used; what is in the meta characters and how are they being used. Help!![/quote]
Hi Alexa-
Let's step through Michael's answer one step at a time:
[code]find ~ 2>/dev/null | grep tr[ie][ca][kt][/code]
So find searches for filenames. The ~ tells it to start at your home directory (try cd-ing into a directory other than your home and typing cd ~ you'll find yourself back in your home directory). At this point we're using find to search starting from your home directory.
The next part, 2>/dev/null redirects stderr (identified with 2) into /dev/null (also known as the bit bucket). This results in all errors dumping into /dev/null as opposed to showing on the screen.
The pipe ( | ) redirects stdout from one command as stdin of the next, so in this case find is going to list all the files in your home directory or deeper in the tree, and list those out. By piping the find command's results into grep, you can now give grep arguments defining what you're searching for.
grep is essentially a regular expression tool- it searches for patterns. Let's break the metacharacters down one by one now.
tr - tells grep to look for files starting with the characters 'tr'
[] tells grep to match only one of the characters inside to the next available character so...
[ie] - tells grep that the next character after 'tr' should be either an 'i' or an 'e'
[ca] - tells grep that the next character after tr[i (or) e] should be either a 'c' or an 'a'
[kt] - tells grep that the next characters after tr[i (or) e][c (or) a] should be either a 'k' or a 't'
grep steps through everything from find's stdout and tries to match it to the pattern specified. In the example here, it ultimately matches to files named 'trick' or 'treat'. I don't think it's necessarily a perfect solution, however, as grep would also match a files named 'trict', 'triak', 'triat', and on and on.
I hope that helps!
-collin