How to Use rsync to Copy Files and Folders in Linux

Use these rsync examples to learn how to copy from the command line

erhui1979 / Getty Images

rsync is a file transfer program for Linux that lets you copy directories and files with a simple command, one that includes additional options past the traditional copy function.

One of the useful features of rsync is that when you use it to copy directories from the command line, you can exclude files in a systematic way. That way, if you're using rsync to make file backups, you can have it only back up the files you truly want to archive, while avoiding everything else.

rsync Syntax

Using the rsync command properly requires that you follow the correct syntax:

rsync [OPTION]... [SRC]... [DEST]
rsync [OPTION]... [SRC]... [USER@]HOST:DEST
rsync [OPTION]... [SRC]... [USER@]HOST::DEST
rsync [OPTION]... [SRC]... rsync://[USER@]HOST[:PORT]/DEST
rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]

The option space provided above can be filled with a number of things. See the OPTIONS SUMMARY section of the rsync documentation for a full list, but here are a few examples:

  • -v, --verbose: Increase verbosity
    • --info=FLAGS: Fine-grained informational verbosity
    • --debug=FLAGS: Fine-grained debug verbosity
    • --msgs2stderr: Special output handling for debugging
  • -q, --quiet: Suppress non-error messages
    • --no-motd: Suppress daemon-mode MOTD
  • -c, --checksum: Skip based on checksum, not mod-time and size
  •  -r, --recursive: Recurse into directories
  • -b, --backup: Make backups
    • --backup-dir=DIR: Make backups into a hierarchy based in DIR
    • --suffix=SUFFIX: Backup suffix
  • -d, --dirs: Transfer directories without recursing

rsync Examples

Here are just a few examples of how to use rsync with some of those options:

Tip: In each of these examples, the bold text cannot be changed because it's part of the command. As you can tell, the folder paths and other options are custom to our specific examples, so they're going to be different when you use them.

rsync /home/jon/Desktop/data/*.jpg /home/jon/Desktop/backupdata/

In this above example, all of the JPG files from the /data/ folder are copied to the /backupdata/ folder on the user Jon's Desktop folder.

rsync --max-size=2k /home/jon/Desktop/data/ /home/jon/Desktop/backupdata/

This rsync example is a bit more complicated since it's set up to not copy files if they're larger than 2,048 KB. That is, to only copy files smaller than the stated size. You can use k, m, or g to indicate kilobytes, megabytes, and gigabytes in the 1,024 multiplier, or kb, mb, or gb to use 1,000.

rsync --min-size=30mb /home/jon/Desktop/data/ /home/jon/Desktop/backupdata/

The same can be done for --min-size, as you see above. In this example, rsync will only copy files that are 30 MB or larger.

rsync --min-size=30mb --progress /home/jon/Desktop/data/ /home/jon/Desktop/backupdata/

When you're using rsync to copy files that are pretty large, like 30 MB and bigger, and especially when there's a number of them, you might want to see the progress of the copy function instead of assuming the command has become frozen. In those cases, use the --progress option to watch the process reach 100%.

rsync --recursive /home/jon/Desktop/data /home/jon/Desktop/data2

The --recursive option provides an easy way to copy an entire folder to a different location, like to the /data2/ folder in our example.

rsync -r --exclude="*.deb" /home/jon/Desktop/data /home/jon/Desktop/backupdata​

You can also copy a whole folder but exclude files of a certain file extension, such as DEB files in this example above. This time, the whole /data/ folder is copied to /backupdata/ like in the previous example, but all DEB files are excluded from the copy.