Say I have a directory named foo/
. This folder includes subdirectories. How can I delete all the empty directories in one command?
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.
-
I think this is the easiest way to do it so I am going to go ahead and mark this as answered. Oct 30, 2011 at 3:24
-
4The way you would remove directories, regardless of whether or not they are empty, type the following command:
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.– BillNov 3, 2011 at 23:24 -
2It is possible to do this by using a pipe to feed the
stdout
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.– BillNov 4, 2011 at 4:55 -
1@joeybaruch On MacOS almost every directory you "visited" with Finder.app contains a
.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.– gerlosFeb 22, 2018 at 13:09 -
1With the
-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
. Feb 22, 2018 at 13:49
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.
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
-
This is not suitable for scripting since it will exit with a non-zero status code but it works.– the_drowDec 16, 2013 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.) Dec 16, 2015 at 1:35 -
-
Because it will report failure if some of the directories are not empty.– the_drowDec 16, 2015 at 7:19
-
find . -type d -empty -delete -maxdepth 1
For if you only want to delete the direct subdirectories of foo/
.
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)
-
-
@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 Feb 17, 2019 at 6:24