The command to do something recursively for the find directory structure:
find . -name "*.jpg" -exec bash -c 'file="{}"; cp "$file" "${file%%.jpg}1.jpg"' \;
Using -exec instead of for i in $(find ...) will process the names of files that contain a space. Of course, there is another quote received; if the file name contains " , file="{}" will expand to file="name containing "quote characters"" , which will obviously be violated ( file will become name containing quote and it will try to execute the characters command).
If you have such file names or you can print each file name separated by null characters (which are not allowed in the file names) using -print0 and use while read -d $'\0' i to cycle through the results with a null limit:
find . -name "*.jpg" -print0 | \ (while read -d $'\0' i; do cp "$i" "${i%%.jpg}1.jpg"; done)
As with any complex team, it is a good idea to test it without doing anything to make sure that it expands to something reasonable before you run it. The best way to do this is to add a valid command using echo , so instead of running it, you will see the commands that will be executed:
find . -name "*.jpg" -print0 | \ (while read -d $'\0' i; do echo cp "$i" "${i%%.jpg}1.jpg"; done)
Once you have looked into it and the results look good, delete echo and run it again to run it for real.
source share