Tuesday, December 2, 2008

Using find Utility Effectively in *NIX

Sometimes you need to find files with certain criterion as fast as possible. For example in task where you need to strip out files bigger than a predefined size. Say 50KB. This can be done efficiently using the find utility in *NIX with the following command:
 
darmawan@opunaga:AP $ find rootfs/bin -type f -size +50k -exec ls -lah \{} \;

The command above assumes you are searching in rootfs/bin directory. Note the backslashes in the command, they are used to escape the subsequent character(s) after them in order to prevent the shell from interpreting them.

Another rather often used combination is find, grep and awk. The following example shows how to find certain string and ignoring paths with .svn directory in it.
 
darmawan@opunaga:AP $ find rootfs -type f -exec grep -H -n 'iwcontrol' \{} \; | awk '$0 !~ /\.svn/ {print}'

I think long commands like above should be scripted for ease of use in the long-run.
Post a Comment

1 comment:

Dila said...

Hi...bro!!!!
what an amazing posts of yours......