a script to remove empty directories
Andreas Seidl
seidlcw@gmx.net
Thu Feb 19 18:53:00 GMT 2004
Bruce Ingalls wrote:
> Anyhow, here is an elegant, working solution. If no optional dir is
> passed, then the current dir is checked recursively, and empty
> subdirectories are passed.
>
> #!/bin/bash
> ROOT=${1:-.}
> if [ ! -d $ROOT ];then ROOT=.;fi
> find $ROOT -type d -empty|xargs rmdir -
Meanwhile I was able to overcome the spaces in filenames problem, and,
funny enough, i used wc -l as in the script posted by Volker for testing
a directory to be empty. But I use recursion, which is bad for
incorporation in other scripts. Anyway, below is, just for the records,
my meanwhile obsolete solution.
Your version is much faster (but has problems with spaces) than my
version and the version posted by Volker. (e.g. 4s versus 50s).
----- begin -----
#!/usr/bin/bash
# rmed -- remove empty directories recursively
# Andreas Seidl -- http://www.fmi.uni-passau.de/~seidl/ -- 19.02.2004
# usage: rmed [DIR] where DIR is an optional argument, the directory
# where to start examining.
if [ -z "$1" ] ; then
basedir="."
else
basedir=$1
fi
cd "$basedir"
for dir in * ; do
if [ -d "`pwd`/$dir" ] ; then
if [ `\ls "$dir" | wc -l` -eq 0 ] ; then
#if [ -z $(\ls "$dir") ] ; then
echo "empty: `pwd`/$dir"
rmdir "$dir"
else
#echo "non-empty: `pwd`/$dir"
rmed "$dir"
fi
fi
done
cd -
----- end -----
>
> Here is a shortcoming:
> 1) Makes only one pass, so if a directory only contains empty
> subdirectories, that new, empty parent remains.
> You could try saving the dirnames/parents of deleted directories into a
> list, or recursively attempt to rmed them.
>
>
>
>
More information about the Cygwin-apps
mailing list