DEV Community

Cover image for Using Rsync to Transfer Files in Linux
Sanskriti Harmukh for Vultr

Posted on with Aashish Chaurasiya Originally published at docs.vultr.com

Using Rsync to Transfer Files in Linux

Rsync is a file transfer tool for transferring and synchronizing files across systems. It's commonly used for backups, file mirroring, and updating files between local directories or remote servers. Rsync reduces data transfer by copying only the differences between the source and destination files, and its speed, flexibility, and extensive options make it a popular choice for system administrators and developers. This guide installs Rsync and walks through using it to perform local and remote file transfers from your workstation. By the end, you'll have transferred and excluded files locally, and synchronized a directory to a remote machine over SSH.


Install Rsync

Rsync is available in the official repositories on most Linux distributions and is often pre-installed.

1. Install Rsync on Ubuntu and Debian using the APT package manager:

$ sudo apt install rsync
Enter fullscreen mode Exit fullscreen mode

2. Install Rsync on FreeBSD using the PKG package manager:

$ sudo pkg install rsync
Enter fullscreen mode Exit fullscreen mode

3. Install Rsync on Arch Linux using the Pacman package manager:

$ sudo pacman -S rsync
Enter fullscreen mode Exit fullscreen mode

4. Install Rsync on Fedora, AlmaLinux, Rocky Linux, and VzLinux using the DNF package manager:

$ sudo dnf install rsync
Enter fullscreen mode Exit fullscreen mode

5. Install Rsync on CentOS using the YUM package manager:

$ sudo yum install rsync
Enter fullscreen mode Exit fullscreen mode

Rsync Command Syntax

Rsync uses the following command syntax.

rsync [OPTION] SRC DEST
Enter fullscreen mode Exit fullscreen mode

Within the command:

  • [OPTION]: Represents the options or flags passed to the Rsync command.
  • SRC: The source files and directories you want to synchronize.
    • Use USER@HOST:SRC when transferring files remotely over SSH.
  • DEST: The destination where the files or directories will be synchronized.
    • Use USER@HOST:DEST when transferring files remotely over SSH.

Rsync Command Options

The following are the most common Rsync command options:

Option Long Form Short Form Description
Help --help -h Show Rsync command help
Verbosity --verbose -v Increase verbosity
--quiet -q Suppress non-error messages
File Selection --archive -a Archive mode
--recursive -r Recurse into directories
--relative -R Use relative path names
Updates --update -u Skip files that are newer on the receiver
--inplace Update destination files in-place
Backups --backup -b Make backups (see --suffix & --backup-dir)
--backup-dir=DIR Make backups into hierarchy based in DIR
Symlinks --links -l Copy symlinks as symlinks
--copy-links -L Transform symlink into referent file/dir
Permissions --perms -p Preserve permissions
Timestamps --times -t Preserve modification times
Deletion --delete Delete extraneous files from dest dirs
--delete-excluded Also delete excluded files from dest dirs
Simulation --dry-run -n Perform a trial run with no changes made
Remote Shell --rsh=COMMAND -e Specify the remote shell to use
Size Control --max-size=SIZE Don't transfer any file larger than SIZE
--min-size=SIZE Don't transfer any file smaller than SIZE
Partial Transfer --partial Keep incomplete transferred files
File Filtering --exclude=PATTERN Exclude files matching PATTERN
--include=PATTERN Don't exclude files matching PATTERN
Output Format --progress Show progress during transfer
-P Same as --partial --progress
--human-readable -h Output numbers in a human-readable format
Compression --compress -z Compress file data during the transfer
IP Version --ipv4 -4 Prefer IPv4
--ipv6 -6 Prefer IPv6

Transfer Files and Directories Using Rsync

Set up a test environment before running the Rsync commands below.

1. Create a directory named rsync-test to work with:

$ mkdir rsync-test
Enter fullscreen mode Exit fullscreen mode

2. Navigate into the newly created directory:

$ cd rsync-test
Enter fullscreen mode Exit fullscreen mode

3. Create three sample files:

$ touch file1 file2 file3
Enter fullscreen mode Exit fullscreen mode

Transfer Local Files

1. Copy file1, file2, and file3 to a new directory named dest. In Rsync, the last argument always represents the destination. Ensure the destination path is correctly specified to prevent accidental overwrites:

