how can I recursively delete empty directories in my home directory?
Solution 1
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
Solution 2
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
Related videos on Youtube
Santosh Kumar
Updated on September 18, 2022Comments
-
Santosh Kumar over 1 year
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.
-
Admin almost 12 yearsThis does not answer your question, but could solve the underlying problem. I often use the construct:
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.
-
-
Santosh Kumar almost 12 yearsAnd what if I only want to find/locate and not delete?
-
jordanm almost 12 years@Santosh - In either of the last two examples, just change
rmdir -v
toecho
. -
jordanm almost 12 yearsGood solution, but it should be noted that not all version of find have
-empty
. -
Santosh Kumar almost 12 years@Baldrick Doesn't looks from
home
directory if I run it form~/Desktop
. -
Baldrick almost 12 years@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 almost 12 years@jordanm: You're quit right, I edited my answer accordingly.
-
Santosh Kumar almost 12 years@Baldrick Just a last task. I want to escape my
~/project
directory. -
Baldrick almost 12 years@Santosh: You can use something like this:
find "$HOME" -type d \! -path "$HOME/project/*" -empty -delete
. The!
operator (or the escaped\!
) returns false if the following expression is true. In effect, it excludes the~/project
directory. Note that if~/project
is empty, it will be deleted. -
Vishnu Dasu about 9 yearsBusyBox
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 {} \;
-
jlh over 8 yearsThese 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. -
jordanm over 8 years@jlh No, the first command will only execute
rmdir
once. That's the difference between using+
and\;
for terminating a command infind
. -
Greg Dubicki almost 8 yearsI 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.. -
KingsInnerSoul over 7 yearsI had to use
$ find . -type d -empty -delete
-
Franklin Yu over 6 years@KingsInnerSoul Wasn't that already mentioned in answer?
-
KingsInnerSoul over 6 yearsMaybe you are new to SE, but you can see that the my comment was added on October, and the answer was edited in November to include it! There is no need to spam everyone over a year after it was answered!
-
Charaf over 5 yearsIt seems that it doesn't see hidden files. How to do it ?
-
Hotschke over 5 yearsBSD version under macOS 10.12 has
-empty
.