[olug] Challenge: Prune a directory tree

Brian Roberson roberson at olug.org
Thu Oct 14 20:21:11 UTC 2004


Not a recursive solution, but if ran multple times in succession should 
do the trick:

in bash even - for those perl haters.....

#############################
#!/bin/bash
unalias ls
find /some/base/directory/ -type d | while read dir ; do
if [ `ls $dir | grep -v "^\." | wc -l` -ge "1" ] ; then
echo "not removing $dir...."
else
rmdir $dir
fi
done
###############################






Jay Hannah wrote:

>
> Just for fun...
>
> I have a big, deep directory tree. Some directories contain files, 
> some don't.
>
> I want to prune out all the directories for which there are no files 
> in that directory, or any sub-directory thereof. Every directory 
> contains zero or more sub-directories.
>
> After the solution runs, all the files should remain, untouched and 
> unmoved. All empty directory branches should be gone.
>
> Clear as mud? I failed to Google a solution in 5 minutes.
>
> Any takers? If not maybe we'll hit it for the Perl Monger mtg next 
> Thr. -grin-
>
> j
> Omaha Perl Mongers: http://omaha.pm.org
>
> _______________________________________________
> OLUG mailing list
> OLUG at olug.org
> http://lists.olug.org/mailman/listinfo/olug





More information about the OLUG mailing list