tags:

views:

82

answers:

3

I have a lot of files whose names end with '_100.jpg'. They spread in nested folder / sub-folders. Now I want a trick to recursively copy and rename all of them to have a suffix of '_crop.jpg'. Unfortunately I'm not familiar with bash scripting so don't know the exact way to do this thing. I googled and tried the 'find' command with the '-exec' para but with no luck.

Plz help me. Thanks.

+2  A: 

if you have bash 4

shopt -s globstar
for file in **/*_100.jpg; do 
  echo mv "$file" "${file/_100.jpg/_crop.jpg}"
one

or using find

find . -type f -iname "*_100.jpg" | while read -r FILE
do
  echo  mv "${FILE}" "${FILE/_100.jpg/_crop.jpg}"
done
ghostdog74
missing quotes, doesn't work with filenames containing newlines. Use `find ... -exec` or `find ... -print0 | xargs -0` instead.
Philipp
yes, missing quotes and fixed. So no need to use xargs. Accepted answer doesn't have quotes as well..
ghostdog74
+5  A: 
  find bar -iname "*_100.jpg" -printf 'mv %p %p\n' \
    | sed 's/_100\.jpg$/_crop\.jpg/' \
    | while read l; do eval $l; done
akira
Excellent ! That's the easiest solution I've ever read on that topic ! When I read it I just wondered why I didn't find it by myself ! Maybe b/c I'm not smart enough :)
PierrOz
A: 

This uses a Perl script that you may have already on your system. It's sometimes called prename instead of rename:

find /dir/to/start -type f -iname "*_100.jpg" -exec rename 's/_100/_crop' {} \;

You can make the regexes more robust if you need to protect filenames that have "_100" repeated or in parts of the name you don't want changed.

Dennis Williamson