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

旧城冷巷雨未停 提交于 2020-03-25 12:30:32

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/60677799/how-to-perform-a-cronjob-only-when-a-file-is-greater-than-a-certain-size

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!