Efficient file management in Linux often requires more than simple copying, that’s where rsync excels. Unlike the basic cp command, rsync is designed for synchronisation and the intelligent transfer of changed parts of files.
It’s perfect for tasks like backups, mirroring directories, or transferring files between local and remote systems. In this guide, you’ll learn how to use rsync effectively, whether you’re copying files locally or across servers.
To copy files or directories using rsync, specify the source and destination paths. If you’re copying a folder, include the -r (recursive) flag to ensure all contents are included.
Example:
rsync -r /home/user/Documents /home/user/Backup/
This command copies the entire Documents directory into Backup, only transferring updated or new files to save time and resources.
Use the -a (archive) flag to retain file permissions, timestamps, symbolic links, and directory structure. This is more comprehensive than the cp -p option and is especially useful for accurate backups or migration tasks.
Example:
rsync -a /home/user/Documents /home/user/Backup/
This ensures the copied files match the source exactly in structure and metadata.
For large file transfers, it’s helpful to track the progress in real time. The -P flag combines –progress and –partial, showing detailed transfer stats and resuming partially transferred files if interrupted.
Example:
rsync -aP /home/user/Documents /home/user/Backup/
This provides a clear visual of data being transferred, including percentage complete and estimated time remaining.
A standout feature of rsync is its ability to copy files over SSH. This eliminates the need for manual uploads via FTP and allows seamless file synchronisation between systems.
Example:
rsync -a /home/user/Documents user@remote:/home/user/Backup/
This securely transfers the Documents directory to a remote server using SSH, maintaining file integrity and efficiency throughout the process.