78

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

hsnee
  • 145
0xnuminous
  • 11,280

6 Answers6

163

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.

Bill
  • 1,944
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.

dessert
  • 40,956
waltinator
  • 37,856
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
evilsoup
  • 4,625
4
find . -type d -empty -delete -maxdepth 1

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

0
rm -d */

Remove all directories that are empty in this folder. Probably not foolproof. Append 2>/dev/nullto ignore errors (non-empty folders)

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)