59

What command can I type into the Terminal so that I can delete all .svn folders within a folder (and from all subdirectories) but not delete anything else?

Mithical
  • 321
  • 1
  • 3
  • 14
Mikey
  • 1,651
  • 2
  • 22
  • 27
  • Maybe you could just `svn export path/to/repo path/to/export/to` instead... – Jonny Jul 24 '15 at 04:09
  • A note on `svn export` is that is doesn't copy across files that are not part of svn. For example, I wanted to do this for an iOS application that uses Cocoa Pods where we do not commit the Pods folder. This was then skipped from the output. I ended up using something similar to Rich's answer for what I wanted. – jackofallcode Jun 21 '16 at 13:29

1 Answers1

127
cd to/dir/where/you/want/to/start
find . -type d -name '.svn' -print -exec rm -rf {} \;
  • Use find, which does recursion
  • in the current directory .
  • filetype is directory
  • filename is .svn
  • print what matched up to this point (the .svn dirs)
  • exec the command rm -rf (thing found from find). the {} is a placeholder for the entity found
  • the ; tells find that the command for exec is done. Since the shell also has an idea of what ; is, you need to escape it with \ so that the shell doesn't do anything special with it, and just passes to find
Rich Homolka
  • 31,057
  • 6
  • 55
  • 80
  • 3
    Good solution. While `find` has `-delete` it won't delete non-empty directories. This approach is cleaner than e.g. starting to match parts of the whole path of files. – Daniel Beck Feb 09 '12 at 17:02
  • 13
    I'd recommend running it without the `-exec rm -rf {} \;` the first time to make sure it's only finding what you want it to. I've never had an issue where I've accidentally deleting the wrong thing with it, because I always check first. – Rob Feb 09 '12 at 18:09
  • 2
    @Rob good point, i usually do -exec echo rm -rf {} \; then remove the echo. – Rich Homolka Feb 09 '12 at 18:29
  • Another option if there are not too many would be to confirm deletion of each file with `-exec rm -ri {} \;` – doovers Jan 14 '15 at 01:44
  • 4
    `-exec rm -rf {} +` is probably many times as fast, because it doesn’t have to launch too many `rm` instances. Instead, it packs the maximum amount of arguments (directories to remove in this case) in one call. This is possible because `rm` accepts multiple arguments. – Daniel B Feb 17 '15 at 18:33
  • Thank youuuu!!! So happy for this. And thanks for the explanation of what the command does at each step. – Joel Glovier Dec 19 '20 at 20:35
  • Thanks for this command and its simple explanation. However, in some instances, i get "Operation not permitted". Is there a way to force the deletion still? I am trying to clean a backup drive, and those files add so much time to the backup process otherwise. Thanks in advance! – jansensan Jan 13 '23 at 14:14