Sunday, July 8, 2012

Renaming large groups of files with find

My normal inclination is to use xargs coupled with find to do something batch-y like renaming groups of files across a whole directory tree, but I've had a lot of trouble getting the syntax perfect for using something like cp with xargs. It turns out that the find at the start of the chain has a -exec argument that allows multiple uses of the found file name no fuss no muss like xargs should. The command is:

find . -name "*.htm" -exec cp -p {} {}.original \;

The key seems to be in the \; at the end, which I guess that I wasn't doing right before.

Here the site that had the info that got me this breakthrough; it's in somebody's comments near the bottom:

http://csm.mech.utah.edu/content/2011/03/01/use-of-the-shell-xargs-command/