How to Use 'rsync' to Copy Files and Folders in Linux

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

Copy directories and files using the rsync file-transfer program for Linux. The command includes additional options beyond customary copy functions.

One of the more useful features of rsync is that it excludes files in a systematic way. As such, it backs up files intended for archiving while avoiding everything else.

Two file folders with an arrow pointing from one to the other.
 erhui1979 / Getty Images

Command 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]

Some commonly used option switches include:

  • -v, --verbose: Increase verbosity (provides more details about what the command is doing).
    • --info=FLAGS: Provides detailed informational messages.
    • --debug=FLAGS: Provides detailed debug messages.
    • --msgs2stderr: Special output handling for debugging.
  • -q, --quiet: Suppresses non-error messages.
    • --no-motd: Suppresses daemon-mode message of the day.
  • -c, --checksum: Skips files based on checksum, not mod-time and size.
  •  -r, --recursive: Browse into sub-directories for additional files.
  • -b, --backup: Make backups.
    • --backup-dir=DIR: Make backups into a matching directory hierarchy.
    • --suffix=SUFFIX: Adds suffix text to the end of backed up files.
  • -d, --dirs: Transfer only directories without browsing inside of them.

Command Examples

Use rsync with some of those options to fine-tune your backup strategy.

Selective Copying Based on File Type

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

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

Copying Files Based on Size

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. It only copies files smaller than the specified size.

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, too. In this example, rsync only copies files that are 30 MB or larger.

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

Use the --progress option to watch the process work up to 100 percent—handy when you're copying very large files.

Copy Entire Folders

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 the example above.

This command copies the entire folder and all of its contents to the new location.

Exclude Certain Files

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

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