Skip to content

Instantly share code, notes, and snippets.

@drewdomi
Created September 30, 2025 05:25
Show Gist options
  • Select an option

  • Save drewdomi/0ee95fced8b5a4c5f7bbfbc57b40650a to your computer and use it in GitHub Desktop.

Select an option

Save drewdomi/0ee95fced8b5a4c5f7bbfbc57b40650a to your computer and use it in GitHub Desktop.

File Copying in Linux: cp vs rsync (with Examples)

1. What Is the cp Command?

  • cp is a standard Unix/Linux command for copying files and directories locally.
  • It is simple and fast for everyday file management.

2. When to Use cp

  • Quick local copy—on the same machine or mounted filesystem.
  • Simple syntax—easy for basic file/directory tasks.
  • Not for remote copying—does not support copying to/from other computers directly.

3. Key Differences: cp vs rsync vs scp

Command Local Copy Remote Copy Resume/Partial Sparse Incremental/Sync
cp
rsync
scp

4. cp Examples

a. Copy a file locally

cp file1.txt file2.txt

b. Copy a file to a directory

cp file1.txt /home/drew/Backup/

c. Copy multiple files

cp file1.txt file2.txt /home/drew/Backup/

d. Copy a directory recursively

cp -r myfolder /home/drew/Backup/

e. Preserve file attributes (timestamp, permissions)

cp -p file1.txt /home/drew/Backup/

f. Copy sparse files efficiently (e.g., VM images)

cp --sparse=always Trisquel.qcow2 /home/drew/Backup/

5. Limitations of cp

  • No resume: Interrupted copies must restart.
  • No remote copy: Can’t copy across network directly.
  • No incremental sync: Always copies whole file, even if unchanged.

6. Using rsync for Advanced Copying

a. Local copy

rsync --progress file1.txt /home/drew/Backup/

b. Remote copy via SSH

rsync --progress file1.txt drew@morgan.local:/home/drew/Backup/

c. Resume partial transfer (if interrupted)

rsync --progress --partial --append-verify file1.txt drew@morgan.local:/home/drew/Backup/
  • --partial: Keep partially transferred files.
  • --append-verify: Resume and verify integrity.

d. Copy sparse files

rsync --sparse --progress Trisquel.qcow2 drew@morgan.local:/home/drew/Backup/

e. Copy entire directories

rsync -a --progress myfolder/ drew@morgan.local:/home/drew/Backup/myfolder/
  • -a (archive): Preserve permissions, timestamps, symlinks, etc.

7. Why Choose rsync?

  • Resume interrupted transfers.
  • Remote copying via SSH.
  • Efficient for large files and backups.
  • Supports sparse files, incremental updates, and verification.

8. Copying Remotely: Why Not cp?

  • cp cannot copy files directly to remote systems.
  • Use rsync (recommended) or scp for remote file transfer.

9. Summary Table

Task Use cp Use rsync
Simple local copy
Copy directory recursively ✅ (-r) ✅ (-a)
Preserve attributes ✅ (-p, -a) ✅ (-a)
Copy sparse files ✅ (--sparse) ✅ (--sparse)
Resume after interruption ✅ (--partial --append-verify)
Copy to/from remote ✅ (user@host:path)
Incremental backup/sync

10. Quick Reference: Commands

Local file copy (simple)

cp file.txt /path/to/destination/

Local directory copy (recursive)

cp -r mydir/ /path/to/destination/

Local sparse file copy

cp --sparse=always myimage.qcow2 /path/to/destination/

Remote copy (with resume)

rsync --partial --append-verify --progress file.img user@host:/path/to/destination/

Remote directory copy (with archive)

rsync -a --progress mydir/ user@host:/path/to/destination/

11. Final Tips

  • For quick, small local copies, use cp.
  • For large files, remote transfers, or interrupted jobs, use rsync.
  • If you only need a one-way remote copy (no resume), scp is an option, but rsync is safer and more flexible.

Feel free to copy, edit, or share this guide in your Gist or documentation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment