You are not authorized to post comments.

Unix tip: Recursively removing empty directories

By Sandra Henry-Stocker  Add a new comment

As you undoubtedly know, the rmdir command will remove an empty directory but, if a directory contains files, it will balk and report "Directory not empty". To remove directories which are in fact empty is then easy. In any file system containing multiple directories, you can issue the "rmdir *" command knowing that only the empty directories will be removed.

To recursively remove empty directories, you could try a mix of the find and rmdir commands. For example, this command would remove numerous directories yet leave all the populated ones:

find . -type d -print | xargs rmdir

If you don't want to see all the "Directory not empty" errors, you can send standard error to /dev/null:

find . -type d -print | xargs rmdir 2>/dev/null

Looking for directories with find and then removing them has one drawback, however, If a directory becomes empty in the process of removing a directory that's inside of it, the parent directory will not be removed because find will detect it before it finds its child directory and it won't yet be empty.

You could run one of the commands shown above multiple times, maybe until no more "Directory not empty" errors appear. Alternatively, you could run a script like this one that uses find to create a list of directories, reverses the list so that child directories appear before their parents and then runs a rmdir against each directory in the list. In this way, all of the empty directories are removed in one pass.

#!/bin/bash

# get directory list
find . -type d -print > dirs$$

# reverse the listing
tail -r dirs$$ > revdirs$$

while read dirname
do
    rmdir $dirname 2>/dev/null
done < revdirs$$

# clean up
rm dirs$$
rm revdirs$$

One of the core commands in this script is the tail -r command. This command reverses the contents of a file or of any input piped to it. In fact, this command is so useful for this operation that you could reduce the script above to this one-liner:

find . -type d | tail -r | xargs rmdir

Or, again, if you don't want to see all the "Directory not empty" complaints, pipe the error output of this string of commands to /dev/null:

find . -type d | tail -r | xargs rmdir 2>/dev/null

As a one-liner, this command lends itself to use as an alias.

$ alias rmEmptyDirs="find . -type d | tail -r | xargs rmdir 2>/dev/null"

If you want to test this alias, create a directory under /tmp and populate it with a series of new directories. The easiest way to do this is to use the mkdir -p command:

cd /tmp/testing
mkdir -p a/b/c/d/e/f/g/h/i/j
mkdir -p d/r/a/g/o/n/f/l/y
mkdir -p d/i/t/c/h
mkdir x
mkdir y
mkdir z

Then touch a file somewhere in the newly created directory structure:

touch a/b/c/myfile

If you then run the rmEmptyDirs alias, you will find that only the a, a/b and a/b/c directories and the file "myfile" remain.

The mkdir -p command, which creates a directory structure by creating all the directories along the requested path that don't already exist, has a partner in rmdir -p that removes all the directories along the way, provided they are empty. This leads to a further simplification of our alias. Now we can reduce it to this:

alias rmEmptyDirs="find . -type d | xargs rmdir -p"

or:

alias rmEmptyDirs="find . -type d | xargs rmdir -p" 2>/dev/null

Using rmdir -p, the list reversing operation is no longer needed. If you don't suppress standard error, you will note that this command will refuse to remove directories that it finds until it reaches one that is empty. At that point, it will remove all of its empty parents and grandparents.

Another simplication is to use the rmdir command's -s option to suppress the error output instead of piping it to /dev/null. Now, we have the ultimately simply command for removing empty directories:

alias rmEmptyDirs="find . -type d | xargs rmdir -ps"

You can't make the process much easier than that! Our original rmdir command with a couple command options only has to be run once and gets rid of all empty directories in its path.

ITworld LIVE

IT Management/StrategyWhite Papers & Webcasts

White Paper

The Cloud: Reinventing Enterprise Collaboration

Collaboration and content sharing are not, of course, new concepts. But cloud computing has changed the nature of collaboration, content sharing, document storage and project management to enable more efficient, faster-acting and cost-effective enterprises. According to a new study by IDG Research, the vast majority of knowledge workers (86%) placed a very high level of importance on collaborating with internal coworkers and external stakeholders, and having access to the most up-to-date corporate information. Read how organizations are realizing massive productivity gains by transitioning their content management solutions to cloud-based models.

White Paper

Empowering Your Mobile Worker

Today's most productive employees are mobile, and your company's IT strategy must be ready to support them with 24/7 access to the business information they need across a range of mobile devices.See how corporations are meeting the many needs of their mobile workers with the help of Box.

White Paper

Market Landscape Report: Online File Sharing and Collaboration in the Enterprise

The trend toward "consumerization" marches onward in IT; more and more end-users are choosing their own hardware plaforms and software applications in lieu of the IT-sanctioned business tools provided by their companies. These end-users are looking to tackle issues like data sharing, portability, and access from multiple intelligent endpoint devices, creating a conundrum for IT as it needs to balance business enablement, ease of access, and collaborative capacity with the need to maintain control and security of information assets. This need for balance is one of the drivers of the fast growing online file sharing and collaboration segment of the SaaS market. This paper examines the market drivers, inhibitors, and top vendors in this segment, including Box, Citrix Sharefile, Dropbox, Egnyte, Nomadesk, Sugarsync, Syncplicity and YouSendIt.

White Paper

Sharing Simplified - Consolidating File-sharing Technologies

Employees need to share content with colleagues within their organization and outside. Yet, ECMs make it hard to share content within a business and impossible between organizations. Read how one company consolidated multiple file sharing technologies to increase productivity and reduce complexity.

White Paper

Content Sharing 2.0: The Road Ahead

A growing number of companies are taking advantage of the natural synergies that exist between cloud-based IT services and content access and sharing. Legacy content management and collaboration systems simply weren't designed to meet the evolving requirements of today's IT and business managers, as well as the needs of content users. Box provides cloud-based content storage, access and collaboration services that require virtually no user training and supports file access and delivery on almost all popular PC and mobile devices. Read how Box let companies rapidly implement a cost-effective and secure content storage and sharing system that can easily expand to accommodate any size and number of files.

See more White Papers | Webcasts

Ask a question

Ask a Question