The Power of Rsync: Efficient File Synchronization for Linux
saeedeldeeb
Posted on July 25, 2023
Rsync is a powerful utility for file synchronization in Linux that allows you to efficiently copy and synchronize files between local and remote locations. In this article, we'll explore some of the most commonly used rsync commands and their practical applications.
Basic File Copying
- Copy a File from Source to Destination:
To copy a single file from the source to the destination, use the following command:
$ rsync source/photos.zip destination
- Copy a Folder Recursively:
To copy a folder and its contents recursively, use the -r
option:
$ rsync -r source/ destination
- Copy Files with Timestamp Preservation:
The -a
option (archive mode) preserves file permissions, timestamps, and more during the copy operation:
$ rsync -a source/ destination
Monitoring Progress
- List Copying Progress:
For a more verbose output that lists the files being copied and the progress, use the -v
(verbose) option:
$ rsync -av source/ destination
Alternatively, use the --progress
option for a human-readable progress indicator:
$ rsync -a --progress source/ destination
- Human-Readable Progress:
For a human-readable progress display, add the -h
option:
$ rsync -ah --progress source/ destination
Handling Interruptions
- Partial Copies for Large Files:
When dealing with large files, use the --partial
option to allow for partial copies. This enables you to resume interrupted transfers:
$ rsync -ah --progress --partial source/ destination
Alternatively, the -P
option is a shorthand for --partial --progress
.
Advanced Synchronization
- Syncing with Deletion:
To sync files and delete items in the destination that no longer exist in the source, add the --delete
option:
$ rsync -ahP --delete source/ destination
- Remove Source Files after Successful Copy:
To automatically delete files from the source after a successful copy, use the --remove-source-files
option:
$ rsync -ahP --remove-source-files source/ destination
Note: This won't delete source folders, so an additional step is required, you can use $ find like -type d -empty -delete
Remote Synchronization
- Synchronizing with a Remote Server:
To sync files from a local machine to a remote server, use the following syntax:
$ rsync -ahP source root@your.ip:path/to/folder
-
Archiving Before Sync:
Archiving can be useful to preserve attributes and reduce the amount of data transferred. To archive a remote folder before syncing, use:
$ rsync -ahPz root@your.ip:path/to/folder source/
If the SSH port is non-default (e.g., 22), specify it using the
-e
option:
$ rsync -ahPze 'ssh -p 22' root@your.ip:path/to/folder source/
Rsync is a tool for efficiently managing file synchronization tasks on Linux systems. With its flexibility and numerous options, it empowers users to handle a wide range of data synchronization requirements with ease.
Posted on July 25, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 28, 2024