Your own personal IT department

Handy linux script to recursively delete empty directories

If you have ever had the problem of lots of empty directories that you want to clean up by recursively deleting an empty directories, while still leaving any files  in their existing structure, then this handy script is your saviour!

Enter the base directory after “SOURCE=” and  this script will quickly cleanup any messy empty folder structure (such as that left behind from running a “rsync -avRtp –remove-source-files –ignore-existing $SOURCE $DESTINATION” archival command.

#!/bin/bash
# ———————————————————————-
# this script will find all empty directories and remove them.
# ———————————————————————-

unset PATH # avoid accidental use of $PATH

# ————- file locations —————————————–

SOURCE={base directory from which you want to clean out the empty directories from}
/usr/bin/find $SOURCE -type d -empty -prune -exec rmdir –ignore-fail-on-non-empty -p \{\} \;

 
Comments

No comments yet.