Greg
Greg

Reputation: 1803

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.

Upvotes: 3

Views: 130

Answers (1)

devnull
devnull

Reputation: 123448

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' '{}'" \;

Upvotes: 4

Related Questions