rsync Command in Linux
Overview
Rsync (Remote Sync) is a powerful command-line tool in Linux that allows users to synchronize files and directories between local and remote systems or between two local directories. Rsync uses the delta-transfer algorithm and can transfer data over SSH, making it an efficient and secure choice for data synchronization.
Linux rsync Command Syntax
The syntax for the rsync command is as follows:
Where:
- options: A combination of flags and settings that control the behavior of the rsync command.
- source: The source file or directory to be synchronized.
- destination: The destination file or directory where the synchronized data will be stored.
rsync Command Options:
- -a, --archive: Archive mode; it is equivalent to -rlptgoD, which preserves symbolic links, permissions, timestamps, owner, group, and device files while recursively copying directories.
- -v, --verbose: Provides more detailed information about the synchronization process.
- -z, --compress: Compresses the data during transfer, reducing the amount of bandwidth used.
- -e, --rsh=COMMAND: Specifies the remote shell to use for data transfer, such as using SSH with -e 'ssh'.
Example Usages
-
Synchronize a local directory with a remote directory:
Output:
Explanation: This command synchronizes the /local/dir/ directory with the /remote/dir/ directory on the remote host, using archive mode (-a), verbose output (-v), and compression (-z).
-
Synchronize a remote directory with a local directory:
Output:
Explanation: This command synchronizes the /remote/dir/ directory on the remote host with the /local/dir/ directory, using archive mode (-a), verbose output (-v), and compression (-z).
Tips
-
Always include a trailing slash (/) at the end of the source directory to prevent rsync from creating an additional directory level at the destination.
-
Use the --dry-run option to simulate the synchronization process without making any actual changes.
Advanced Use Cases of rsync Command in Linux
-
Synchronize files while preserving hard links:
Output:
Explanation: This command synchronizes files and directories while preserving hard links (-H) in addition to using archive mode (-a), verbose output (-v), and compression (-z).
-
Synchronize files and exclude specific files or directories:
Output:
Explanation: This command synchronizes files and directories while excluding specific files or directories using the --exclude option, in addition to using archive mode (-a), verbose output (-v), and compression (-z).
-
Synchronize files with bandwidth limit:
Output:
Explanation: This command synchronizes files and directories with a bandwidth limit of 100 KB/s, using the --bwlimit option, in addition to using archive mode (-a), verbose output (-v), and compression (-z).
Conclusion
-
Rsync is a powerful and efficient tool for synchronizing files and directories.
-
It supports local and remote synchronization, as well as data compression and bandwidth limiting.
-
Rsync can transfer data securely using SSH and can preserve file attributes, such as permissions and timestamps.