Say I have a directory named foo/
. This folder includes subdirectories. How can I delete all the empty directories in one command?

- 145

- 11,060
5 Answers
Try this command:
find . -empty -type d -delete
The find
command is used to search for files/directories matching a particular search criteria from the specified path, in this case the current directory (hence the .
).
The -empty
option holds true for any file and directory that is empty.
The -type d
option holds true for the file type specified; in this case d
stands for the file type directory.
The -delete
option is the action to perform, and holds true for all files found in the search.

- 1,854
You can take advantage of the rmdir
command's refusal to delete non-empty directories, and the find -depth
option to traverse the directory tree bottom-up:
find . -depth -exec rmdir {} \;
(and ignore the errors), or append 2>/dev/null
to really ignore them.
The -depth
option to find
starts finding at the bottom of the directory tree.
rm -rf
will delete all the files in the directory (and its subdirectories, and ....) AND all the directories and everything.

- 39,982

- 36,399
rmdir *
Will delete all empty directories. It'll throw up an error for every non-empty directory and file, to stop those errors from cluttering your terminal, use
rmdir * 2> /dev/null

- 4,485
-
This is not suitable for scripting since it will exit with a non-zero status code but it works. – the_drow Dec 16 '13 at 16:34
-
@the_drow how does it make unsuitable? By the way you can also use
rmdir * 2>/dev/null || true
. (The find(1) way is better for scripting but for other reason: because it expresses better what you want to do.) – Alois Mahdal Dec 16 '15 at 01:35 -
-
Because it will report failure if some of the directories are not empty. – the_drow Dec 16 '15 at 07:19
-
find . -type d -empty -delete -maxdepth 1
For if you only want to delete the direct subdirectories of foo/
.

- 141
Python approach
$ tree
.
├── empty_dir1
├── empty_dir2
├── subdir1
│ ├── file1.abc
│ └── file2.abc
└── subdir2
├── file1.abc
└── file2.abc
4 directories, 4 files
$ python -c 'import os;empty=[r for r,s,f in os.walk(".") if not f and not s and r != "." ];map(lambda x: os.rmdir(x),empty)'
$ tree
.
├── subdir1
│ ├── file1.abc
│ └── file2.abc
└── subdir2
├── file1.abc
└── file2.abc
This works like so:
- we use
os.walk()
function to walk recursively the directory tree. On each iterationr
is set to current folder that we're accessing,s
contains list of directories withinr
, andf
will contain list of files in that folder. Of course iff
ands
are empty, we know thatr
is empty. - first list-comprehension allows us to create
empty
, the list of all directories that are empty, based on the evaluation stated above. - second function,
map()
is used to performos.rmdir()
on each item inempty
list. List comprehension could be used as well as alternative.
As a script this would be as so:
#!/usr/bin/env python
import os
empty=[]
for r,s,f in os.walk("."):
if not f and not s and r != ".":
empty.append(r)
for i in empty:
os.rmdir(i)

- 105,154
- 20
- 279
- 497
-
-
@nurulhudamustaqim Depends on point of view. For a lot of Linux users who are used to Python this is actually very modest :) And besides its variety. Modern system administration is not limited to bash or
/bin/sh
only and Python is actually more elegant language than those two – Sergiy Kolodyazhnyy Feb 17 '19 at 06:24
rm -rf <directory-name>
. This will remove the directory, along with all its contents including files and subdirectories. The-r
option means delete recursively, while the-f
command means don't prompt before deleting. If you want to be prompted before deleting a directory/file, however, replace the-f
option with the-i
option. – Bill Nov 03 '11 at 23:24stdout
of one command (e.g.find
) into thestdin
of the other (e.g.rm
), however, be very careful since it may wipe out or delete files/directories that you don't want to delete! For further information on how to do this, see theman
pages for each of these commands. To be safe, always test such things in a temporary directory before trying out on the real thing. – Bill Nov 04 '11 at 04:55.DS_Store
hidden file, that usually stores your sorting/viewing preferences for that directory. Other apps may add other hidden files (e.g. Adobe Bridge may add a.BridgeLabelsAndRatings
file), so perhaps those directories aren't really empty. Anyways, you can remove the.DS_Store
file withfind . -name '.DS_Store' -delete
and then try again to remove the empty directories with the suggested command. – gerlos Feb 22 '18 at 13:09-depth
optionfind
can even match and delete directories that only become empty because all of their (empty) subdirectories were deleted. Try it out withmkdir -p a/b/c/d && find a -depth -type d -empty -print -delete
. – David Foerster Feb 22 '18 at 13:49