$ rsync file1 file2 file3 dest
Enter fullscreen mode Exit fullscreen mode
  • file1 file2 file3: Source files to copy.
  • dest: Destination directory where the files will be copied.

2. Verify that the files were copied successfully:

$ ls dest/
Enter fullscreen mode Exit fullscreen mode

Your output should be similar to the one below.

./dest/file1
./dest/file2
./dest/file3
Enter fullscreen mode Exit fullscreen mode

Transfer Local Directories

Rsync does not copy files inside a directory by default. Use the -a flag to enable recursive copying, which enables archive mode, preserving timestamps, permissions, group, owner, and symbolic links.

1. Copy the dest directory you created earlier:

$ rsync -a dest/ dest-copy
Enter fullscreen mode Exit fullscreen mode
  • -a: Enables archive mode to preserve file attributes while copying recursively.
  • dest/: The source directory to copy (trailing / means copy contents, not the directory itself. If you omit the trailing /, Rsync copies the entire directory instead of just its contents).
  • dest-copy: The destination directory.

2. Verify the copy operation:

$ ls dest-copy/
Enter fullscreen mode Exit fullscreen mode

Your output should be similar to the one below. A dest-copy directory is created with the same content as the dest directory.

./dest-copy/file1
./dest-copy/file2
./dest-copy/file3
Enter fullscreen mode Exit fullscreen mode

Exclude Files or Directories Using Rsync

To exclude specific files or directories from being copied, use the Rsync --exclude flag.

1. Copy the contents of the current directory, excluding file2 and all directories that start with dest:

$ rsync -a --exclude 'file2' --exclude 'dest*' . output
Enter fullscreen mode Exit fullscreen mode
  • -a: Enable archive mode.
  • --exclude 'file2': Exclude files and directories named file2.
  • --exclude 'dest*': Exclude files and directories that start with dest.
  • .: Specify the current directory as the source.
  • output: The destination directory.

2. Verify the copy operation:

$ ls output/
Enter fullscreen mode Exit fullscreen mode

Your output should be similar to the one below. Notice that the output directory contains only file1 and file3.

./output/file1
./output/file3
Enter fullscreen mode Exit fullscreen mode

Synchronize Data to a Remote Machine Using Rsync

1. Synchronize files and directories to a remote machine:

$ rsync -hazvP --delete --stats . user@192.0.2.1:synced_from_local
Enter fullscreen mode Exit fullscreen mode
  • -h: Output numbers in human-readable format.
  • -a: Enables archive mode for preserving metadata.
  • -z: Compresses data during transfer.
  • -v: Enables verbose output.
  • -P: Displays progress and resumes partial transfers.
  • --delete: Removes files from the destination that no longer exist in the source.
  • --stats: Provides a detailed summary of the transfer.
  • .: Specify the current directory as the source.
  • user@192.0.2.1:synced_from_local: Specify the remote machine as the destination. This consists of three parts:
    • user: The username to access the remote machine.
    • 192.0.2.1: The IP address of the remote machine.
    • synced_from_local: The destination path.

2. Log in to the remote machine using SSH:

$ ssh user@192.0.2.1
Enter fullscreen mode Exit fullscreen mode

3. Verify that the files have been synced:

$ ls synced_from_local/
Enter fullscreen mode Exit fullscreen mode

Output:

synced_from_local/dest-copy/file1
synced_from_local/dest-copy/file2
synced_from_local/dest-copy/file3
synced_from_local/dest/file1
synced_from_local/dest/file2
synced_from_local/dest/file3
synced_from_local/file1
synced_from_local/file2
synced_from_local/file3
synced_from_local/output/file1
synced_from_local/output/file3
Enter fullscreen mode Exit fullscreen mode

Warning on Using the --delete Rsync Option

Be cautious when using --delete, as it permanently removes files from the destination if they don't exist in the source. Always perform a dry run first:

$ rsync -a --delete --dry-run SRC DEST
Enter fullscreen mode Exit fullscreen mode

Next Steps

  • Schedule recurring Rsync jobs with cron to keep a backup directory continuously in sync.
  • Combine --exclude with a .rsync-filter file to manage more complex exclusion rules.
  • Use --dry-run as a standard habit before any sync that includes --delete.
  • Run man rsync for the full list of options and remote-shell configurations.

For the full guide with additional tips, visit the original article on Vultr Docs.

Top comments (0)