Small Mosaic


Categories:

books
career
codinghorrors
comics
events
geekstuff
justdont
languages
languages/bash
linkshot
magazines
meta
misctech
movies
nottech
operatingsystems
operatingsystems/linux
operatingsystems/linux/debian
operatingsystems/solaris
paranoidadmin
perl
python
ruby
security
security/apache
security/tools
serversmells
sites
specifications
sysadmin
tools
tools/commandline
tools/firefox
tools/gui
tools/network
tools/online
tools/online/greasemonkey
unixdaemon

Archives:

October 20085
September 20084
August 200812
July 20089
April 20084
March 20081
February 20081
January 200815
August 20072
June 20079
May 20076
April 20078
March 200731
February 20073
January 200721
December 20061
November 20064
October 20066
September 200632
August 200617
July 200614
June 20069
May 200613
March 200611
February 200616
January 200611
December 20051
November 20056
October 200519
September 200525
August 200516
July 200516
June 200513
May 20052
April 200519
March 200531
February 200520
January 200531
December 200421
November 200430
October 200432
September 200418
August 20047
July 200414
June 20045

Sat, 15 Jul 2006

Don't do This With 'find' - part 1
Hell is other peoples code. -- Not quite Sartre.

I don't mind using other peoples code. I'll even submit patches if I find a problem, but discovering the same mistakes in almost half-a-dozen projects is enough to drive me insane. Here are three common red flags involving the find command and how to get around them:

If you want to be portable, don't use GNU/Linuxisms.

Compare these two commands -

find -name "*.log" # not portable
find . -name "*.log" # works everywhere

The first one works with GNU find so it's fine for (almost) all Linux distributions. It doesn't work on older Solaris, HPUX or AIX machines and saves one dot. Lesson: specify your paths.

Know what happens when you match nothing.

The first example (shown below) works fine as long as it matches. If the find doesn't return anything you get a syntax error.

find . -name "*.tmp" | xargs rm # pray for a .tmp file
find . -name "*.tmp" | xargs -r rm

The second example exits silently if there is nothing for it to do. As noted by Dave Cantrell, the -r option is a GNUism so be careful where you use it. You can also get around this by using find options -exec foo instead of xargs. But that comes with its own baggage. Lesson: Plan for errors.

If you're using GNU/Linuxisms then use them properly.

The GNU utilities add a number of powerful and convenient options. If you've decided that your script only cares about Linux use them and save a lot of hassle.

find . -ls | awk '{ printf("%s %s %d\n", $NF, $5, $7) }' # horrible
find . -printf "%p %u %s\n" # better

The first example is (possibly) more portable but it's horrible. And not something you want to see in an init script. The second one is tied to newer versions of find but is much clearer to read, once you've looked up the escapes using man find. Lesson: Know your options.

Like this post? - Digg Me! | Add to del.icio.us! | reddit this!

Posted: 2006/07/15 13:36 | /codinghorrors | Permanent link to this entry | This entry + same date


books career codinghorrors events geekstuff justdont languages/bash linkshot magazines meta misctech movies nottech operatingsystems/linux operatingsystems/linux/debian operatingsystems/solaris perl python ruby security security/apache security/tools serversmells sites specifications sysadmin tools/commandline tools/firefox tools/gui tools/network tools/online tools/online/greasemonkey unixdaemon

Copyright © 2000-2005 Dean Wilson XML feed logo