Whether you are migrating a website to a new server, backing up critical user data to a Network Attached Storage (NAS) drive, or simply copying a massive directory of photos from your laptop to a USB drive, relying on the basic cp (copy) command is highly inefficient. If a transfer is interrupted halfway through, cp forces you to start entirely from scratch. To perform intelligent, robust file synchronization, Linux system administrators rely on the rsync command. rsync is famous for its “delta-transfer algorithm,” meaning it only copies the precise pieces of a file that have changed, drastically reducing network bandwidth and transfer times.
Basic Local Synchronization
You can use rsync to perfectly mirror a folder from one location on your hard drive to another (e.g., backing up a project folder to an external hard drive).
- Open your Linux terminal.
- The basic syntax requires the
-a(archive) flag. This is a critical flag that tellsrsyncto copy directories recursively while perfectly preserving all file permissions, ownerships, and timestamps.rsync -a /home/user/Documents /mnt/usb_drive/Backup/ - Press Enter.
Crucial Syntax Warning (The Trailing Slash):
The way rsync handles trailing slashes on the source directory is a common trap for beginners.
- If you write
/home/user/Documents(no trailing slash),rsyncwill copy the entire Documents folder itself, placing it inside the Backup folder (resulting in/mnt/usb_drive/Backup/Documents/file.txt). - If you write
/home/user/Documents/(with a trailing slash),rsyncwill only copy the contents of the Documents folder, dumping the loose files directly into the Backup folder (resulting in/mnt/usb_drive/Backup/file.txt).
Adding Verbosity and Progress Bars
By default, rsync operates completely silently. If you are transferring 50GB of data, staring at a blank terminal can be anxiety-inducing. You should almost always add the -v (verbose) flag to see a list of files being copied, and the --progress flag to see a real-time progress bar, transfer speed, and estimated time remaining for each file.
rsync -av --progress /home/user/Documents/ /mnt/usb_drive/Backup/
Synchronizing with a Remote Server over SSH
The true power of rsync lies in its ability to synchronize files across a network securely. It uses SSH to establish an encrypted tunnel before transferring the data.
To push your local website files to a remote web server, you use the standard SSH connection string as your destination:
rsync -av --progress /var/www/html/ [email protected]:/var/www/remote_html/
Because it uses the delta-transfer algorithm, if you run this exact command tomorrow after editing only a single line of CSS in a 10MB stylesheet, rsync will not re-upload the entire 10MB file. It will analyze the file, realize only a few bytes changed, and transmit only those modified bytes over the network, completing the backup in milliseconds.
Using the –delete Flag for Perfect Mirroring
By default, rsync only adds or updates files. If you delete a photo from your local Documents folder, and then run your backup script, that photo will not be deleted from the Backup drive. Over time, your Backup drive will become bloated with thousands of old, deleted files.
If you want the destination to be a perfect, exact mirror of the source, you must append the --delete flag. This tells rsync to permanently delete any files on the destination drive that no longer exist on the source drive.
rsync -av --delete /home/user/Documents /mnt/usb_drive/Backup/
Warning: Use this flag with extreme caution. If you accidentally swap your source and destination paths, the --delete flag will instantly wipe your original files.