Linux/Unix/Mac OSX – Find & Remove Empty Directories

| | | | | | | |

Occasionally you get empty directories and need to clear them out, these scripts below will help with that, just be careful what directory you choose.

The scripts below are set to ~/ (current user home directory) but can easily be changed to any directory on your system.

This first block will only list the empty files and directories.

Bash
# List empty files
find ~/ -depth  -empty -type f

# List empty directories
find ~/ -depth  -empty -type d

This second block will look in the current user’s home directory and delete the empty files and directories along with some files most people don’t want to have taking up space.

NOTE: You can simply remove line 6 if you only want to remove empty files.

Bash
# Delete Empty Files/Directories - User Home Directory
# Delete empty files
find ~/ -depth -empty -type f -delete

# Remove .DS_Store and more files before delete empty directories so it actually removes empty directories
find ~/ -depth \( -name "Thumbs.db" -o -name "desktop.ini" -o -name "error_log" -o -name ".DS_Store" -o -name "._*" -o -name "SyncToy*.dat" -o -name "Picasa.ini" \) -type f -exec rm -rfv {} \;

# Remove empty files in general before delete empty directories
find ~/ -depth  -empty -type f -exec rm -rfv {} \;

# Delete empty directories
find ~/ -depth  -empty -type d -exec rm -rfv {} \;

This third block will look in the current directory and delete the empty files and directories along with some files most people don’t want to have taking up space.

NOTE: You can simply remove line 6 if you only want to remove empty files.

Bash
# Delete Empty Files/Directories - Current Directory/SubDirectories
# Delete empty files
find . -depth -empty -type f -delete

# Remove .DS_Store and more files before delete empty directories so it actually removes empty directories
find . -depth \( -name "Thumbs.db" -o -name "desktop.ini" -o -name "error_log" -o -name ".DS_Store" -o -name "._*" -o -name "SyncToy*.dat" -o -name "Picasa.ini" \) -type f -exec rm -rfv {} \;

# Remove empty files in general before delete empty directories
find . -depth -empty -type f -exec rm -rfv {} \;

# Delete empty directories
#find . -depth -empty -type d -delete
find . -depth -empty -type d -exec rm -rfv {} \;

Similarly if you have a file or directory in multiple subdirectories that you need removed you can do

Bash
# Delete Files/Directories

# Delete files - Option 1
find ~/ -depth Documents/WebSite -name "Thumbs.db" -type f -exec rm -rfv {} \;

find ~/ -depth Google\ Drive -name "desktop.ini" -type f -exec rm -rfv {} \;

# Delete files - Option 2
find . -depth -name "SyncToy*.dat" -print0 | xargs -0 rm -rf

# Delete directories that Dreamweaver adds
find ~/ -depth -depthDocuments/WebSite -name "_notes" -type d -exec rm -rfv {} \;
Originally Posted on May 4, 2016
Last Updated on October 15, 2025
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.