Linux – How to use find to copy all found files to a new name in their same directories

bashcpfindlinuxxargs

I've got a simple command that does almost what I want. The following will locate all files with a suffix of '_compressed.swf' and copy each into its same directory with a '.bak2' appended:

find '../content' -name '*_compressed.swf' -print0 | xargs -0 -I {} cp {} {}.bak2

Results
In: /content/somefile_compressed.swf
Out: /content/somefile_compressed.swf.bak2

However, I need to replace '_compressed.swf' with '_content.swf' I'd like to use find, rather than recursive flag on cp for consistency.

Objective
In: /content/somefile_compressed.swf
Out: /content/somefile_content.swf

Best Answer

This solution is probably the most portable:

find "../content" -name "*_compressed.swf" -exec sh -c 'cp {} `dirname {}`/`basename {} compressed.swf`content.swf' \;

There is also the famous rename.pl script which is distributed with Perl, and the rename command which could have made this a bit easier. These aren't available on all distributions though, these commands are for the most part.

Related Question