Copying files from one directory to another using the standard cp command is fine for small, everyday tasks. However, when you need to backup a massive directory containing hundreds of gigabytes of data, or sync files to a remote server, cp is inefficient. If the transfer is interrupted, cp forces you to start over from the beginning. The professional solution for file synchronization in Linux is rsync.
What makes rsync so powerful?
rsync (Remote Sync) is famous for its delta-transfer algorithm. When you tell rsync to copy a folder, it first compares the source folder with the destination folder. If a file already exists at the destination, rsync does not waste time copying the entire file again. It only copies the specific pieces of data (the delta) that have changed. This makes subsequent backups nearly instantaneous and drastically reduces bandwidth when transferring files over a network.
Basic Syntax for Local Syncing
To use rsync to copy files from one folder to another on the same machine, the syntax is straightforward:
rsync -avz /path/to/source /path/to/destination
Let’s break down those critical flags (-avz), which are used in almost every rsync command:
- -a (Archive): This is the most important flag. It tells
rsyncto copy everything recursively (all subdirectories) and, crucially, to preserve all file permissions, ownership (users and groups), timestamps, and symbolic links exactly as they are in the source. - -v (Verbose): This tells
rsyncto print out exactly what it is doing, listing every file as it transfers, so you are not staring at a blank terminal wondering if it is working. - -z (Compress): This compresses the file data during the transfer. While not strictly necessary for local copies, it drastically speeds up transfers when syncing over a network.
The Critical Importance of the Trailing Slash
The most common mistake beginners make with rsync involves the trailing slash (/) on the source directory.
Example 1: Without a trailing slash
rsync -avz /var/www/html /backup/
If you run this, rsync will copy the entire folder itself. Inside your backup directory, you will find a new folder named html containing all the files.
Example 2: With a trailing slash
rsync -avz /var/www/html/ /backup/
By adding the slash, you are telling rsync to copy the contents of the folder, not the folder itself. Inside your backup directory, you will find all the raw files (index.php, images, etc.) spilled directly into the /backup/ folder without an enclosing html folder.
Using the –delete Flag for True Mirroring
By default, if you delete a file in your source folder and run rsync, the file will still exist in the destination backup folder. rsync only adds and updates; it does not delete.
If you want the destination to be a perfect, 1:1 mirror of the source, you must use the --delete flag. This tells rsync to delete any files in the destination that no longer exist in the source.
rsync -avz --delete /var/www/html/ /backup/
Warning: Use the --delete flag with extreme caution. If you accidentally switch the source and destination paths, rsync will instantly delete all the files in your source directory to make it match the empty destination directory.