Friday, October 23, 2009

What to Do with Files after You Find Them













What to Do with Files after You Find Them

After you find the file or files you are looking for, you can do more than just look at their names. If you want, you can tell the find command to do something with every file it finds.


Rather than end the find command with the -print option, you can use the -exec option. It tells find to execute a UNIX shell command every time it finds a file. The following command, for example, tells the find command to look for files with names beginning with report:



find . -name "report*" -exec lpr {} ";"

Every time the command finds that type of file, it runs the lpr program and substitutes the name of the file for the {}. (You type two curly braces, which was some nerd’s idea of a convenient placeholder.) The semicolon indicates the end of the UNIX shell command. (You have to put quotation marks around the semicolon, or else the shell hijacks it and thinks that you want to begin a new shell command. If that didn’t make sense, take our word for it and remember to put quotation marks around the semicolon when you use find.) Every time find finds a filename beginning with report, this command prints the file it finds.


You can use almost any UNIX command with the -exec option, so, after you find your files, you can print, move, erase, or copy them as a group. A slight variation is to use -ok rather than -exec. The -ok option does the same thing except that, before it executes each command, find prints the command it’s about to run, followed by a question mark, and waits for you to agree to run the command. Press the y key if you want to do it, and press the n key if you want it to skip that particular command.



�Warning��By using find and -exec rm, you can delete many unwanted files in a hurry. If you make the smallest mistake, however, you can delete many important and useful files equally as quickly. We don’t recommend that you use find and rm together. If you insist, however, please use -ok to limit the damage.











No comments:

Post a Comment