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?

This Post Has 17 Comments

  1. anjan bacchu

    hi there,

    can you try
    find . -type d -name .svn ?

    BR,
    ~A

  2. anjan bacchu

    Hi there,

    OOPS, I meant that you just add “-type d” to your existing find commandline .

    BR,
    ~A

  3. erik

    Anjan, I’ll give it a try tomorrow and report back.

  4. erik

    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

  5. Ben Hurwitz

    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.

  6. erik

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

  7. erik

    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.

  8. DevSolar

    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.

  9. tymoteusz

    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.

  10. Stéphane Mor

    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!

  11. Steve

    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.

  12. Nick

    Guys, thanks for this! It saves me receiving the same error message on the daily cron.

  13. Rusty

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

  14. dridri

    or.. you could just do a:

    find . -name “.svn” -delete;

  15. dridri

    or not.. just forget about it, it doesn’t work for non-empty directories 🙁

  16. Ferhat Ayaz

    wow, I had exact the same problem with exact the same .svn folders 🙂 Great!

  17. DevSolar

    You could, of course, simply use “svn export” as well. 😉

Leave a Reply