Previous Section  < Day Day Up >  Next Section

Recipe 16.2. Using rsync for Local File Transfers and Synchronization

16.2.1 Problem

You need to keep file trees synchronized on your workstation because your workstation is a staging ground for web directories, image galleries, source code trees, or other complex collections of files. So you use a working directory for editing files, and then copy the finished files to a separate upload directory. You would like something faster and more intelligent than cp, because cp takes a long time and you lose track of what needs to be copied.

16.2.2 Solution

Use rsync to keep file trees synchronized. rsync copies only changes, so it speeds up file copying, and it tracks changes inside files and in file trees.

Be sure you have the latest version, to get all the bugfixes and security patches. You definitely want 2.6 or later:

$ rsync —version

rsync  version 2.6.2  protocol version 26


This command copies a directory of web files to a staging directory that will later be uploaded to the web server:

$ rsync -av —stats  /home/pearlbear/webs  ~/web_upload

building file list ... done

...<all the files being copied fly by>...

Number of files: 254

Number of files transferred: 235

Total file size: 8923014 bytes

Total transferred file size: 8923014 bytes

Literal data: 8923014 bytes

Matched data: 0 bytes

File list size: 6490

Total bytes written: 8939848

Total bytes read: 3780

To verify the transfer, use:

$ ls ~/web_upload


There is a subtle gotcha here: /home/pearlbear/images transfers the contents of /images and the images directory itself. Adding a trailing slash (/home/pearlbear/images/) copies only the contents of the /images directory, but not the directory itself. The trailing slash only matters on the source directory; it makes no difference on the destination directory.

If more files are added to /home/pearlbear/webs, or any existing files are changed, simply re-run the same command. rsync will transfer only the changes.

You can specify more than one source directory to be copied:

$ rsync -av  /home/pearlbear/webs /home/pearlbear/web_images  ~/web_upload

You can also test your rsync command first with the —dry-run option:

$ rsync -av  —dry-run  /home/pearlbear/webs /home/pearlbear/web_images  ~/web_upload

16.2.3 Discussion

If any files are deleted from the source directory, rsync will not delete them from the destination directory unless you explicitly tell it to with the —delete flag:

$ rsync -av —delete /home/pearlbear/webs  ~/web_upload

The -av flags mean archive, which retains file permissions and ownership, and verbose output.

Be careful using the —delete flag; if you accidentally delete from the rsync archive a file that you wanted to keep, it's gone for good. Be especially careful with your filepaths, because —delete will happily erase an entire directory or file tree.

rsync is a great tool for keeping local archives synchronized. When you're authoring web pages, writing code, assembling files to burn to CD, or managing any kind of large collection of files, it's a real timesaver to have rsync track and synchronize all the changes.

Installation is simple—both RPMs and Debian packages are named rsync, and you only need the single package. The sources are statically linked, so there is no danger of dependency problems, however you elect to install it.

16.2.4 See Also

  • rsync(1)

    Previous Section  < Day Day Up >  Next Section