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

C# WinForm Write Text Log

Posted on July 31, 2014October 26, 2015

public void LogMessage (string msg) { string sFilePath = Environment.CurrentDirectory + "Log_" + System.AppDomain.CurrentDomain.FriendlyName + ".txt"; System.IO.StreamWriter sw = System.IO.File.AppendText(sFilePath); try { string logLine = System.String.Format( "{0:G}: {1}.", System.DateTime.Now, msg); sw.WriteLine(logLine); } finally { sw.Close(); } } LogMessage("Problem with program"); Originally Posted on July 31, 2014Last Updated on October 26,…

Read More

Update Record Upper / Lower Case

Posted on March 19, 2013October 26, 2015

Upper Case UPDATE [Members] SET [Email_1] = UPPER([Email_1]) WHERE [Email_1] = [Email_1] Lower Case UPDATE [Members] SET [Email_1] = LOWER([Email_1]) WHERE [Email_1] = [Email_1] Originally Posted on March 19, 2013Last Updated on October 26, 2015 All information on this site is shared with the intention to help. Before any source…

Read More

UNIX – Bash Identify Distribution (What OS Am I On)

Posted on April 9, 2018April 6, 2021

Originally Posted on April 9, 2018Last Updated on April 6, 2021 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…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • PowerShell - IIS Remove Site
  • SQLite - Auto-Increment / Auto Generate GUID
  • PowerShell - FTP Upload Directory With Sub-Directories

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
  • PowerShell - IIS Remove Site
  • SQLite - Auto-Increment / Auto Generate GUID
  • PowerShell - FTP Upload Directory With Sub-Directories
  • Raspberry Pi - Remove Default Apps
  • PowerShell - Change Windows CD/DVD Drive Letter
©2025 David Kittell | WordPress Theme by SuperbThemes