How to Use rsync Command in Linux: Basic Syntax and Examples
The
rsync command in Linux copies and synchronizes files and directories efficiently between locations. Use rsync [options] source destination to transfer files locally or remotely with options like -a for archive mode and -v for verbose output.Syntax
The basic syntax of rsync is:
rsync [options] source destination
source is the file or directory you want to copy.
destination is where you want to copy the files to.
options modify how the copying works, like preserving file permissions or showing progress.
bash
rsync [options] source destination
Example
This example copies the directory myfolder to /backup/ preserving all file attributes and showing progress:
bash
rsync -av --progress myfolder/ /backup/myfolder/
Output
sending incremental file list
file1.txt
file2.txt
sent 1.23M bytes received 45 bytes 123.45K bytes/sec
total size is 1.20M speedup is 1.00
Common Pitfalls
Common mistakes include:
- Forgetting the trailing slash on source directory which changes behavior.
- Not using
-aoption to preserve file permissions and timestamps. - Using
rsyncwithout-vor--progresscan hide what is happening.
Example of difference:
bash
rsync -av myfolder /backup/ rsync -av myfolder/ /backup/
Quick Reference
| Option | Description |
|---|---|
| -a | Archive mode; preserves permissions, timestamps, symbolic links, and more |
| -v | Verbose output; shows details of the transfer |
| --progress | Shows progress during transfer |
| -z | Compress file data during transfer |
| -r | Recursively copy directories |
| --delete | Delete files in destination not present in source |
Key Takeaways
Use
rsync -av source destination to copy files with permissions and timestamps preserved.A trailing slash on the source directory changes whether the directory itself or its contents are copied.
Add
--progress to see transfer progress in real time.Use
--delete carefully to keep destination in sync by removing extra files.Rsync works locally and over network by specifying remote paths like
user@host:/path.