Copying Directories of Files: rsync vs cp

From Free Knowledge Base- The DUCK Project
Revision as of 15:21, 24 December 2025 by Littleguy (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Copying Directories of Files: rsync vs cp

This page provides a detailed comparison between using the cp and rsync commands for backing up directories containing files, particularly in scenarios such as copying from a network-mounted drive to a removable storage device like an SD flash drive. The discussion is based on usage in Linux Mint 21.1 with the Cinnamon desktop environment.

Background and Example Scenario

A common task is to back up a directory of files using the cp command. An example command might look like this:

cp -adv ./SourceDirectory /path/to/BackupDestination/

This command copies the directory "SourceDirectory" (including the directory itself) recursively to the destination. The options used are:

  • -a: archive mode, which preserves symbolic links, permissions, timestamps, and other attributes
  • -d: preserves symbolic links as links
  • -v: verbose mode, which displays the files being copied

This method works effectively for straightforward copies. However, if the copy process is interrupted, it does not resume, and partial progress may be lost.

The objectives include ensuring file copy integrity, allowing resumption after interruptions, providing visible progress, and correctly handling directory names that contain spaces.

Recommended rsync Command

The robust alternative is rsync. To copy a directory (including the directory name itself) and all its contents to a destination folder, use:

rsync -av --progress --partial --whole-file "./Source Directory" /path/to/BackupDestination/

This creates "/path/to/BackupDestination/Source Directory/" containing the full structure.

Key options:

  • -a: archive mode (recursive, preserves attributes)
  • -v: verbose output
  • --progress: shows per-file and overall progress
  • --partial: keeps partial files for resumption
  • --whole-file: optimizes speed for initial full copies by skipping delta transfers

Important notes on directory handling:

  • Use quotes around the source path if the directory name contains spaces (e.g., "./Source Directory").
  • Do not add a trailing slash to the source path — this ensures the directory itself is created at the destination.
  • Adding a trailing slash to the source (e.g., "./Source Directory"/) would copy only the contents directly into the destination, without creating the base directory.
  • The destination path may end with a slash or not; rsync will create necessary directories.

For maximum integrity, --checksum can be added, but it significantly slows the process.

Performance Comparison

For initial full copies to an empty or partial destination:

  • cp -adv is slightly faster (typically 5–20% less overhead) due to direct copying without file list checks.
  • rsync has minor overhead from scanning, but device I/O (network and SD card) is usually the bottleneck, making differences small.
  • --whole-file reduces rsync overhead in full-copy scenarios.

Advantages of Each Method

Advantages of cp -adv

  • Slightly faster for initial full copies
  • Simpler command syntax
  • Lower CPU usage
  • No preliminary file scanning

Advantages of rsync -av --progress --partial --whole-file

  • Resumes automatically after interruptions
  • Displays detailed progress
  • Safe to re-run; skips identical files
  • Excellent handling of spaces in filenames and paths
  • Robust preservation of attributes
  • Supports dry runs for verification (--dry-run)
  • Retains partial transfers

Recommendation

For one-time copies with no interruptions expected, cp -adv (with proper quoting for spaces) is simple and fast.

For backups involving large directories, spaces in names, network sources, or removable media prone to disconnection, rsync with the above options and correct slash handling is far more reliable and practical.

Related