Search code examples
shelljpegoptim

jpegoptim save shell last run and check files or folders after this date


I have thousands of images which I optimize weekly by running it over a cronjob. My Problem is that it search also optimized images which lower the CPU. How could I save the last scan / optimization and began to optimize Files and Folders after this Date?

My Code

find . -name '*.jpg' | xargs jpegoptim --strip-all
find . -name '*.jpg' | xargs jpegoptim --all-progressive
chmod -R 777 *

Solution

  • The easy thing to do is to touch a file to track the most recent processing time, and to tell find to limit itself to content newer than that file.

    To keep the prior semantics, where we were running two separate passes, completing all invocations of jpegoptim in one mode before going on to the other:

    #!/bin/bash
    
    extra_args=( )
    [[ -e last-scan ]] && extra_args=( -newer last-scan )
    
    find . -name '*.jpg' "${extra_args[@]}" -exec jpegoptim --strip-all '{}' +
    find . -name '*.jpg' "${extra_args[@]}" -exec jpegoptim --all-progressive '{}' + 
    touch last-scan
    

    As an alternative, consider:

    #!/bin/bash
    
    extra_args=( )
    [[ -e last-scan ]] && extra_args=( -newer last-scan )
    
    find . -name '*.jpg' "${extra_args[@]}" \
      -exec sh -c 'jpegoptim --strip-all "$@"; jpegoptim --all-progressive "$@"' _ '{}' +
    
    touch last-scan
    

    In this latter approach, we're doing only one find pass, and then passing each batch of files to a shell, which is responsible for running jpegoptim in each mode in turn for that batch.


    Finally: if jpegoptim is safe for concurrent usage, you could do the following:

    #!/bin/bash
    
    extra_args=( )
    [[ -e last-scan ]] && extra_args=( -newer last-scan )
    
    find . -name '*.jpg' "${extra_args[@]}" \
      -exec jpegoptim --strip-all '{}' + \
      -exec jpegoptim --all-progressive '{}' + 
    touch last-scan
    

    Here, we have a single find pass directly starting both copies of jpegoptim; the risk here is that if jpegoptim --strip-all and jpegoptim --all-progressive can't safely operate on the same file at the same time, this may behave badly.