Simple but annoying bash(shell)/find issue

Posted on 2009-12-24
Last Modified: 2013-12-26
I wanted to remove all the '.svn' dirs. in a file structure so I simply did (excuse any typos):

find ./ -type d -name '.svn' -exec rm -r {} \;

This works but the "rm" part removes all subdirs. and then "find" spits out a "No such file or directory" error for every single dir processed.

Does anyone no a better way to do this so there are no errors generated?

Question by:RegProctor
    LVL 68

    Expert Comment


    use the -depth option!

     -depth processes each directory's contents before the directory itself, so that the message "No such file or directory"  is avoided.

    find ./ -type d -name '.svn' -depth -exec rm -r {} \;


    LVL 40

    Expert Comment

    can you show sample output of

    find ./ -type d -name '.svn'
    LVL 7

    Expert Comment

    Any spaces in filenames?
    This command is better anyway (not forking a new process for each single dir):

    find . -type d -name "*.svn" -print0|xargs --null -r rm -rf

    However be aware, that if there are any .svn below .svn then it may lead to trying to remove dirs, that don't exist anymore. This doiesn't matter however.
    LVL 9

    Expert Comment

    find . -name ".svn" -type d -exec rm -rf {} \;
    LVL 1

    Author Comment

    Thanks all but none of these actually gets rid of the error.

    Regarding your questions and suggestions:
    - There will be no spaces in the file names
    - No need for '*.svn', the directory is always specifically '.svn'.
    - No need for "rm -rf", i.e.: adding the 'f' flag as the dirs. are getting removed.
    Here is a sample of the output. Note the line starting it is a bash script but when you look at the script (below), it's essentially the one command so there is no real difference:

    reg@quark:~/scripts> ./ ~/data/core/
    find: `/home/reg/data/core/cms/modules/system/.svn': No such file or directory
    find: `/home/reg/data/core/cms/modules/help/.svn': No such file or directory
    find: `/home/reg/data/core/cms/_moved-from-home/.svn': No such file or directory
    find: `/home/reg/data/core/cms/.svn': No such file or directory
    find: `/home/reg/data/core/cms/scripts/.svn': No such file or directory
    find: `/home/reg/data/core/cms/includes/.svn': No such file or directory
    find: `/home/reg/data/core/cms/misc/farbtastic/.svn': No such file or directory
    find: `/home/reg/data/core/cms/misc/.svn': No such file or directory
    reg@quark:~/scripts> rm -r ~/data/core/.svn

    Note the line:  "rm -r ~/data/core/.svn" above. That does not produce any errors.

    Here is the script, as you can see I used the latest suggestion for this particular output.

    #! /bin/bash
    if [ -n "$1" ]; then
    #find "$d" -type d -name '.svn' -exec rm -fr {} \;
    find "$d" -type d -name '.svn' -print0|xargs --null -r rm -r

    Open in new window

    LVL 1

    Author Comment

    Oops, sorry, I miss read the question, here is the sample output  you wanted using just:
        find ~/data/core -type d -name '.svn'

    reg@quark:~/scripts> find ~/data/core -type d -name '.svn'
    LVL 68

    Expert Comment

    Did you try -depth , the option I suggested in the very first comment to this Q?
    LVL 1

    Author Comment

    One more thing, there will never be a '.svn' below a '.svn'.
    LVL 1

    Author Comment

    Did you try -depth... yes I tried all suggestions.
    LVL 1

    Author Comment

    Ok, I take it back! I don't understand it but this line:

    find "~/data/care" -depth -type d -name '.svn' -exec rm -r {} \;

    I re-examined it after you prompting me because the logic made perfect sense that it should solve the problem.

    When executed the line from the bash shell it did not produce any errors.
    However, when execute from the script I showed you, it did.

    So, for whatever reason the issue is to do with using a script... which makes no sense to me.
    LVL 68

    Accepted Solution


    it doesn't really make sense to me neither,
    but please try to call the script like this:

    ./ "~/data/core"

    Note the double quotes around the parameter - maybe the expansion of the command line argument done by the shell would do some harm to find (??)

    If no luck you could also try single quotes.
    And please omit the trailing slash. It's not needed, and we should leave out everything which could cause trouble.


    LVL 1

    Author Closing Comment

    The only reason I show it as not complete is the that the problem just wasn't fully solved in that the error persisted in the script and I didn't want someone looking at this mistakenly thinking it was completely solved.

    However, it did get rid of the error outside the script and I think that is as good as we can get for this one so kudos for the the great solution anyway.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now