a question for you shell scripters

Gerald Combs gerald at ethereal.com
Wed Mar 26 16:11:59 CST 2003


On Wed, 26 Mar 2003, Kevin Hodle wrote:

> If you ask me, the 'right' way would be witchever way works :)
> 
> ... But, if you wanted a snippet of shell code, here you go
> 
>  ls /whatever > test.dir
>  if [ -s test.dir ]; then
>     # directory has stuff in it
>  else
>     #directory is empty, you can rm -rf it
> 
> 
> Frankly, your original method seems fine for your needs.

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.

> 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