[olug] Re: [Omaha.pm] Challenge: Prune a directory tree

Daniel Linder dan at linder.org
Thu Oct 14 21:35:54 UTC 2004


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

<quote who="Jay Hannah">
> I have a big, deep directory tree. Some directories contain files, some
> don't.
> I want to prune out all the directories for which there are no files in
> that directory, or any sub-directory thereof. Every directory contains
> zero or more sub-directories.

I know it's sacreligious, but how about a simple "find" command?
  cd \base\directory\to\clean
  find . -depth -type d -exec rmdir {} \;

This goes to the maximal subdirectory depth of a tree, then works its way
backwards.  If a directory is empty it gets removed, if it has something
an error is printed to the screen when the "rmdir" fails.

Of course, this only works on a Unix-type system...  The Windows/DOS find
command doesn't work anywhere like this. :(

Dan

- - - - -
"I do not fear computer,
I fear the lack of them."
 -- Isaac Asimov

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFBbvE6NiBNyqUzGb8RAu91AJ9X0C76TcSNiX+zXUiEXy8K74OnogCeMM35
JrpbSl55ymzL2VKF6k6bD6Q=
=Rn07
-----END PGP SIGNATURE-----



More information about the OLUG mailing list