17

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

34

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 '{}' \;
  • It gives me output about illegal option -- t typeand empty seems to not be able to recognize. – AsTeR Mar 5 '13 at 12:38
  • 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 Mar 5 '13 at 12:43
  • 5
    While 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 Nov 4 '14 at 12:17
  • 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 Aug 4 '15 at 16:42
5

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.

1

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;
# =============================================================================
# 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;

echo 'Removing ZERO file sized files...'
find ~/Documents -type f -empty -delete;
# =============================================================================
# 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;

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

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.