What to Know
- Copy based on file type: rsync /home/jon/Desktop/data/*.jpg /home/jon/Desktop/backupdata/
- Copy based on file size: rsync --max-size=2k /home/jon/Desktop/data/ /home/jon/Desktop/backupdata/
- Copy entire folders: rsync --recursive /home/jon/Desktop/data /home/jon/Desktop/data2
This article explains how to use the rsync file-transfer program for Linux to copy directories and files, and even exclude files in a systematic way. As such, it backs up files intended for archiving while avoiding everything else.
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]... [USER@]HOST:SRC [DEST]
rsync [OPTION]... [USER@]HOST::SRC [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.