Skip to main content

emptydir

Look for empty (or nearly empty) directories and delete them

This tool looks for empty directories and deletes them.

$ emptydir

More specifically, it deletes directories which are completely empty, or which only contain files/folders which I don’t think are worth keeping (e.g. .DS_Store or __pycache__).

Why not use find?

Deleting empty directories is a common problem, and there’s a suggestion on Unix Stack Exchange question:

Combining GNU find options and predicates, this command should do the job:

find . -type d -empty -delete

The reason this isn’t suitable is because it only deletes directories which are completely empty. But sometimes a directory can be non-empty, even if it appears empty.

For example, this directory on macOS:

A Finder window for a folder 'totally_empty' which apparently contains no files.

I consider this directory to be empty, but it will be skipped by find . -type d -empty -delete because of the hidden .DS_Store file.

This tool will delete directories which are empty or almost empty – that is, when they only contain files or folders which I don’t think are worth keeping, like .DS_Store or __pycache__.

Installation

You can download compiled binaries from the GitHub releases.

Alternatively, you can install from source. You need Rust installed; I recommend using Rustup. Then clone this repository and compile the code:

$ git clone "https://github.com/alexwlchan/emptydir.git"
$ cd emptydir
$ cargo install --path .

Usage

Pass the path of the top-level directory you want to clean as a command-line argument, for example:

$ emptydir ~/Desktop

It will print the path to every empty directory that it deletes:

$ mkdir -p ~/Desktop/foo/bar/baz
$ emptydir ~/Desktop/
/Users/alexwlchan/Desktop/foo/bar/baz
/Users/alexwlchan/Desktop/foo/bar
/Users/alexwlchan/Desktop/foo
3 directories deleted

If you pass no arguments, it will look for empty directories in the current directory:

$ emptydir

Which files/folders are safe to delete?

Currently the list of files/folders which I consider safe to delete is hard-coded in can_be_deleted.rs:

If you want to change that list, you need to modify the source code and compile a new version – it’s not a configurable setting.

Note that these files will only be deleted if they are the only items in a folder. If, for example, a folder contains both .DS_Store and some text files, then nothing will be deleted.

How it works

For a detailed explanation of how this tool works, you can read my accompanying article.

License

MIT.