find . -type d -execdir du -sh '{}' ';' | grep -E "[0-9]+K" | sed 's/^[0-9\.]\+K[\t ]\+//' | tr "\n" "\0" | xargs -0 rm -rf

Remove all directories less than 1 MB in size in or below current directory


0
By: jdunn
2009-02-20 19:44:49

These Might Interest You

What Others Think

This command is dangerous because it can blindly remove directories which exist at the level it is run. Example: DirA |--DirB (2 MB) |--DirC (3 MB) |--DirB (1 KB) |--DirD (3 MB) If you run this command in DirA, it will delete the FIRST DirB, and not the DirC/DirB directory. This is due to the way that the find command reports the path information. It shows only the directory found, relative to it's current search position. So when it is in DirC, it reports DirB as "./DirB". Then, when the 'rm' command is run, it is relative to the current directory (DirA) and removes "./DirB" but NOT "./DirC/DirB". I have since changed to the following command: du | awk '{if($1<1028)print;}' | cut -d $'\t' -f 2- | tr "\n" "\0" | xargs -0 rm -rf There may be a more efficient way of doing that, but I'm still learning (aren't we all). I used 1028 as a value because my system uses 4KB for a directory so blank directories are 4KB + 1024KB = 1028. This may work differently on systems depending on variations of awk, and other commands. Always try this without the last command (xargs ...) so that nothing is removed right away.
i814u2 · 151 weeks and 5 days ago
This command is dangerous because it can blindly remove directories which exist at the level it is run. Example: DirA |--DirB (2 MB) |--DirC (3 MB) |--DirB (1 KB) |--DirD (3 MB) If you run this command in DirA, it will delete the FIRST DirB, and not the DirC/DirB directory. This is due to the way that the find command reports the path information. It shows only the directory found, relative to it's current search position. So when it is in DirC, it reports DirB as "./DirB". Then, when the 'rm' command is run, it is relative to the current directory (DirA) and removes "./DirB" but NOT "./DirC/DirB". I have since changed to the following command: du | awk '{if($1<1028)print;}' | cut -d $'\t' -f 2- | tr "\n" "\0" | xargs -0 rm -rf There may be a more efficient way of doing that, but I'm still learning (aren't we all). I used 1028 as a value because my system uses 4KB for a directory so blank directories are 4KB + 1024KB = 1028. This may work differently on systems depending on variations of awk, and other commands. Always try this without the last command (xargs ...) so that nothing is removed right away.
i814u2 · 151 weeks and 5 days ago
Sorry for the double-post, I received an error on the first one and simply resubmitted. Also I see that it stripped extra spaces. The directory example should look more like this: DirA |--DirB (2 MB) |--DirC (3 MB) .....|--DirB (1 KB) .....|--DirD (3 MB)
i814u2 · 151 weeks and 5 days ago
And, perhaps this is different on different systems, but I felt the need to comment just in case it saves someone. And remember to always test before actually removing, especially when system differences can mean that apps produce slightly different results.
i814u2 · 151 weeks and 5 days ago
works well. Just tested on some folders that were less than 2MB. Is there a way to set a date limit, so only folders older than 7 days get deleted, but newer folders are left alone?
sunk818 · 71 weeks and 4 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: