Saturday, May 09, 2009

Effective find

Having been a long time user of find in unix, though I only recently stumbled into some of its more finer usages. Once example would be the usage of the -exec argument. I've spent countless hours piping find to a file, doing a global replace to execute the desired command and running it as a shell script; that I was simply thrilled to find that I could do it all in one single step :) (I only wish I had a little more patience reading the man pages that I would have stumbled into it sooner). Take for example removing those pesky ws_ftp.log files that litter your directories when someone decides to use ws_ftp to ftp files instead of filezilla. Here is how you can get rid them in a single command
find . -type f -name 'WS_FTP.log' -exec rm -f {} \;
Another cool trick to get rid of the Permission denied messages that otherwise fill up the screen when doing global searches is to redirect stderr to /dev/null.
find / -type f -name 'serverindex.xml' -exec grep -l '8100' {} \; 2>/dev/null
One quick note about using cygwin the \; that should terminate the exec portion of the find should actually be \ ; in cygwin or according to the wikipedia even a plain ; will do

The unix paste command

One relatively unknown unix command is paste which I had occasion to use recently. I had to twiddle some data in between 2 fixed length files and the machine did not have vim so that I could use visual block selection and too big to quickly scp to my workstation. With some help from google I discovered paste, which in combination with cut reduced the task to something as simple as this
cut -c 1-32 file1.txt > left
cut -c 48-  file1.txt > right
cut -c 64-108 file2.txt > middle
paste -d '\0' left middle right # do not use the default delimiter instead use null
Finally I managed to refine the entire process into a series of cut and pastes in sequence.