Search code examples
cronvpscp

How to perform a cronjob only when a file is greater than a certain size?


The following script (credit to Romeo Ninov) selects the most recent directory and performs a cp operation:

dir=$(ls -tr1 /var/lib/test|tail -1)
cd /var/lib/test/$dir && cp *.zip /home/bobby/

Please see: How can I use a cronjob when another program makes the commands in the cronjob fail? for the previous question.

I would like to modify this so that the cp only happens if the .zip file is larger than a defined byte size e.g. 28,000 bytes. If the .zip file is smaller, nothing is copied.

As before, this would happen in /var/lib/test/**** (where **** goes from 0000 to FFFF and increments every day).

Thanks!


Solution

  • You can rewrite your script on this way:

    dir=$(ls -tr1 /var/lib/test|tail -1)
    cd /var/lib/test/$dir
    for i in *.zip
     do
     if [ "$(stat --printf="%s" $i)" -gt 28000 ] 
      then cp $i /home/bobby
     fi
    done