What command can I execute to check the directories inside a folder and if the total size of the directory is smaller than 30MB, delete it? I played around with the find command trying to move files based in the size but it didn't work properly.

I was using (for 1mb):

find . -size +102400k -exec mv DIRECTORY
find . -size -102400k -exec mv DIRECTORY

If we're talking about directory size, I'm not sure find is going to help. du is going to have to be involved at some point.

Here's something I have written to find near directories under 30M:

du --max-depth 1 | awk -v q='"' '$1 < 30000000 && $2 != "." {sub(/^[0-9\t ]+/, "", $0); print q $0 q}'

You can then chain that into xargs rm -rf but I would test it very seriously before doing that. You might want to move the directories (with mv) instead of straight-up deleting them.

  • Hi Oli, I've tested this command and it just listed all directories ... sorry, I'm not really used with linux commands so maybe I had to do something else that I'm not aware. – Daniel Gomes Mar 29 '12 at 11:03
  • @DanielGomes You need to stick | xargs rm -rf on the end of that command... But as I say, please test the first part thoroughly first. – Oli Mar 29 '12 at 12:12

Find doesn't know the total size of directories. So that approach won't work.

You could find all directories, and exec a script that uses du to see if they are smaller than 1MB.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.