Skip to main content

README.md

1# emptydir
3This tool looks for empty directories and deletes them.
5```console
6$ emptydir
7```
9More 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__`).
15## Why not use `find`?
17Deleting empty directories is a common problem, and there's a suggestion on [Unix Stack Exchange question](https://unix.stackexchange.com/a/107556/431830):
19> Combining GNU find options and predicates, this command should do the job:
21> ```
22> find . -type d -empty -delete
23> ```
25The reason this isn't suitable is because it only deletes directories which are *completely* empty.
26But sometimes a directory can be non-empty, even if it appears empty.
28For example, this directory on macOS:
30<img src="totally_empty.png" alt="A Finder window for a folder 'totally_empty' which apparently contains no files.">
32I consider this directory to be empty, but it will be skipped by `find . -type d -empty -delete` because of the hidden [`.DS_Store` file](https://en.wikipedia.org/wiki/.DS_Store).
34This 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__`.
40## Installation
42You can download compiled binaries from the [GitHub releases](https://github.com/alexwlchan/emptydir/releases).
44Alternatively, you can install from source.
45You need Rust installed; I recommend using [Rustup].
46Then clone this repository and compile the code:
48```console
49$ git clone "https://github.com/alexwlchan/emptydir.git"
50$ cd emptydir
51$ cargo install --path .
52```
54[Rustup]: https://rustup.rs/
60## Usage
62Pass the path of the top-level directory you want to clean as a command-line argument, for example:
64```console
65$ emptydir ~/Desktop
66```
68It will print the path to every empty directory that it deletes:
70```console
71$ mkdir -p ~/Desktop/foo/bar/baz
72$ emptydir ~/Desktop/
73/Users/alexwlchan/Desktop/foo/bar/baz
74/Users/alexwlchan/Desktop/foo/bar
75/Users/alexwlchan/Desktop/foo
763 directories deleted
77```
79If you pass no arguments, it will look for empty directories in the current directory:
81```console
82$ emptydir
83```
89## Which files/folders are safe to delete?
91Currently the list of files/folders which I consider safe to delete is hard-coded in `can_be_deleted.rs`:
93* .DS_Store stores some folder attributes used for showing the folder
94 in the Finder, which I don't need to keep
95* `.ipynb_checkpoints` is a folder used by Jupyter Notebooks, but not
96 important if I've deleted the notebooks
97* `.jekyll-cache` is a cache directory used by Jekyll sites, but
98 can be easily regenerated and will be rebuilt regularly as part
99 of the Jekyll build process
100* `.venv` is the name I use for virtual environments, which I can
101 easily regenerate if necessary
102* `__pycache__` is the bytecode cache in Python projects, which is
103 pointless if the original Python files have been removed
104* `Thumbs.db` is a file that contains thumbnails on Windows systems
106If you want to change that list, you need to modify the source code and compile a new version -- it's not a configurable setting.
108Note that these files will only be deleted if they are the only items in a folder.
109If, for example, a folder contains both `.DS_Store` and some text files, then nothing will be deleted.
113## How it works
115For a detailed explanation of how this tool works, you can read [my accompanying article](https://alexwlchan.net/2024/emptydir/).
119## License
121MIT.