Categories
java

Linux/Unix find command question…

I have a nagging question related to the Unix/Linux find command. When I do a find with an -exec rm -rf I get a bunch of messages back saying ” find: `<what I found>’: No such file or directory”. My problem is that that message is garbage because find DID find what I asked it to find. It found it and successfully ran the -exec command on it. I can do an ls and prove it.

So digging a bit further I see something interesting. If I add the -v flag to my “rm -rf” I get a verbose listing of what rm is doing. So somebody please explain this… You can look at the output below if you don’t believe me. Why does find drill down into a dir tree. Do what I asked it to with the exec, in this case run the rm. Then immediately run a find on the file it just found and rm’ed???

eweibust@CWXPTDV6N02U $> find . -name “.svn” -exec rm -vrf {} \;
removed `./.svn/empty-file’
removed `./.svn/entries’
removed `./.svn/format’
removed directory: `./.svn/prop-base’
removed directory: `./.svn/props’
removed `./.svn/README.txt’
removed `./.svn/text-base/pom.xml.svn-base’
removed directory: `./.svn/text-base’
removed directory: `./.svn/tmp/prop-base’
removed directory: `./.svn/tmp/props’
removed directory: `./.svn/tmp/text-base’
removed directory: `./.svn/tmp/wcprops’
removed directory: `./.svn/tmp’
removed directory: `./.svn/wcprops’
removed directory: `./.svn’
find: `./.svn’: No such file or directory

It makes no sense. Either way, that is why I get the annoying “No such file or directory” error message back. No kidding you couldn’t find “whatever”. You just found it and rm’ed it. Jeez.

Is there a way to suppress this behaviour? Am I using find/rm incorrectly? Am I nuts for obsessing over this?

17 replies on “Linux/Unix find command question…”

Anjan, I tried what you recommended and still had no luck.

eweibust@CWXPTDV6N02U $> find . -name “.svn” -type d -exec rm -vrf {} \;
removed `./.svn/file1′
removed `./.svn/file2′
removed directory: `./.svn/junk1′
removed directory: `./.svn’
find: `./.svn’: No such file or directory

What you need to do is add -depth to the options of the command. Then find will process directory contents first, so when it gets to deleting the directory it won’t expect it to be there afterwards.

Hmm… Good tip, Ben. I’ll try it tomorrow, and report back on my results. Thanks for stopping by.

Great tip, Ben! The -depth option did the trick. I’m not sure I understand why because the man page description for -depth says… Process each directory’s contents before the directory itself. That still makes me think that find will still process the dir, but it doesn’t look like it is.

Either way, my problem is resolved.

I know this is old stuff, but I had the same problem and google’d this page looking for a solution.

-depth indeed solves the problem, but I can also explain why:

Without -depth, after rm’ing .svn, find will attempt to enter the directory it just found (and deleted) to see if there are any subdirectories named “.svn” to process. (The find process being blissfully unaware of what its -exec part just did.)

With -depth, find will *first* recurse into subdirectories, so after it deleted the top-level .svn it won’t attempt to find anything within.

Hope this sheds some light on it.

Fantastic, my hair is saved. I have been ripping it out in huge chunks over precisely this error and the -depth did the trick. I’l certainly make sure I remember this one.

Hi!

It saved my day, too… So, to sum up, here is the final command:

$> find . -depth -name “.svn” -exec rm -vrf {} \;

(It could prove useful when I stumble upon it again in a few years…)

Thanks!

Same thing here, and my first try was also the -type d, thinking maybe it was trying to delete the contents after deleting the dir. Another happy find -exec rm -rf user served!

I love the internet.

Thanks also! I was doing a “mv” instead of an “rm” but had the same problem. -depth fixed it.

Leave a Reply

Your email address will not be published. Required fields are marked *