I have the following script, though I would like to change it to deleting content once the directory reaches a certain size. What are my options?

find /home/scott/google-drive/CAMMEDIA/* -mtime +1 -exec rm -rf {} \;
  • do you mean date or size?? I would prefere for date to use -mmin instead of -mtime, example delete all files older than 24h -mmin +1440, works better than -mtime. – WiKrIe May 8 '18 at 19:33
  • Thanks, @WiKrle. I was referring to folder size. Thanks for the suggestive change. – Scott May 9 '18 at 15:13
  • Hi Scott, so just add the -sizeflag with your needed parameters, I would also add the -typeto search only for directories and not for files if you want that. So as an example search for all directories bigger than 10K you need this find /place/to/search -type d -size +10Kand then you can continue however you like. – WiKrIe May 14 '18 at 9:42
  • @WiKrle, in this scenario, what directory is -size for? I'm looking to watch the total size of CAMMEDIA. – Scott May 15 '18 at 18:24

This is pseudo code. You will have to set the folder size and what you want to remove yourself (you were not clear on what you wanted to be removed so I'm running the same thing you were)

DIR_SIZE=$(du /home/scott/google-drive/CAMMEDIA/*)
    find /home/scott/google-drive/CAMMEDIA/* -mtime +1 -exec rm -rf {} \;
  • Thank you @Jeff. I assume this is in KB? – Scott May 9 '18 at 15:07
  • @Scott yes, from the man page: "Display values are in units of the first available SIZE from --block-size, and the DU_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables. Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set)". You can get a human-readable format by using du -h but I think you will want the raw number for your code. If this answer works for you please upvote and accept it. – Jeff May 9 '18 at 15:33
  • 1
    I'm receiving an error message in running this now...line 3: [: missing `]'. BTW, I changed 'than' to 'then'. – Scott May 13 '18 at 15:45
  • can you address the error I'm receiving? – Scott May 16 '18 at 22:23
  • Try putting spaces between the variable and the bracket. if [ $MAXSIZE -gt $DIR_SIZE ] – Jeff May 22 '18 at 4:14

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.