a question for you shell scripters

Steven Elling ellings at kcnet.com
Thu Mar 27 02:35:07 CST 2003


The beauty of Linux / UNIX is there is no `Right Way' to do anything. Their 
is the `What works for the task at hand'.

On my Linux system, rmdir supports the '-p' option, which tries to remove 
each directory component of the path name specified.  However, this is not 
that useful unless you know the paths that are empty prior to running or 
have a small directory tree.

Another option I use frequently is `find' combined with another command.  
You could run find in the following manner:

find /usr/local/share/multimedia -depth -type d -exec rmdir  
--ignore-fail-on-non-empty {} ;

or, if find does not support the `-exec' option:

find /usr/local/share/multimedia -depth -type d | xargs rmdir 2>/dev/null

You might want to check out the man pages for the commands listed here to 
make sure they support the options used.

find and xargs are some of the many command to put in your bag of tricks.

On Wednesday 26 March 2003 09:21, Jason Clinton wrote:
> 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.




More information about the Kclug mailing list