29

How can I recursively delete all files & directories that match a certain pattern? e.g. remove all the ".svn" directories and the files they contain?

(Sadly DOS only)

Rory
  • 40,559
  • 52
  • 175
  • 261

6 Answers6

56

Since you're looking for a DOS solution, last week's post was almost identical and the consensus was:

Command line tool to delete folder with a specified name recursively in Windows?

for /d /r . %d in (.svn) do @if exist "%d" rd /s/q "%d"

or

for /f "usebackq" %d in ("dir .svn /ad/b/s") do rd /s/q "%d"

Actually, SVN also gives you the option to export a working directory without the .svn/_svn directories.

Afterthoughts, three years later: I think the reason people end up needing to recursively delete the .svn/_svn folders is because they've directly copied their local working copy to a new location in order to do a folder comparison of their modified version compared to a clean export, i.e. after something goes awry with the modified local working copy. (At least that's why I've needed it. It's definitely easier/faster to just use 'svn export' when that's possible.)

Community
  • 1
  • 1
JMD
  • 7,331
  • 3
  • 29
  • 39
  • 5
    "Actually, apparently TortoiseSVN also gives you the option to export a working directory without the .svn/_svn directories." I believe that is a feature of svn itself (`svn export`) not just TortoiseSVN, so any other client can do it too. – Tyler Jun 15 '10 at 01:07
  • That was nice! Windows Explorer didn't always delete the .svn stuff. – cbmeeks Feb 08 '12 at 18:40
  • or try the hard core DOS variant suggested in http://superuser.com/questions/409479/how-do-i-recursively-delete-all-files-or-folders-whose-names-match-a-pattern-in – Gregor Apr 19 '12 at 14:10
  • What I have to modify in this if I want to delete a file name recursively using this command ? currently this work only for folders – Dev G Jul 11 '12 at 02:18
  • I've had more than one occasion where I needed to nuke all .svn directories because TortoiseSvn hosed the local repository, often as a result of the app crashing mid-operation, the user attempting to branch in a strange fashion, or the user misunderstanding Revert or Recover. – Chris Moschini Aug 06 '12 at 23:48
19

Is this Unix or Windows? On Unix, an easy solution is

find . -name '.svn' -type d | xargs rm -rf

This searches recursively for all directories (-type d) in the hierarchy starting at "." (current directory), and finds those whose name is '.svn'; the list of the found directories is then fed to rm -rf for removal.

If you want to try it out, try

find . -name '.svn' -type d | xargs echo

This should provide you with a list of all the directories which would be recursively deleted.

Antti Huima
  • 25,136
  • 3
  • 52
  • 71
  • 1
    I upvoted because I was looking for the same question, on Win32, but with gnutils installed -- so this worked perfectly for me. Man, pure-DOS is so crazy! – Michael Paulukonis Jul 23 '10 at 20:23
  • Upvoting - I needed a unix based solution to get rid of the awful eclipse .project and .settings files. Thanks! :D – gusterlover6 Feb 21 '12 at 19:14
  • 2
    Downvoting for wrong usage of `rm`+`xargs`+`find`. You should **always** use `-print0` and `NUL` termination in general in situations like these, otherwise it's just a disaster waiting to happen. – TC1 Mar 29 '12 at 17:51
  • @Meltemi It's been a while, but 1) the question asks for a generic solution, the '.svn' was just an example that was given and 2) *nix filenames can contain almost anything except for a `NULL` byte and a forward slash in them. Whitespace is the harmful thing for this, an example with a space char in the name would be having a dir named `foo bar` somewhere in the hierarchy and a dir named `bar` in the current dir. Then you search for `foo*`, it feeds `some/path/foo bar` to `rm`, which fails silently for the first and deletes `bar`. See greyfade's answer below or use `-delete` for GNU find. – TC1 Feb 15 '13 at 20:33
  • ok, starting to make sense. I needed to do something like this today and have no idea what the `-print0` or `-0` flags mean and having hard time finding documentation. on a Mac so many (most?) files/directories have space chars in them...so your comment piqued my curiosity. #funwithunix – Meltemi Feb 15 '13 at 20:48
  • FWIW- For my needs I settled on: `find . -name '.svn' -type d -exec rm -rf '{}' +` cryptic as that may be, it works... – Meltemi Feb 15 '13 at 21:21
  • @Meltemi The important part to take from all that is that your shell doesn't escape things for you, when you pipe stuff, the target simply reads things from stdin -- there's other troublesome cases, `rm` is just the most obvious bad one. In this case, when there's a space, it's the same as executing `rm -rf foo/bar baz` -- once you write it out, it's obvious. Since Darwin (Mac) is technically a Unix, you should be able to find documentation on your `find` in the `man`, type `man find` in a shell. P.S. Your case probably acts funny if there's an apostrophe in the filename. :) – TC1 Feb 16 '13 at 00:34
7

If your files are in subversion, then doing an export from the repository will give you a directory tree with the .svn files and any other cruft removed.

  • Just be aware that the exported version will not contain any uncommited changes you may have in your working directory (for that you will need to commit then export) – Jaysen Marais Jan 28 '12 at 03:33
6

Something like this may do the trick, but of course be careful with it!

find . -name ".svn" -exec rm -rf {} \;

Try something like this first to do a dry run:

find . -name ".*" -exec echo {} \;

Note that the empty braces get filled in with the file names and the escaped semicolon ends the command that is executed (starting after the "-exec").

dreeves
  • 26,430
  • 45
  • 154
  • 229
  • This worked perfectly for me. Thanks! I needed to remove all "CVS" directories, so I ran a find . -name "CVS" -exec rm -rf {} \; – cmcculloh Feb 24 '09 at 19:44
5

On *nix or Cygwin:

find -name .svn -print0 | xargs -0 rm -rf
greyfade
  • 24,948
  • 7
  • 64
  • 80
1

If you want to copy it without exporting and eliminating the .svn from the projects, you shold use the /EXCLUDE option from XCOPY.

Like this:

xcopy /S/Q/EXCLUDE:svn.excludelist [path_from] [path_to\]

Observe the "\" (backslash) on the [path_to]. It determines that it's an output directory, so, xcopy will not question if it's a file or a directory.

The svn.excludelist is a text file containing the patterns to ignore on copy separated by line.

For Example:

.svn
.svn\
\.svn\
.obj
.o
.lib
\src\

And so on...

mannysz
  • 951
  • 8
  • 19