Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
add comment

2 Answers 2

up vote 0 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
add comment

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
add comment

Your Answer

 
discard

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.