?
Solved

Simple but annoying bash(shell)/find issue

Posted on 2009-12-24
12
Medium Priority
?
370 Views
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?

Thanks,
Reg
0
Comment
Question by:RegProctor
12 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 26118602
Hi,

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 {} \;

wmp


0
 
LVL 40

Expert Comment

by:omarfarid
ID: 26118619
can you show sample output of

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

Expert Comment

by:hemmi
ID: 26118731
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.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
LVL 9

Expert Comment

by:jeff_01
ID: 26118899
find . -name ".svn" -type d -exec rm -rf {} \;
0
 
LVL 1

Author Comment

by:RegProctor
ID: 26120843
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> ./svn-remove.sh ~/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
reg@quark:~/scripts>

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
    d=$1
else
    d='./'
fi

#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

0
 
LVL 1

Author Comment

by:RegProctor
ID: 26120859
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'
/home/reg/data/core/cms/modules/menu/.svn
/home/reg/data/core/cms/modules/blogapi/.svn
/home/reg/data/core/cms/modules/system/.svn
/home/reg/data/core/cms/modules/help/.svn
/home/reg/data/core/cms/_moved-from-home/.svn
/home/reg/data/core/cms/.svn
/home/reg/data/core/cms/scripts/.svn
/home/reg/data/core/cms/includes/.svn
/home/reg/data/core/cms/misc/farbtastic/.svn
/home/reg/data/core/cms/misc/.svn
reg@quark:~/scripts>
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 26120865
Did you try -depth , the option I suggested in the very first comment to this Q?
0
 
LVL 1

Author Comment

by:RegProctor
ID: 26120867
One more thing, there will never be a '.svn' below a '.svn'.
0
 
LVL 1

Author Comment

by:RegProctor
ID: 26120873
Did you try -depth... yes I tried all suggestions.
0
 
LVL 1

Author Comment

by:RegProctor
ID: 26120900
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.
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 1200 total points
ID: 26120939
OK,

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

./svn-remove.sh "~/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.

wmp


 
0
 
LVL 1

Author Closing Comment

by:RegProctor
ID: 31669750
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.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

840 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