
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.
Before you begin, you need to:
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:
Rsync uses the following command syntax.
Within the command:
[OPTION]: Represents the options or flags passed to the Rsync command.SRC: The source files and directories you want to synchronize.USER@HOST:SRC when transferring files remotely over SSH.DEST: The destination where the files or directories will be synchronized.USER@HOST:DEST when transferring files remotely over SSH.The following are the most common Rsync command options:
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.
Navigate into the newly created directory.
Create three sample files.
The following steps show how to transfer files between directories using Rsync.
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.
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.
Your output should be similar to the one below.
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.
-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.
Your output should be similar to the one below. A dest-copy directory is created with the same content as the dest directory.
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 with dest.
-a: Enable archive mode.--exclude 'file2': Exclude files and directories named file2.--exclude 'dest*': Exclude files and directories that starts with dest..: Specify the current directory as the source.output: The destination directory.Verify the copy operation using the ls command.
Your output should be similar to the one below. Notice that the output directory contains only file1 and file3.
Follow the steps below to synchronize your files and directories to a remote machine using SSH.
Synchronize files and directories to a remote machine.
-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.
Verify that the files have been synced.
Output:
--delete Rsync OptionBe 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:
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.
0 Comments
Be the first to comment and share your perspective with the community.