find - Executing the Easy Way
find does much more than search for files, as its name suggests. It is a versatile tool that confimrs to the Unix philosophy. more from less. This article will look at using find to execute commands and make life easy for the administrator','<p>Administrators are stuck with a thankless job that involves tedious and repeatitive tasks. No one appreciates an administrator as much as they deserve. If a system works well people think its supposed to work well and if it fails, guess who takes the rap.. yes you would be right, the admin.

 

I reckon the developers and designers of UNIX were empaths, particularly to administrators. There are so many command line utilities that work wonders if properly combined with one another. UNIX has developed over the years to make tedious administration tasks easy and automated. find is one such command. looking at all the switches to find is beyond the scope of this article. we shall look at executing commands, yes you heard me right, using find.

Well lets get to it then.

say you have a large chunk of files in a directory tree and you want to copy them into a single directory without preserving the directory tree.... hassle you say... find i say. find does it in one line using the -exec switch.

kavit@trishul:/tmp> find . -type f -exec cp -p {} /path/to/dest_dir \;

this command will copy all files from every sub directory in the directory and copy them to the destination directory and preserve permissions.

Or Say you want to delete all *.gif files  from within a directory structure, find helps yet again.

kavit@trishul:/tmp> find . -name "*.gif" -exec \rm -rf {} \;

or you just want to move them to a different directory

kavit@trishul:/tmp> find . -name "*.gif" -exec \mv {} /path/to/dest_dir \;

it even lets you delete files belonging to a particular user

root@trishul:/home> find . -uid 1001 -name "*.avi" -exec \rm -rf {} \; 

find does much more. Read the man pages for further information about find and its features.
This just goes to show the power and ease of use UNIX/Linux provides to the administrator.

 
[ Back ]