Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

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

Posted on May 4, 2016January 29, 2024 By David Kittell

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.

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

# List empty directories
find ~/ -depth  -empty -type d
# 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 {} \;
# 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

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

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

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

# Delete directories that Dreamweaver adds
find ~/ -depthDocuments/WebSite -name "_notes" -type d -exec rm -rfv {} \;
Originally Posted on May 4, 2016
Last Updated on January 29, 2024
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.

Related

CentOS Code Fedora Mac OS X Shell Mac OSX Red Hat Ubuntu UNIX UNIX Shell Scripts remove empty directoryremove empty file

Post navigation

Previous post
Next post

Related Posts

MySQL to MSSQL Data Conversion

Posted on October 18, 2014October 26, 2015

Ever have to take a MySQL database and put it in Microsoft SQL? This is a MySQL dump script that will assist you in this conversion. Only problem that I’ve had with it is that you need to review the exported SQL and make sure your version of Microsoft SQL…

Read More

UNIX – List USB Devices With lsusb

Posted on February 12, 2018February 12, 2018

On the Mac “lsusb -v” is rather clean in comparison to other systems. # If not already installed… # brew update && brew tap jlhonora/lsusb && brew install lsusb lsusb -v lsusb -v # or lsusb -v | grep -E ‘\<(Bus|iProduct|bDeviceClass|bDeviceProtocol|idVendor|idProduct|iManufacturer)’ 2>/dev/null All information on this site is shared with…

Read More

Display Second Largest Value

Posted on June 26, 2014October 26, 2015

SELECT MAX(col) FROM TABLE WHERE col < ( SELECT MAX(col) FROM TABLE ) Originally Posted on June 26, 2014Last Updated on October 26, 2015 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…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • Create Shortcut (VB, VB.Net, PowerShell)
  • SQLite - Auto-Increment / Auto Generate GUID
  • Mac OS X - Remove Users In Terminal/SSH

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • Create Shortcut (VB, VB.Net, PowerShell)
  • SQLite - Auto-Increment / Auto Generate GUID
  • Mac OS X - Remove Users In Terminal/SSH
  • PowerShell - UNIX SED Equivalent - Change Text In File
  • Mac OSX Terminal - Convert RAW Photos to JPG
©2025 David Kittell | WordPress Theme by SuperbThemes