PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, August 2, 2022

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

 August 02, 2022     cp, cron, vps     No comments   

Issue

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


Answered By - Romeo Ninov
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing