34

Is it possible to find the directories only having size large than x MB. Suppose, I want to find all the directories only whose size is large than 1000MB with only 1 maxdepth under /home, how to find it in ?

2

2 Answers 2

80

If I'm interpreting your question right, I think this might be what you want:

cd /home
du -sm * | awk '$1 > 1000'

This will show all directories in /home that contain more than 1000MB. If your version of du doesn't support -m, you can use du -sk and adjust the awk bit to look for more than 1,000,000KB instead...

6

According to the manpage, the -k option is POSIX compliant but the -m option is not.

So the following is more portable (i.e. if you're on BSD, it will still work) but essentially does the same:

du -sk * | awk -v m=1000 '$1 > 1024*m'

Just set the awk variable m to the number of megabytes you would like as your cut-off.

I found this very useful for moving a batch of files, hence posting here for others' benefit.

To extend this to move all files meeting your criteria to another directory, you can adjust the awk command to print just the bit you need for the move (excluding the size) and then loop:

# moving all directories meeting the size criteria to another location:
for d in $(du -sk * | awk -v m=1000 '$1 > 1024*m {print $2}')
do 
    mv $d $DESTINATION
done
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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