a script to remove empty directories

Andreas Seidl seidlcw@gmx.net
Thu Feb 19 19:07:00 GMT 2004


Dr. Volker Zell wrote:

> Here is another one from
> 
>  o http://www.shelldorado.com/scripts/categories.html

I searched with Google, but did not find it...

> ----------- cut here -------------------------------
> :
> # rmemptydir - remove empty directories
> # Heiner Steven (heiner.steven@odn.de), 2000-07-17
> #
> # Category: File Utilities
> 
> [ $# -lt 1 ] && set -- .
> 
> find "$@" -type d -depth -print |
>     while read dir
>     do
>         [ `ls "$dir" | wc -l` -lt 1 ] || continue
>         echo >&2 "$0: removing empty directory: $dir"
>         rmdir "$dir" || exit $?
>     done
> exit 0
> ----------- cut here -------------------------------

This is the best one we have seen today. The clever -depth option to 
find ensures, that directories, which contain only empty directories, 
are removed as well:

$ find . -type d -depth -print
./asd
./asdf asdf/jkl
./asdf asdf
./usr/bin
./usr
.

as opposed to:

$ find . -type d -print
.
./asd
./asdf asdf
./asdf asdf/jkl
./usr
./usr/bin





More information about the Cygwin-apps mailing list