How to Copy Directories and Files With the rsync Command on Linux

Use the Linux rsync command to copy folders/files from the command line

Serious female computer programmer working at laptop in meeting
 Hero Images/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 copy directories, 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 Examples

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 page for a full list.

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

Tip: In all 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 example of rsync is a bit more complicated since it is 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 copying 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.

Was this page helpful?