a question for you shell scripters
L. Adrian Griffis
adrian at nerds.org
Thu Mar 27 02:34:13 CST 2003
On Wed, 26 Mar 2003, Gerald Combs wrote:
> How about
>
> find <path> -type d -links 2
>
> Empty directories normally have two links - themslves and the "." entry
> they contain. Unless you're doing something silly like creating hard
> links to directories, the above command will find any empty ones. Of
> course, you'd have to make multiple passes to catch any newly emptied
> directories.
You wouldn't have to make multiple passes if you changed it
to:
find <path> -depth -type d -links 2 -exec rmdir {} ;
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.
It's important to note that the "-depth" will only help if
the "-exec" clause is used to remove subdirectories, since
that needs to be done by the time find checks the "-links 2"
option, or nearly empty directories that contain subdirectores
that get removed later, would fail the -links check.
If you leave off the -links option, you could try something
like:
rmdir `find <path> -depth -type d -print`
Which might be more efficient.
>
>
> > Kevin Hodle
> > CCNA, Network+, A+
> > Alexander Open Systems
> > Network Operations Center
> > kevinh at aos5.com
> >
> >
> > -----Original Message-----
> > From: Jason Clinton [mailto:clintonj at umkc.edu]
> > Sent: Wednesday, March 26, 2003 9:22 AM
> > To: kclug
> > Subject: a question for you shell scripters
> >
> >
> > How can I rmdir all subdirectories that have no files in them,
> > recursively. For instance: I have a music directory with artist
> > subdirectories with album directories beneath that. Over the course of
> > time, I have asked XMMS to delete a song I'm tired of hearing. I've
> > found that there are not artists that I have no songs remaining for so
> > their entire directory structure is just a bunch of empty folders.
> >
> > I came up with this newbish solution:
> >
> > # rmdir */*
> > # rmdir *
> >
> > This works fine (since rmdir will only delete empty stuff) from the
> > music directory and if I don't have lots of subdirectories, but I'm
> > wondering what the /right/ way to do it is.
> >
> > --
> > Jason Clinton
> > I don't believe in witty sigs.
> >
> >
> >
> >
>
>
>
>
More information about the Kclug
mailing list