GVS is now part of Acquia. Acquia logo

Fun at the Command Line: Add All Files in a Directory Tree to SVN (subversion)

Greg's picture

I often update code by adding files in a bunch of directories and then need to add all thosefiles to the svn repository. Here is the command line one-liner to get that done and an explanation:

svn stat | grep "^?" | awk '{print $2}' | xargs svn add

  1. svn stat tells you the status of all the files in the directory with a question mark at the beginning of the line for any files that svn doesn't currently know about (i.e. that aren't under revision control).
  2. The next step is to pipe that to a grep command that looks for lines beginning with a question mark so that we can add only those items to svn.
  3. Next, since the output of svn stat is a two column listing split by whitespace with the status indicator first and the full path to the file second, we use a simple awk pattern to print out just filename
  4. Finally, we use the xargs command to take the input and pass it on to the svn add command which schedules the files to be added to the repository.

I will typically run svn stat | less first and review the output to make sure that the command is only going to add things I want. I do the same thing just before any commit. If you need to undo the addition of some file prior to the commit, simply svn revert filename or use the recursive flag like svn revert --recursive path/to/directory/ if you are dealing with a directory.

Once you're happy with the changes, all you need to do is an actual svn commit so that the files will be permanently added to the repository.

Comments

Thanks Dear, it helped a lot

Thanks Dear,
it helped a lot in reducing what I used to do earlier.
its pretty cool
and you have explained it as well. that great !

Thanks a lot, Greg.

Thanks a lot, Greg.

Works great

I just used this to update some Drupal modules into SVN where there were several new files in various module folders, perfect, thanks.

worked like a charm

Thanks greg!

Thanks!

Just what I was looking for, super!

Save me alot of time!

Thanks for posting this

Hi, cool web page. I'm really

Hi, cool web page. I'm really delighted. Excellent job. The articles are superb. I also run a blog, stop sometimes to me. I hope you like it too.

Thanks a lot! I would like to

Thanks a lot!

I would like to advise everybody to make a clean before running this command to be sure that you are not adding temporary files (like .o files) to your repository.

I'm not sure it will be a good idea to run this command in case if items being added contains directories as well as ordinary files. But I'll try to check it more carefully.

One more note

Yeah, if you have a complex tree of directories and files, use svn import instead.

GVS projects

The Hyperlocal News installation profile is an "internal project" for some of the folks at GVS. Profiles are ways to bundle together Drupal, some contributed modules, and the configuration necessary to make the site actually do something cool. Users are presented with an wizard that sets up...

GVS is now part of Acquia.

Acquia logo

Contact Acquia if you are interested in a Drupal Support or help with any products GVS offered such as the Conference Organizing Distribution (COD).

We Wrote the Book On Drupal Security:

Cracking Drupal Book Cover