I have an image folder that serves content, it contains jpeg, jpg and gif files. More files are added automatically all the time.

What I intend on doing is adding the following commands to crontab (with the appropriate file path of course)

mogrify -quality 80% "*.jpg"

mogrify -quality 80% "*.jpeg"

mogrify -quality 80% "*.gif"

I'd like to respectfully ask if anyone knows if these commands would end up processing files multiple times, so when a file is converted to 80%, then next time cron runs it reduces it further etc...

Does ImageMagick ensure it doesn't process the same file twice, and if not how do I deal with this given I can't rename the files.

Any help from the community would be greatly appreciated.

thank you.

Edited: Thanks to user: Sven for the answer below. This is the script I am now using that appears to be working:

Thank you for this. It appears that mogrify can write in place, preliminarily.

I will use this script:


watchedFolder=[insert path here]

while [ true ]
fileName=$(inotifywait -q -e create --format "%f" "$watchedFolder")
sleep 1s
mogrify -quality 80% "$watchedFolder/$fileName"
sleep 2s
echo "Processed " $fileName
sleep 2s
  • 1
    How can you distinguish between a file that has been processed and a new file that has not been processed? Despite its name, ImageMagick doesn't actually perform magic. You must have some way to tell the files apart. Renaming/moving them is the simplest and most effective way to do that. – Michael Hampton Feb 10 at 16:15

IMagick will process any files any time you call it, it has no way of identifying that the file has already been processed. After all, you tell it "process all jpg files", so this is perfectly normal and expected.

There are many ways to circumvent this problem, depending on your specific circumstances.

  • You can write a wrapper around your cron job that will keep a list of files it has already processed and only process new files. This will work in most scenarios but is also the least elegant and most complicated solution.
  • Employ a naming scheme. Rename the small files and use wildcards that exclude the small versions.
  • Alter your workflow. Put the new files in a staging area and write the smaller files into the final destination.
  • Check if mogrify can write in place (as opposed to write to a new file and delete the old one afterwards). If it can, use inotifywatch to learn about new files and process those as they arrive. Writing in place is important in this method, as otherwise inotifywatch would also see a new file when mogrify writes out the reduced version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.