58

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

118

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. – justingrif Oct 30 '11 at 3:24
  • Hey Bill, I tried to post another similiar question and it wouldn't let me because it said it was a dupe, so I was hoping you might be able to answer me here. Say I wanted to do the same thing as above but remove all directories whether they are empty or not. – justingrif Nov 3 '11 at 19:39
  • 3
    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 '11 at 23:24
  • What if there were some files in said directory I wanted to keep. So, I wanted to delete all directories in /foo/bar/ but keep any files that might be in there. – justingrif Nov 4 '11 at 3:23
  • 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 '11 at 4:55
11

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
  • 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 1:35
  • I often do this at $HOME. With an evil grin. – Alois Mahdal Dec 16 '15 at 1:37
  • Because it will report failure if some of the directories are not empty. – the_drow Dec 16 '15 at 7:19
  • is this faster than the find method? Has anyone compared the two? – Xerus Apr 17 '18 at 10:58
1
find . -type d -empty -delete -maxdepth 1

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

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)
  • too complicated for simply questions :( – nurulhudamustaqim Feb 17 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 – Sergiy Kolodyazhnyy Feb 17 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.