Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Say I have a directory named "foo". This folder is full of other directories. How can I delete all the empty directories in one command?

share|improve this question
1  
You can try find . -empty -delete, to delete empty directories recursing down from the current directory, however, this will also delete files that are empty. This is a feature of find that you may not want. –  Bill Oct 30 '11 at 3:04
    
For what I am trying to do right now this will work great. –  justingrif Oct 30 '11 at 3:07
1  
No problem. Note that there is an extra option, which I have posted as a solution to your question, that selects directories only. –  Bill Oct 30 '11 at 3:14

3 Answers 3

up vote 31 down vote accepted

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.

share|improve this answer
    
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
1  
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
1  
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
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
share|improve this answer
    
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

You can take advantage of the rmdir command's refusal to delete non-empty directories, and thye 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

rm -rf will delete all the files in the directory (and its subdirectories, and ....) AND all the directories and everything.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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