On Mon, 25 May 2015, Tim Chase wrote:
> On 2015-05-25 09:32, [email protected] wrote:
> > $ mkdir -p /tmp/foo/bar
> > $ ls -dF /tmp/foo/bar
> > /tmp/foo/bar/
> > $ find /tmp/foo -type d -empty -ls
> > 31751 4 drwx------ 2 craig wheel 512 May 25 09:06 /tmp/foo/bar
> > 
> > $ find /tmp/foo -type d -empty -exec rmdir -- {} \;
> > find: /tmp/foo/bar: No such file or directory
> >                     ^^^^^^^^^^^^^^^^^^^^^^^^^
> 
> Build the list of files/dirs to delete depth-first and it Works For
> Me(TM):
> 
>  find /tmp/foo -depth -type d -empty -exec rmdir -- {} \;

That prunes from the bottom up, so it'll also remove directories that only 
contain directories that are empty, and so on recursively, which may not 
be the goal.

If the goal is only to remove the empty leaf directories, then use -prune 
to tell find to not recurse into the directory you're removing:

        find /tmp/foo -type d -empty -prune -exec rmdir -- {} \;



> or possibly
> 
>  find /tmp/foo -depth -mindepth 1 -type d -empty -exec rmdir -- {} \;

That works, but is dependent on the target hierarchy only being 2 levels 
deep, so it's harder to generalize.


Philip

Reply via email to