Tuesday, August 11, 2009

Nifty trick with gnu find

Common problem - you have a bunch of files on disk that you need to manage. Lets say you need to scour through /home/${user}/files and set all files to permission 0640 and all directories to 0750. Let's also assume that you have a lot of files to manage.

Performance is important, so you naturally you choose to use 'findutils' to perform the action. But how do you get the most out of find? Try this nifty little tidbit:

find "${dir}" \(   \
\( -type d -exec chmod 0750 {} \; ) \
-o \
\( -type f -exec chmod 0640 {} \; \) \
\)

This one-liner has the magic "-o" operator (or) in find - but, with the ability to group your commands, you're not only limited to:
\( -iname '*.pm' -o -iname '*.pl' \)

but you can also combine test/action sequences.

Now, instead of running boring.sh, which takes an incredible amount of time - you can run nifty.sh, cutting your find time down exponentially!

Labels: , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home