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.

I would like to find (later delete) all music folders that are nearly empty (Banshee deletes only the music files but not the other files in there).

I tried with:

find -type d -size -500k \;

But it shows folders that contain bigger files too.

find -type d -size -500k -exec du {} \;

shows the correct size.

How to modify the find cmd to only show folders that are smaller then N?

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

One command I use, as long as you don't need to pipe this straight into another script, is

du . | sort -rn

This would put the smallest folders at the bottom of the printed list along with their sizes. It would take a bit more work to filter out the ones that are larger.

share|improve this answer
    
easy and elegant - like it :) –  Paradiesstaub Nov 8 '11 at 1:43
    
If you do have a script like awk that can filter by the first field, the final command you can toss in to get rid of the number on the start is | cut -f 2- –  ekthomson Nov 8 '11 at 2:02
    
Incase you've got too many lines on your screen for you to browse in your terminall you can add ">>du.log" directly on the end of that command to write the output of that command to that file. Really handy trick for when the screen just blurts out too much! Open and view with your favourite text editor. (may need sudo depending on your location, or use >>/home/YOURUSER/du.log to save it to home. To reiterate: "du . | sort -rn >>output.log" –  Laice Nov 8 '11 at 5:27
    
I'm not a n00b ;) –  Paradiesstaub Nov 10 '11 at 18:57
add comment

If you simply want to delete folders which contain under N bytes, the following one liner will work:

du | awk '$1 <= 500 { print "rm -rf \"" substr($0, length($1)+2) "\""}' | sh

What does this do? Consecutively:

  • du prints sizes of directories along with their paths
  • $1 <= 500 is a condition that tests if the first column is under 500 (if it's smaller than 500 bytes)
  • print in awk prints rm -rf "/path/to/small/dir"
  • | sh pipes it into sh so it can be executed

You could also do it using xargs instead of piping it into sh, but I'm used to this way.

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.