This is the mail archive of the cygwin-apps@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: a script to remove empty directories


Andreas Seidl wrote:


Igor Pechtchanski wrote:


On Wed, 18 Feb 2004, Andreas Seidl wrote:

Hmm, wouldn't the generic build script be the place to add functionality
to remove empty directories? Doing it by hand adds work, and even worse,
is a possible source for bugs, as a newer release might have actually
files in directories which were empty before, and if overlooked, they
could get removed.




Andreas,

Feel free to send a patch for this.
    Igor



I have written the following script, which is as good as I can do at the moment.


----- begin -----
#!/usr/bin/bash
# rmed -- remove empty directories recursively
# usage: rmed [DIR] where DIR is an optional argument, the directory
#   where to start examining.
# limitations:
#   - spaces in file or direcory names



Hm. I'm used to Linux compatible systems treating `test -z` as checking files/dirs for zero length, not strings.
I'm not sure why the following doesn't work:
#!/bin/bash
for dir in **/*; do if [ -d $dir -a ! -s $dir ];then echo $dir; fi; done
for dir in **/.*; do if [ -d $dir -a ! -s $dir ];then echo $dir; fi; done


Maybe zsh works better than bash here?

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 -

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.





Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]