Ashari Abidin's Developer Docs

Ubuntu Copy Files Complete Guide

๐Ÿ“ cp ยท rsync // Ubuntu power copy

Master file & directory copying from terminal โ€” from basic duplication to advanced sync
โšก Ubuntu 24.04 LTS ยท Linux

๐Ÿ“„ 1. Basic file copy using cp

The cp command (copy) is the standard tool for duplicating files and directories. Simple and efficient for everyday tasks.

cp source_file destination_file
cp file1.txt backup_file1.txt

โœ… Creates backup_file1.txt as an exact copy in the current directory.

๐Ÿ“ 2. Copy a file to another directory

cp file1.txt /home/user/Documents/

Copies file1.txt directly into the Documents folder.

๐Ÿ“š 3. Copy multiple files at once

cp file1.txt file2.txt file3.txt /home/user/Documents/

All specified files are copied into the target directory in one command.

๐Ÿ—‚๏ธ 4. Copy an entire directory (recursive)

Use the -r flag to copy directories and all nested content:

cp -r source_folder /home/user/Documents/
๐Ÿ” -r means recursive. Without it, cp returns an error when target is a directory.

โš™๏ธ 5. Force overwrite & interactive confirmation

-f (force) โ€“ overwrites existing files without prompt:

cp -f file1.txt /destination/

-i (interactive) โ€“ asks before overwriting:

cp -i file1.txt /destination/

Output: overwrite '/destination/file1.txt'? โ†’ prevents accidental data loss.

๐Ÿ“ข 6. Display copy process verbosely

cp -v file1.txt /destination/

Example output: 'file1.txt' -> '/destination/file1.txt' โ€” ideal for monitoring & scripts.

๐Ÿ•’ 7. Preserve permissions & timestamps (-p)

cp -p file1.txt /destination/

Preserves file permissions, ownership, and timestamps โ€” crucial for backups and server migrations.

๐ŸŽฏ 8. Copy all contents of a directory (not parent)

cp -r /source_folder/* /target_folder/

* wildcard copies inner files/folders only, without the source folder itself.

๐Ÿš€ 9. Advanced copying using rsync

rsync is the powerhouse for backups, synchronization, incremental transfers, and remote operations.

rsync -avh source/ destination/
rsync -avh /var/www/html/ /backup/html/

Efficient, resumable, and bandwidth-friendly.

๐Ÿง  10. Understanding rsync parameters

  • -a (archive): preserves permissions, ownership, symlinks, timestamps + recursive copy.
  • -v (verbose): detailed operation logs.
  • -h (human-readable): shows sizes in KB/MB/GB.
rsync -avh --progress /data/ /backup/data/

โš–๏ธ 11. Why rsync outperforms cp for large transfers

Featurecprsync
Basic file copyโœ… Yesโœ… Yes
Recursive copyโœ… Yes (-r)โœ… Yes (-a or -r)
Progress visibilityโš ๏ธ Limited (with -v)โœ… Excellent (--progress)
Incremental syncโŒ Noโœ… Yes (delta transfer)
Resume interrupted transferโŒ Noโœ… Yes (partial & --append)
Remote server support๐ŸŸก via scp onlyโœ… Native over SSH
Efficiency for large backupsโŒ Limitedโœ… Excellent

๐ŸŒ 12. Copy files between servers using rsync

rsync -avh /local/folder/ user@192.168.1.10:/remote/folder/

Securely copies over SSH. rsync only transfers changed blocks โ†’ super fast for incremental backups.

๐Ÿ’ก Tip: Add --delete to remove files in destination that no longer exist in source โ€” perfect for mirroring.

๐Ÿ› ๏ธ 13. Common real-world examples

๐Ÿ“Œ Backup Nginx config:

cp -p /etc/nginx/nginx.conf /backup/nginx.conf

๐Ÿ—„๏ธ Website data backup:

rsync -avh /var/www/html/ /backup/html/

๐Ÿ“ Duplicate project folder:

cp -r myproject myproject_backup

๐Ÿ”„ Incremental server sync:

rsync -avh --delete /data/ user@backup-server:/backup/data/

๐Ÿ“Œ 14. Recommended best practices

โœ”๏ธ Use cp when: simple local copies, small files, quick everyday ops.
โœ”๏ธ Use rsync when: backing up servers, synchronizing folders, large datasets, network transfers, preserving metadata incrementally.
  • Always test rsync with --dry-run (-n) before large destructive operations.
  • Combine cp -i to avoid accidentally overwriting important configs.
  • Use rsync -z for compression over slow networks.
  • For remote sync, set up SSH keys to automate without password prompts.

๐ŸŽฏ 15. Conclusion

Ubuntu provides flexible, powerful file management tools directly from the terminal. cp remains the go-to for straightforward copying, while rsync delivers enterprise-grade synchronization and backup capabilities. Mastering both commands is crucial for system administration, DevOps pipelines, automation scripts, and efficient server management.

For daily Linux administration: cp for quick tasks, rsync for professional backup & sync workflows.


โœ… Both commands are preinstalled on Ubuntu โ€” rsync might need install via sudo apt install rsync if not present. Ensure proper permissions.

โšก Quick reference โ€” cp & rsync flags

cp options

  • -r : recursive (copy directories)
  • -i : interactive prompt
  • -f : force overwrite
  • -v : verbose output
  • -p : preserve attributes
  • -u : update (copy only newer)

rsync essentials

  • -a : archive (preserve all)
  • -v : verbose
  • -h : human-readable
  • --progress : show transfer progress
  • --delete : remove extraneous files
  • -z : compress during transfer
  • -n : dry-run
๐Ÿ”ฅ Example: rsync -avh --progress --delete /source/ user@remote:/dest/ โ†’ professional-grade sync.
Back