简体   繁体   中英

Bash Script to find, process and rename files?

I am trying to put together a script which will run through all the files on my server (under various subdirectories) , look for .jpeg files and run them through a translator which converts them to non progressive jpgs.

I have:

 find /home/disk2/ -type f -iname "*.jpg" 

Which finds all the files.

Then if it finds for example 1.jpg, I need to run:

/usr/bin/jpegtrans /file location/1.jpg > /file location/1.jpg.temp

The jpegtrans app converts the file to a temp file which 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 single files but i need to do it for 100's on my server.

Use find with -exec :

find /home/disk2/ -type f -iname "*.jpg" -exec sh -c "/usr/bin/jpegtrans {} > {}.temp; mv -f {}.temp {}" \;

EDIT: For handling spaces in filenames, say:

find /home/disk2/ -type f -iname "*.jpg" -exec sh -c "/usr/bin/jpegtrans '{}' > '{}.temp'; mv -f '{}.temp' '{}'" \;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM