How to Use 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. Its speed, flexibility, and extensive options make it a popular choice for system administrators and developers.
This article explains how to install Rsync to transfer files in Linux. You will use Rsync to perform local and remote file transfers on your workstation.
Prerequisites
Before you begin, you need to:
- Have access to a Linux-based workstation as a non-root sudo user.
- Enough disk space for large file transfers.
Install Rsync
Rsync is available in the official repositories on most Linux distributions and is often pre-installed. Install Rsync on your system using the commands below, based on your distribution.
Install Rsync using the APT package manager:
console$ sudo apt install rsync
Install Rsync using the PKG package manager:
console$ sudo pkg install rsync
Install Rsync using the Pacman package manager:
console$ sudo pacman -S rsync
Install Rsync using the DNF package manager:
console$ sudo dnf install rsync
Install Rsync using the YUM package manager:
console$ sudo yum install rsync
Rsync Command Syntax
Rsync uses the following command syntax.
```
rsync [OPTION] SRC DEST
```
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.
- Use
DEST
: The destination where the files or directories will be synchronized.- Use
USER@HOST:DEST
when transferring files remotely over SSH.
- Use
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
This section provides step-by-step instructions on how to use Rsync to transfer files and directories. Follow the steps below to set up a test environment and execute Rsync commands.
Create a directory named
rsync-test
to work with.console$ mkdir rsync-test
Navigate into the newly created directory.
console$ cd rsync-test
Create three sample files.
console$ touch file1 file2 file3
Transfer Local Files
The following steps show how to transfer files between directories using Rsync.
Copy
file1
,file2
, andfile3
to a new directory nameddest
. In Rsync, the last argument always represents the destination. Ensure the destination path is correctly specified to prevent accidental overwrites.console$ rsync file1 file2 file3 dest
file1 file2 file3
: Source files to copy.dest
: Destination directory where the files will be copied.
Verify that the files were copied successfully using
ls
.console$ ls dest/
Your output should be similar to the one below.
./dest/file1 ./dest/file2 ./dest/file3
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. Follow the steps below to copy local directories recursively using the Rsync command.
Copy the
dest
directory you created earlier.console$ rsync -a dest/ dest-copy
-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.
Verify the copy operation using the
ls
command.console$ ls dest-copy/
Your output should be similar to the one below. A
dest-copy
directory is created with the same content as thedest
directory../dest-copy/file1 ./dest-copy/file2 ./dest-copy/file3
Exclude Files or Directories Using Rsync
To exclude specific files or directories from being copied use the Rsync --exclude
flag.
Copy the contents of the current directory, excluding
file2
and all directories that start withdest
.console$ rsync -a --exclude 'file2' --exclude 'dest*' . output
-a
: Enable archive mode.--exclude 'file2'
: Exclude files and directories namedfile2
.--exclude 'dest*'
: Exclude files and directories that starts withdest
..
: Specify the current directory as the source.output
: The destination directory.
Verify the copy operation using the
ls
command.console$ ls output/
Your output should be similar to the one below. Notice that the
output
directory contains onlyfile1
andfile3
../output/file1 ./output/file3
Synchronize Data to a Remote Machine Using Rsync
Follow the steps below to synchronize your files and directories to a remote machine using SSH.
Synchronize files and directories to a remote machine.
console$ rsync -hazvP --delete --stats . user@192.0.2.1:synced_from_local
-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 consist 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.
Log in to the remote machine using SSH.
console$ ssh user@192.0.2.1
Verify that the files have been synced.
console$ ls synced_from_local/
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
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
Conclusion
You have installed Rsync used it to synchronize data between local and remote machines. You also used common Rsync options like filtering files with --exclude
and copying directories recursively. Always double-check your Rsync commands, especially when using options like --delete. Run the man rsync
command for more information and command options.