9

I want to remove all the versioned files from my repository, but KEEP the versioned directory structure. Obviously I want to leave all the .svn directories untouched.

In other words, I want to completely empty a working copy's directory structure WITHOUT harming the directory structure itself.

For example, removing the files from this structure:

dir/
    .svn/
        [files]
    svsubdir1/
        file1
        .svn/
            [files]
    subdir2/
        file2
        file3
        file4
        .svn/
            [files]
        subsubdir1/
            file5
            .svn/
                [files]

Should result in:

dir/
    .svn/
        [files]
    svsubdir1/
        .svn/
            [files]
    subdir2/
        .svn/
            [files]
        subsubdir1/
            .svn/
                [files]

I'm looking for some sort of find command or something to accomplish this, and I'm having trouble constructing the command. Thanks for the help!

Brian Lacy
  • 1,093
  • 4
  • 15
  • 23
  • The reason I'm doing this is that I have a web app which has been developed in "versions" but has never been under revision control before. I want to put it under SVN revision control now, and for ease of historical reference, etc., I'm trying to replicate a progression of four versions from four different "backup" folders containing progressively older versions. Once the files are deleted, I can copy the newer files into the hierarchy to simulate the newer revision. Not sure if all that makes sense. :) – Brian Lacy Oct 26 '10 at 19:59
  • 1
    It is worth noting that SVN since version 1.7 (October 2011) no longer has multiple ".svn" directories, but just one .svn dir at the top level. See http://subversion.apache.org/docs/release-notes/1.7.html#wc-ng – Rich Jan 04 '18 at 15:24

2 Answers2

10
find dir/ -path '*/.svn' -prune -o -type f -print

should fit the bill (mostly comes from the find manpage for -path). Pipe it to less and check it out. What it does is first find (path ends in .svn and don't recurse into (prune) this directory) or (if it's a file, print it).

If it looks good, change it to

find dir/ -path '*/.svn' -prune -o -type f -exec rm {} +

The + version sticks all of the files together into one rm command. If you're paranoid, keep a backup of the tree (cp -a dir/ otherdir/) first.

DerfK
  • 19,313
  • 2
  • 35
  • 51
  • Perfect solution, that's exactly what I needed! Thanks Derf! – Brian Lacy Oct 26 '10 at 19:53
  • In place of '-exec rm {} +' you can also use the more intuitive '-delete' option. – Tim Bielawa Oct 26 '10 at 21:05
  • 1
    @Tim According to the find manpage, `-delete` is depth-first and therefore conflicts with `-prune` because the files inside the folder would be checked before the folder itself was examined. The other solution (using `-not -path ...` rather than `-path ... -prune` can use the -delete option. – DerfK Oct 26 '10 at 21:56
  • Nice catch! – Tim Bielawa Oct 26 '10 at 22:05
  • I've used this trick (or similar) numerous times over the years in a variety of scenarios. So thanks again, @DerfK. :) – Brian Lacy Jan 29 '18 at 19:33
2
find . -not -path "*/.svn/*" -and -type f -and -exec /bin/rm '{}' \;

Ought to do the trick.

mark
  • 2,325
  • 14
  • 10