Possible Duplicate:
How to remove all empty directories in a subtree?
I create directories very often, scattered over my home directory, and I find it very hard to locate and delete them.
I want any alias/function/script to find/locate and delete all empty directories in my home directory.
2 Answers 2
The find
command is the primary tool for recursive file system operations.
Use the -type d
expression to tell find
you're interested in finding directories only (and not plain files). The GNU version of find
supports the -empty
test, so
$ find . -type d -empty -print
will print all empty directories below your current directory.
Use find ~ -...
or find "$HOME" -...
to base the search on your home directory (if it isn't your current directory).
After you've verified that this is selecting the correct directories, use -delete
to delete all matches:
$ find . -type d -empty -delete
-
9Good solution, but it should be noted that not all version of find have
-empty
.jordanm– jordanm2012年08月25日 21:49:04 +00:00Commented Aug 25, 2012 at 21:49 -
@Baldrick Doesn't looks from
home
directory if I run it form~/Desktop
.Santosh Kumar– Santosh Kumar2012年08月25日 21:58:31 +00:00Commented Aug 25, 2012 at 21:58 -
@Santosh: The command as it is, is meant to be run from your home directory (that's why I added
~$
in the beginning). If you want to run it regardless of your working directory, use"$HOME"
instead of.
as jordanm suggested in his answer.Baldrick– Baldrick2012年08月25日 22:09:37 +00:00Commented Aug 25, 2012 at 22:09 -
4Consider changing the second example to
.
instead of~
. If somebody copy-pastes this without noticing and only checks the output of the first this can have bad consequences.Florian Wendelborn– Florian Wendelborn2016年06月03日 01:28:07 +00:00Commented Jun 3, 2016 at 1:28 -
7I would add
-mindepth 1
here, to prevent from deleting the starting directory itself, if it would be empty. It's not really probable case for$HOME
but if you would use this on any other directory..Greg Dubicki– Greg Dubicki2016年08月20日 12:46:59 +00:00Commented Aug 20, 2016 at 12:46
You can call rmdir
on every directory, since rmdir
will only delete a directory if it is empty:
find "$HOME" -type d -exec rmdir {} + 2>/dev/null
If you also want to print the directories being removed, you will need to check if they are empty:
find "$HOME" -type d -exec bash -c 'shopt -s nullglob; shopt -s dotglob; files=("1ドル"/*); [[ ${files[@]} ]] || rmdir -v "1ドル"' -- {} \;
Here is a pure bash example (version 4 or higher):
shopt -s globstar
for dir in **/; do
files=("$dir"/*)
[[ ${files[@]} ]] || rmdir -v "$dir"
done
-
And what if I only want to find/locate and not delete?Santosh Kumar– Santosh Kumar2012年08月25日 21:44:00 +00:00Commented Aug 25, 2012 at 21:44
-
@Santosh - In either of the last two examples, just change
rmdir -v
toecho
.jordanm– jordanm2012年08月25日 21:47:33 +00:00Commented Aug 25, 2012 at 21:47 -
10BusyBox
find
doesn't have-empty
, so the-exec
trick is helpful. But rather than sending errors off to /dev/null, it's better to tell rmdir to suppress them, e.g.find . -type d -depth -exec rmdir -p --ignore-fail-on-non-empty {} \;
Robert Calhoun– Robert Calhoun2015年04月01日 19:19:31 +00:00Commented Apr 1, 2015 at 19:19 -
1These commands have terrible performance since they create a new process for every directory found. When using that approach, you really should at least use something like
find ... | xargs -n 100 rmdir
to delete them in batches. Even then, it won't work properly because it will fail to remove early directories that will later become empty because of later empty directories' removal. Thus you'd need to execute it several times.jlh– jlh2015年11月13日 13:25:05 +00:00Commented Nov 13, 2015 at 13:25 -
5@jlh No, the first command will only execute
rmdir
once. That's the difference between using+
and\;
for terminating a command infind
.jordanm– jordanm2015年11月13日 14:10:19 +00:00Commented Nov 13, 2015 at 14:10
WORK=$(mktemp -d)
orcd $(mktemp -d)
. Of course don't put important files that you need to preserve in those directories. But most likely your system is already setup to automagically make those files disappear after a while./z/
directory on start-up and do all my temporary work there.