Bash scripts for finding, processing, and renaming files?… here is a solution to the problem.
Bash scripts for finding, processing, and renaming files?
I have:
find /home/disk2/ -type f -iname "*.jpg"
Locate all files.
Then if it finds e.g. 1.jpg, I need to run:
/usr/bin/jpegtrans /file location/1.jpg > /file location/1.jpg.temp
The jpegTrans application converts the file into a temporary file that needs to replace the original file.
So then I need to delete the original and rename 1.jpg.temp to 1.jpg
rm /file location/1.jpg
mv /file location/1.jpg.temp /file location/1.jpg
I can easily do this for
a single file, but I need to do this for 100 files on my server.
Solution
Use find
and -exec
:
find /home/disk2/ -type f -iname "*.jpg" -exec sh -c "/usr/bin/jpegtrans {} > {}.temp; mv -f {}.temp {}" \;
EDIT: For Handling spaces in file names, say:
find /home/disk2/ -type f -iname "*.jpg" -exec sh -c "/usr/bin/jpegtrans '{}' > '{}.temp'; mv -f '{}.temp' '{}'" \;