How can I delete empty folders in Mac OS X?

28

11

I would like to delete all empty folders on an external disk. How can I do this?

Msquared

Posted 2013-01-07T05:38:45.013

Reputation: 281

Answers

43

First of all note that deleting empty directories usually isn't necessary. Some services or applications might even need certain directories to exist. Be aware of what you're doing.

You can list empty folders with find, when run from Terminal.app:

find . -type d -empty

By default find lists files and folders, but here, -type d restricts it to directories, and the -empty option shows only empty directories. This will recurse all folders descending from your home directory (/Users/your-username/ or short, ~). To expand this to your entire filesystem, use:

find / -type d -empty

Here, / signifies the root of your Mac OS X file system. You can of course use any other starting point as well, for example your external disk mounted under /Volumes/your-disk-name;

find /Volumes/your-disk-name -type d -empty

Now, if you want to delete whatever find outputs, simply append -delete, like so:

find . -type d -empty -delete

Note: This will not ask for confirmation. It'll delete all the directories it can, i.e. the ones where you have permissions to delete. They will not be moved to the trash but gone forever. If you want to be asked before removal, change the command to something like the following:

find . -type d -empty -exec rm -ri '{}' \;

slhck

Posted 2013-01-07T05:38:45.013

Reputation: 182 472

6While this works, it probably doesn't locate folders that only contain the hidden .DS_Store files, making them technically non-empty but appearing empty to the user. If a user wants to find all folders that appear empty, those he once had opened in Finder won't be found this way because the Finder would have added a .DS_Store file. – SuperTempel – 2014-11-04T12:17:38.447

So we might need to do it in two steps: first remove .DS_Store files (can be done using find in a similar way as above), and then use the suggested command to remove remaining empty directories. – gerlos – 2015-08-04T16:42:12.223

It gives me output about illegal option -- t typeand empty seems to not be able to recognize. – AsTeR – 2013-03-05T12:38:59.860

1@AsTeR The OS X version of find requires a path, so try again with find . -type d, not find -type d (the latter works for GNU find). – slhck – 2013-03-05T12:43:43.253

6

I've made a small and free program that solves this better:

http://www.macupdate.com/app/mac/52551/find-empty-folders

The advantage of this program is that it also finds folders that are apparently empty but contain the invisible ".DS_Store" file.

And it also lets you move the found items to Trash right away.

SuperTempel

Posted 2013-01-07T05:38:45.013

Reputation: 331

Would be good if it can handle the Icon file as well - https://superuser.com/questions/298785/icon-file-on-os-x-desktop

– kenchew – 2017-06-05T09:51:51.393

@kenchew I believe you are looking for this find ~/Documents -type f -name 'Icon?' -print -delete; – JayRizzo – 2018-05-14T05:42:50.013

2

To further this effort:

I created a script I use to clean my documents folder from time to time, as I am way to OCD and tired of being APP Overloaded & like simplicity.

I made this in an effort to improve & provide an alternate solution.

Lastly, for @kenche's Icon File, that doesn't seem to exist on my mac as I have not modified my folder icons, but you can in the inspector. Upon dragging a picture to the top left it will create the Icon^M file on that directory.

To find those too you can run:

(Should you be paranoid about if finding a false positive then use:ctrl+v ctrl+m instead of the ?)

find ~/Documents -type f -name 'Icon?' -print;
# and to remove 
find ~/Documents -type f -name 'Icon?' -print -delete;

IMPORTANT

Please as @slhck stated above: Some services or applications might even need certain directories to exist. This also applies to the DS_Store & Icon files, PLEASE Be aware of what you're doing. Also Note: This script will not ask for confirmation. It will delete all the directories it can. i.e. the ones where you have permissions to delete. They will not be moved to the trash but gone forever.

BASH Script

#!/bin/bash
# =============================================================================
# MAC OSX HIGH SIERRA 10.13.4 (17E199)
# Terminal: Version: 2.8.2 64-Bit (Intel): Yes
# Terminal Location: /Applications/Utilities/Terminal.app
# =============================================================================
# Terminal CLEAN UP YOUR DOCUMENTS FOLDER.
# =============================================================================
# START WHAT IS BELIEVED TO BE EMPTY NOW.
# =============================================================================
echo 'Searching Documents for empty folders...'
find ~/Documents -type d -empty -print;
# =============================================================================
# SHOW & THEN REMOVE ALL MAC OS DS_Store FILES
# =============================================================================

echo 'Searching Documents for DS_Store files...'
find ~/Documents -type f -name ".DS_Store" -print;

echo 'Removing DS_Store files...'
find ~/Documents -type f -name ".DS_Store" -print -delete;
# =============================================================================
# SHOW & THEN REMOVE ALL MAC OS ZERO SIZED FILES
# =============================================================================
echo 'Searching Documents for ZERO file sized files...'
find ~/Documents -type f -empty -print;

echo 'Removing ZERO file sized files...'
find ~/Documents -type f -empty -delete -print;
# =============================================================================
# SHOW & THEN REMOVE Icon^M FILES
# USE THE ? MARK FOR EASE OF USE YOU CAN ALSO SUB 'CTRL + V & CTRL + M' FOR ^M 
# =============================================================================

echo 'Searching Documents for Icon files...'
find ~/Documents -type f -name 'Icon?' -print;

echo 'Removing Icon files from Documents...'
find ~/Documents -type f -name 'Icon?' -print -delete;

# SEEMINGLY THE SAME AS
# find ~/Documents -type f -size 0 -print
# find ~/Documents -type f -size 0 -print -delete
# =============================================================================
# SHOWCASE NEW FOUND EMPTY FOLDERS
# =============================================================================
echo 'Showcasing new result of existing and new found empty folders...'
find ~/Documents -type d -empty -print;

echo 'Deleting result of empty folders...'
find ~/Documents -type d -empty -delete -print;

echo 'Showcasing the removal of said, 'empty folders'...'
find ~/Documents -type d -empty;

End of script.

Script can be seen here on GitHub

References:

About the icons that represent files on your Mac

Any way to remove all folders that contain only .DS_Store recursively?

Find folders & subfolders containing only ".DS_Store"

How to find all zero-byte files in a directory including subdirectories

Icon? file on OS X desktop

JayRizzo

Posted 2013-01-07T05:38:45.013

Reputation: 194