73

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

0

5 Answers 5

154

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.

8
  • I think this is the easiest way to do it so I am going to go ahead and mark this as answered.
    – justingrif
    Oct 30, 2011 at 3:24
  • 4
    The 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.
    – Bill
    Nov 3, 2011 at 23:24
  • 2
    It is possible to do this by using a pipe to feed the stdout of one command (e.g. find) into the stdin 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 the man 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 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 with find . -name '.DS_Store' -delete and then try again to remove the empty directories with the suggested command.
    – gerlos
    Feb 22, 2018 at 13:09
  • 1
    With the -depth option find can even match and delete directories that only become empty because all of their (empty) subdirectories were deleted. Try it out with mkdir -p a/b/c/d && find a -depth -type d -empty -print -delete. Feb 22, 2018 at 13:49
14

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.

8
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
5
  • This is not suitable for scripting since it will exit with a non-zero status code but it works.
    – the_drow
    Dec 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
  • I often do this at $HOME. With an evil grin. Dec 16, 2015 at 1:37
  • Because it will report failure if some of the directories are not empty.
    – the_drow
    Dec 16, 2015 at 7:19
  • is this faster than the find method? Has anyone compared the two?
    – xeruf
    Apr 17, 2018 at 10:58
3
find . -type d -empty -delete -maxdepth 1

For if you only want to delete the direct subdirectories of foo/.

0
0

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 iteration r is set to current folder that we're accessing,s contains list of directories within r, and f will contain list of files in that folder. Of course if f and s are empty, we know that r 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 perform os.rmdir() on each item in empty 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)
2
  • too complicated for simply questions :( Feb 17, 2019 at 6:21
  • @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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .