Using Rsync to Transfer Files in Linux
Rsync is a file transfer tool for transferring and synchronizing files across systems. It's commonly used for backups, file mirroring, and updating files between local directories or remote servers. Rsync reduces data transfer by copying only the differences between the source and destination files, and its speed, flexibility, and extensive options make it a popular choice for system…
Rsync is a powerful file transfer tool used for synchronizing files across systems. It efficiently transfers only the differences between source and destination files, making it a popular choice among system administrators and developers.
To install Rsync on various Linux distributions, use the following commands:
- Ubuntu and Debian: `sudo apt install rsync`
- FreeBSD: `sudo pkg install rsync`
- Arch Linux: `sudo pacman -S rsync`
- Fedora, AlmaLinux, Rocky Linux, and VzLinux: `sudo dnf install rsync`
- CentOS: `sudo yum install rsync`
The basic syntax for the Rsync command is `rsync [OPTION] SRC DEST`. Here, `[OPTION]` represents the options or flags passed to the Rsync command, `SRC` is the source files and directories, and `DEST` is the destination where the files or directories will be synchronized.
Common Rsync options include:
- `--help` or `-h`: Display Rsync command help
- `--verbose` or `-v`: Increase verbosity
- `--quiet` or `-q`: Suppress non-error messages
- `--archive` or `-a`: Archive mode
- `--recursive` or `-r`: Recurse into directories
- `--relative` or `-R`: Use relative path names
- `--update` or `-u`: Skip files that are newer on the receiver
- `--inplace`: Update destination files in-place
- `--backup` or `-b`: Make backups
- `--backup-dir=DIR`: Make backups into a hierarchy based in DIR
- `--links` or `-l`: Copy symlinks as symlinks
- `--copy-links` or `-L`: Transform symlink into referent file/dir
- `--perms` or `-p`: Preserve permissions
- `--times` or `-t`: Preserve modification times
- `--delete`: Delete extraneous files from dest dirs
- `--delete-excluded`: Also delete excluded files from dest dirs
- `--dry-run` or `-n`: Perform a trial run with no changes made
- `--rsh=COMMAND` or `-e`: Specify the remote shell to use
- `--max-size=SIZE`: Don't transfer any file larger than SIZE
- `--min-size=SIZE`: Don't transfer any file smaller than SIZE
- `--partial`: Keep incomplete transferred files
- `--exclude=PATTERN` or `--include=PATTERN`: Exclude or don't exclude files matching the pattern
- `--progress`: Show progress during transfer
- `--human-readable` or `-h`: Output numbers in a human-readable format
- `--compress` or `-z`: Compress file data during transfer
- `--ipv4` or `-4`: Prefer IPv4
- `--ipv6` or `-6`: Prefer IPv6
To transfer local files, use the command `rsync file1 file2 file3 dest`. This command copies the specified files to the destination directory.
To transfer local directories, use the `-a` flag for archive mode. For example, `rsync -a dest/ dest-copy` copies the contents of the `dest` directory to a new directory called `dest-copy`.
To exclude specific files or directories from being copied, use the `--exclude` flag. For example, `rsync -a --exclude file2 --exclude dest* .` copies the contents of the current directory, excluding `file2` and all directories starting with `dest`.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.