Transferring files between systems is an essential Linux administration skill. It is required for system backups, file synchronization, and data migration. Linux provides several commands to perform these tasks efficiently. The most commonly used tools are rsync, scp, and nc.
1. rsync (Remote Synchronization) #
rsync is one of the most powerful tools for copying and synchronizing files or directories between local and remote systems. It transfers only the differences between files, which saves both bandwidth and time.
Syntax:
rsync [options] SOURCE DESTINATION
Common Options:
| Option | Description |
|---|---|
-a | Archive mode; preserves permissions, ownership, timestamps, and symbolic links |
-v | Verbose mode; displays detailed information during transfer |
-z | Compress file data during transfer |
--progress | Shows file transfer progress |
--backup | Creates backups of existing files before overwriting |
--delete | Removes files in the destination that no longer exist in the source |
Examples:
- Copy files locally:
rsync -avz --progress /home/user/docs/ /backup/docs/ - Copy files over SSH:
rsync -avz --progress user@192.168.1.10:/home/user/docs/ /backup/docs/ - Create a synchronized backup:
rsync -avz --progress --backup /data/ /backup/data/
This command keeps files synchronized between directories while maintaining backups of modified files.
2. scp (Secure Copy Protocol) #
scp is used for securely transferring files between systems over an SSH connection. It is simpler than rsync but less efficient because it always copies entire files rather than just the changes.
Syntax:
scp [options] SOURCE DESTINATION
Common Options:
| Option | Description |
|---|---|
-r | Recursively copy directories |
-v | Verbose mode to show progress |
-p | Preserve modification times and permissions |
-C | Enable compression during transfer |
Examples:
- Copy a file to a remote system:
scp file.txt user@192.168.1.10:/home/user/ - Copy a directory recursively:
scp -r /home/user/docs user@192.168.1.10:/backup/ - Copy a file from a remote system to the local machine:
scp user@192.168.1.10:/home/user/report.txt /home/localuser/
3. nc (Netcat) #
nc, or Netcat, is a lightweight and versatile tool that can be used to copy files between systems over a TCP connection. It does not use SSH, which makes it faster but less secure. Netcat is often used for quick file transfers within a trusted local network.
Example:
On the receiving system:
nc -l -p 1234 > backup.tar.gz
On the sending system:
cat backup.tar.gz | nc 192.168.1.10 1234
Explanation:
- The receiving system listens on port
1234and saves incoming data tobackup.tar.gz. - The sending system sends the file through a TCP connection to the receiver’s IP address.
Comparison Table #
| Tool | Protocol | Secure | Resume Support | Common Use |
|---|---|---|---|---|
| rsync | SSH/Local | Yes | Yes | Incremental backups and file synchronization |
| scp | SSH | Yes | No | Secure file transfers |
| nc (netcat) | TCP | No | Manual | Fast transfers on local networks |
Summary #
rsyncis ideal for backups and synchronization, as it transfers only changed data.scpis best for simple, secure file transfers using SSH.ncis useful for quick, manual file transfers in a trusted network environment.- Always verify backups after transfer using checksum tools such as
sha256sumormd5sum.