Rsync include/exclude quirks

November 22nd, 2010 by Mark Lewis

Rsync is a very useful application to mirror files from one server to another. This is particularly helpful in web hosting situations where you test a site prior to launch or update then have to copy files to one or more production servers.

Typically, one would rsync the entire module. Rsync, however, does provide directives to include and exclude files. These directives take on filesystem wildcard arguments. You can have multiple include/exclude directives, the easiest way is using a file listing the directives. Rsync performs a pattern match in the order the directives are listed for each file in the module. Itwill execute the first directive it matches, include or exclude. Excluding files is simple as, by default, rsync will sync the entire module. The exclude directive(s) just list which items to skip over.

rsync -rav --exclude=*.txt user@source::modulename /path/to/local/destination

This would leave out all .txt files from syncing. Conversely, if you want to include only certain files, the directives you need are not as apparent. Again, by default, rsync includes all files. Hence if you’re looking to only include one files, a simple include directive will do work:

rsync -rav --include=*.txt user@source::modulename /path/to/local/destination

You would expect only .txt files to sync. However, rsync mirrors everything by default. If you only want .txt files, you need a default exclude directive:

rsync -rav --include=*.txt --exclude=* user@source::modulename /path/to/local/destination

Another issue is subdirectories. If you want to only include a file nested down a level, images/pic.gif, you have to also include the parent directory separately:

rsync -rav --include=images/ --include=images/pic.gif --exclude=* user@source::modulename /path/to/local/destination

Lastly, if you want to include a directory and everything under it, your wildcard must be a double asterisk (**):

rsync -rav --include=images/** --exclude=* user@source::modulename /path/to/local/destination

The easiest thing to do for specifically including files is to start the include/exclude list with an include for all directories and end with a catchall exclude:

rsync -rav --include=*/ [set of includes] --exclude=* user@source::modulename /path/to/local/destination

  • Jb

    FYI The last example doesn’t work. It creates empty directories and includes the filenames.

  • Anonymous

    Hi Jb,

    Do you have an example?  Yes, the command does create empty directories, but only copies the files you specify.  The last example does this.  The purpose of RSYNC is to keep two identical file structures in sync, hence, if the directory structure on the destination does not exist, it will be created.

  • confiq

    The last command works!

    But what “–include=*/” command means?

  • Pingback: Rsync ile Senkranizasyon işlemleri | ÇaylakS BloG

  • Pingback: Rsync ile Senkronizasyon işlemleri | ÇaylakS BloG