[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: find generates spurious error message

From: Bob Proulx
Subject: Re: find generates spurious error message
Date: Fri, 22 Nov 2002 00:00:02 -0700
User-agent: Mutt/1.4i

Paul Almquist <address@hidden> [2002-11-18 15:19:15 -0600]:
> Here is a script that says is all:
> #   Complaint:  find generating spurious error message when removings
> #               empty directory.

Oops!  Wrong list.

  find --help
  Report bugs to <address@hidden>.

This is fileutils not findutils!

But I think I see your problem anyway.  Thanks for a good test case.
It really helps with all of the information and commands to recreate
things.  That was great.

> clear
> mkdir -p /tmp/dir/subdir1/subdir2
> echo "Before - test case as reported by 'tree'"
> tree /tmp/dir
> echo "Note that  /tmp/dir/subdir1/subdir2  exists"
> echo
> echo "directories as find reports them"
> find /tmp/dir  -type d
> echo
> echo "now removing empty directory"
> find /tmp/dir  -type d  -exec rmdir --ignore-fail-on-non-empty {} \;

If you check here you should find that rmdir did NOT remove that
directory.  It just ignored the failure.  However, find prints an
error.  It confused me when I first saw that because the rmdir ignore
errors option pulled me into rmdir instead of find.  But find prints
the error:

  find: /tmp/dir/subdir1/subdir2: No such file or directory

> #               This error message misleads the user into thinking
> #               that something did not work correctly so debugging
> #               was done only to discover that the messages is false.
> #               Now that I have resolved this issue I vaguely remember
> #               going thru this some time ago.  No doubt others have
> #                as well.  Please fix so none else has to repeat this.

I disagree.  The problem is that find is traversing down the hierarchy
of directories.  One of the directories went away during the find
run.  You removed it right out from under find.

Find lists the directory, then descends into each directory it listed
and repeats.  It listed a directory.  Then it tried to descend into it
but the directory which was just there was no longer there.  Find is
at a loss at what to do in that case and so it reported it as an
error.  It is certainly an odd case.

Fortunately there is a good methodology for what you want to do.  If
you are removing directories with find you need to work the problem
out from the bottom up.  Remove the bottom directory first.  Then find
will list then, descend into them, and run the command you are
exec'ing on the way up and not the way down.  This will avoid the
error you are seeing and is designed for just such a purpose.

Add the 'depth' option to your find command.

  find /tmp/dir -type d -depth -exec rmdir {} \;

That will remove the directories from the bottom up and you won't have
any need to ignore errors.

Also, I can't talk about find -exec without pushing xargs as a better
solution.  This is very effecient since it invokes rmdir with a list
of parameters and avoids invoking rmdir for a single parameter.

    find /tmp/dir -type d -depth -print0 | xargs -r0 rmdir

Note that print0 and xargs -0 are GNU extensions and non-standard.
But they perfectly handle the job and are very useful.  If you are not
using GNU find and xargs then your original -exec solution is the best
you can do.


Please follow up to the list and not to me privately unless it is

reply via email to

[Prev in Thread] Current Thread [Next in Thread]