Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have quite complex directory tree. There are many subdirectories, in those subdirectories beside other files and directories are ".svn" directories.

Now, under linux I want to delete all files and directories except the .svn directories.

I found many solutions about opposite behaviour - deleting all .svn directories in the tree. Can somebody quote me the correct answer for deleting everything except .svn?

share|improve this question
add comment

3 Answers

up vote 5 down vote accepted

I usually use a relatively simple find with the -exec option, as I always forget about the -delete command. I also restrict to files-only. Mostly because I use some variation of find {someswitches} -exec {somecommand} a lot - so I remember it!

find . -type f -not path '*.svn*' -exec rm {} \;

share|improve this answer
2  
Using ubuntu I had to add a - before the path like this: find . -type f -not -path '*.svn*' -exec rm {} \; –  Heinrich Filter Apr 3 '12 at 9:02
add comment

Try this rm -rf -- $(ls -la |grep -v .svn). It will remove everything (including hidden files) except the .svn dir.

EDIT: The above solution works for one dir, not a tree, find . ! -name .svn -exec rm {} \; will remove all FILES and not the dirs. It's a safe way to do that, since if you force the rm on directories you can delete directories that have .svn directories inside.

share|improve this answer
    
Sorry. I can't parse your last sentence. What do you mean by "if you force the rm on directories you can delete directories that have .svn directories inside." –  Alexandre Jasmin Aug 2 '10 at 21:43
    
Suppose you have a dir called x that has a .svn dir inside, if you use rm -rf you can end removing x and all it's child directories, thus removing the .svn subdir in the process. –  coredump Aug 3 '10 at 1:11
    
I tried your second command (without the -exec) and it deletes all the files in the .svn folders too. I think what Arek wants is to keep all the stuff in the .svn folders too, not just empty folders. You also get all sorts of errors about trying to delete directories. –  Cylindric Feb 3 '11 at 12:39
add comment

Untested: find . -not -path '*.svn*'... if those are all the files you want to clobber, run it again with the -delete option.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.