3

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?

3 Answers 3

7

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

1
  • 2
    Using ubuntu I had to add a - before the path like this: find . -type f -not -path '*.svn*' -exec rm {} \; Apr 3, 2012 at 9:02
1

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

0

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.

3
  • 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." Aug 2, 2010 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, 2010 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, 2011 at 12:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .