a question for you shell scripters

Steven Elling ellings at kcnet.com
Thu Mar 27 04:26:04 CST 2003


Remember Jason Clinton said:
> How can I rmdir all subdirectories that have no files in them, 
> recursively.

The keyword here is `recursively', which I interpret as:

	If there are `no files' anywhere in a branch of a subdirectory tree, then 
remove that whole branch. For example:

/tmp/1dir
/tmp/1dir/1dir
/tmp/1dir/1dir/test-file

/tmp/2dir
/tmp/2dir/1dir
/tmp/2dir/test-file

/tmp/3dir
/tmp/3dir/1dir
/tmp/3dir/1dir/1dir
/tmp/3dir/2dir
/tmp/3dir/2dir/1dir

Do not remove the branch `/tmp/1dir' because a child branch `/tmp/1dir/1dir' 
has a file in it.

Remove the branch `/tmp/2dir/1dir' because there are no files in it but not 
its parent `/tmp/2dir' because there is a file in it.

Remove the whole `/tmp/3dir' branch because none of the child branches have 
a file in them.

The `-links' and `-empty' options to find will not work in this case because 
there could be a branch with subdirectories but no files anywhere in the 
branch.  The branch would have a indeterminate link count greater than 2 
and would not match the `-empty' option.

On Wednesday 26 March 2003 20:51, Gerald Combs wrote:
> On Wed, 26 Mar 2003, L. Adrian Griffis wrote:
> > I'm not sure the links think will limit the search the way
> > you expect it to.  Additional subdirectories will increase
> > the link count, but I don't think contained files will.
> > Still, rmdir will refuse to remove a non-empty directory,
> > so it won't hurt not to have filtered them all out, and
> > the "-links 2" option will at least filter out directories
> > that contain subdirectories.
>
> You're right.  Hal suggested '-empty', which is a better solution.




More information about the Kclug mailing list