Having trouble with find rename jpg command

Hi,

I have a large series of directories and subdirectories with many jpgs in them. I need to do two things:

  1. Create a copy of each jpg found within it's own subdirectory
  2. Rename this copied jpg such that apple.jpg becomes apple_m.jpg

I have tried to run the following commands in order:

//this creates a copy of every jpg found and appends '_m' to the end of it

find . -name '*.jpg' -execdir cp {} {}_m \;

//now again find everything with 'jpg_m' at the end and rename it as filename_m.jpg

find . -name '*.jpg_m' -execdir rename -v 's/\.jpg_m/_m\.jpg/' {} \;

However the 2nd command does'nt seem to work, can someone please explain what I'm doing wrong?

Thanks,
Adi

Hello,

Could you please try the following code.

a=`find . -name "*.jpg"`
set -A array $a
 
for i in ${array[@]}
do
file_name=`basename $i`
echo $file_name
new_name=`echo $file_name"_m"`
cp "$file_name" "$new_name"
done
 

Hope this may help you.

NOTE: kindly use cp -p to copy the files in same permissions.

Thanks,
R. Singh