Tuesday, 14 June 2005

Favourite UNIX Tool

The UNIX philosophy goes like: 'have a lot of utilities, each of them should do one task but should do it extremely well.' Then, through pipes and other built-in mechanism, it is extremely easy to combine them to produce extremely powerful commands in very few lines.

Having said this, every UNIX user or administrator has is favourite tool. Some only swear by sed or awk, others couldn't do without ps or netstat. It all depends on what task you have to perform more often. My tool of choice is find. Maybe this is because I am not very organised or because, as a Java programmer, I am used to complex directory hierarchies where files can get lost. Anyway, quite often I end up working on one of the Sun Solaris boxes we have at work rather than my windows laptop because I need to do something Windows just cannot do. 9 times out of 10, it involves find.

A typical example occurred today when I needed to find all files in a complex directory hierarchy. Simple shall we say. Except that said directory hierarchy was a working copy of part of our CVS repository. So I needed to prune out all CVS related files. How do you do that with Windows? I don't know. It might be possible but it is not simple. On the Sun workstation, it took one line:

find . -name CVS -prune -o -type f -print

This line can look a bit esoteric but is fairly straightforward when you go through the options one by one:

is the name of the find command
. (dot)
means start searching from the current directory downwards in the hierarchy
-name CVS
find all file with name 'CVS', in effect all CVS specialised directories
prune out all files that matched so far, i.e. discard all CVS directories
logical or with the following filters, i.e. apply the following filters on the set of files left after pruning
-type f
only keep files that are real files, as opposed to directories, soft links, etc.
print the result on screen, usually optional as it is the default behaviour

It took all of 30 seconds and I had the results on screen. That is what UNIX power tools are about. So, what's your favourite UNIX tool?

No comments: