Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 08-10-2003, 11:16 PM   #1
Registered: May 2003
Distribution: RedHat 9
Posts: 32

Rep: Reputation: 15
how do I remove all empty directories?

I'm trying to figure out how to recursivly remove all of the empty directories on my computer.

I have many cases where a directory contains another directory which is empty, so I'm trying to figure out a way to delete all of the empty child folders, and then delete any of the empty parent folders after that.

I've tried looking at the man pages for rmdir, and toying around with grep, but I can't seem to figure this one out.
Old 08-11-2003, 12:18 AM   #2
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
If it's just a few directories that you can delete the parent by hand, just do rm -rf dirname where dirname is the directory name. Otherwise, this might be something you want to script. There might be a fancy find way of doing it, but I can't think of a way to do it with find that would be any easier than doing it in a script.
Old 08-11-2003, 12:47 AM   #3
Registered: Jul 2003
Location: Miami, Florida, USA
Distribution: Debian
Posts: 848
Blog Entries: 1

Rep: Reputation: 47
Be careful! Some empty directories are needed for file locking and several other things. Some of your programs may not work correct if you remove certain subdirectories such as the ones in /proc or /var.

As root you can try the following command:

du -h / > d1.txt ; sort -g d1.txt > d2.txt
less d2.txt

The command above will run du (disk usage), reporting all files and directories and their size. It will store the output to d1.txt. Afterward you can sort d1.txt onto d2.txt in numerical order that way you can look at all the files and folders that have 0 size with the less command.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
finding empty directories with bash Iriel Solaris / OpenSolaris 10 02-18-2016 04:59 AM
how to search for and delete empty directories? BrianK Programming 4 06-03-2005 08:24 AM
ProFTPD - deleting non-empty directories? orange400 Linux - Software 1 05-28-2004 04:36 AM
Removing non-empty directories brunnopessoa Linux - Newbie 8 11-13-2003 08:56 PM
finding empty directories... PokerFace Linux - General 2 10-16-2002 09:23 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:46 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration