rsync only files matching pattern,
a Subversion application

Rsync is just great to synchronize files between two locations, wether they are two directories on your local hard drive or two shares located on servers thousands of kilometers apart. In addition, with --include and --exclude patterns, you can get rsync to transfer just the files you want.

rsync -avz --include "*_small.jpg" --exclude "*.jpg" dir1/ dir2/

will not transfer JPEG files between dir1 and dir2 (and their sub-directories) unless their name ends with _small.jpg. This way you can transfer only the reduced version of your pictures to you website for example.


Now, we want to transfer only JPEG files from dir1 to dir2, excluding all other files (like RAW, of TIFF originals for example). In this case we use:

rsync -avz --include "*/" --include "*.jpg" --exclude "*" dir1/ dir2/

Each file is matched with the include/exclude arguments in sequential order. Here, rsync starts by looking into directories (--include "*/"). This is what we want because we want it to be recursive to be useful. Then if the file is actually a file (i.e. not a directory) and has a jpg extension it is also included. In the end the file is anything else that a jpeg file or a directory (--exclude "*"), it gets excluded. Et voilà!

Now let us apply this to Subversion, which is just great to manage the history of source code, data files or even pictures (does: “Revert to original” ring a bell?). Subversion stores all its information in .svn directories in your working copy.

Let us imagine that you have this working copy full of uncommitted modifications that you safely backed up somewhere… but in the backup process the .svn directories got lost (because of their peculiar name which makes them invisible for example). Well, you’re out of luck. Just suppose now that, in addition, you had a hard drive failure. Yes, you’re totally screwed (Set reminder: “One should never keep too many uncommitted changes in a working copy”).

So you resign and check out a new working copy, wondering how you’ll manage to get all your nice changes back in it (and fear the diff nightmare ahead of you). Fear no more. Basically to be up and running again you need .svn directories back in your modified working copy but not the rest of the files (remember, you want to keep your changes). And there comes rsync: from your just-checked-out working copy you can get the .svn directories you lack in your old-and-modified working copy. Here is the syntax:

rsync -avz --include "*/" --include "**.svn**" --exclude "*"
      just-checked-out/ old-and-modified/

NB: In addition, if your working copy was out of date (i.e. not at HEAD) you need to look through the logs on the svn server and remember at which particular revision your working copy was. You need to get the .svn directories from that particular revision and not from HEAD.

1 Responses to “rsync only files matching pattern,
a Subversion application”


  • hey, thank you for the –include “*/” technique, i was atleast not able to understand that from the man pages, it helped !
    - Anoop

Leave a Reply