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?

Arek
  • 155
  • 4
  • 9

3 Answers3

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

Cylindric
  • 1,107
  • 5
  • 24
  • 44
1

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

medina
  • 1,970
  • 10
  • 7
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.

coredump
  • 12,573
  • 2
  • 34
  • 53
  • 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."* – Alex Jasmin Aug 02 '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 03 '10 at 01: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 03 '11 at 12:39