I need to do some folder clean-up, and I'd like to remove directories that have 10 or fewer files in them. I tried looking at GNU's find
, but there wasn't anything related to the number of files in a directory. What commands should I use to accomplish that?
Ubuntu – How to delete folders that have n or fewer files in them
deletedirectoryfilesfind
Best Answer
One way would be to use
find
's-exec
action to execute a custom test of the number of files.One could use a second
find
command along withwc
to find and count files within each directory, but probably a better option would be to shell globbing to slurp the filenames into an array, and then return a logical value indicating whether the size of the array is less than the threshold i.e.Putting it all together, we should be able to list all subdirectories with fewer than 10 files (including sub-subdirectories) using
(you can adjust the number
10
after thebash {}
for different thresholds - the${2:-10}
parameter expansion makes it default to 10 files if no second argument is given). For example, giventhen
If that appears to be doing the right thing you can actually remove them by adding
rm -rf
but please be very careful with this - remember there is no 'undo'(The
xargs
could be eliminated by using another-exec
action to runrm
more directly, but the formulation above makes it easy to generalize - print, stat, remove or whatever.)Note this will act recursively i.e. a directory that initially has more than
n
files including subdirectories may be removed as a result of some of those subdirectories themselves getting removed asfind
backs up the directory tree - the current directory is explicitly protected from possible deletion by the! -name '.'
test.If you don't need it to act recursively, you can simply loop over directories and perform the same file count and test logic e.g. to remove all first-level directories containing fewer than 10 files in the current directory