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. The command includes additional options beyond the traditional copy function.

One of the more 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 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 parameters. See the OPTIONS SUMMARY section of the rsync documentation for a full list, but here are a few common examples:

  • -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 (MOTD).
  • -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.

rsync Examples

The following are a few examples of how to use rsync with some of those options.

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.

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. 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 very large, like 30 MB and bigger — especially when there's a number of them — you might want to see the progress of the copy function so you know the command is processing. In those cases, use the --progress option to watch the process work up to 100%.

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 will copy 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​

You can also copy a whole folder but exclude files of a certain file extension, such as DEB files in this example above.

In this example, the whole /data/ folder is copied to /backupdata/ like in the previous example, but all DEB files are excluded from the copy.