Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question
up vote 1 down vote accepted

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.

share|improve this answer
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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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