cp

Updated

September 4, 2026

Overview

cp copies files and directories. For large trees with incremental updates, prefer rsync. Mind directory semantics: trailing slashes and whether the destination already exists change results. Default is not recursive — use -r/-R or -a for trees.

Syntax

cp [options] source dest
cp [options] source... directory
cp [options] -t directory source...

Common Options

Option Description
-a, --archive Same as -dR --preserve=all (typical full tree copy)
-r, -R, --recursive Copy directories recursively
-i, --interactive Prompt before overwrite
-n, --no-clobber Never overwrite
-u, --update Copy only when source is newer or dest missing
-v, --verbose Show files copied
-p Preserve mode, ownership, timestamps (subset of archive)
--preserve=ATTR_LIST Fine-grained preserve (mode,ownership,timestamps,links,xattr,context)
-L / -P / -H Always follow / never follow / follow command-line symlinks
-l / -s Hardlink / symlink instead of copying data
--reflink=auto CoW lightweight clone when filesystem supports it
-T Treat dest as a normal file (do not put source into dest dir)
-t DIR Destination directory first
--parents Create full parent path under dest

Safety

  • cp -a src dest vs cp -a src/ dest/ vs cp -a src/. dest/ behave differently when dest exists.
  • Dry-run mentally or use rsync -n for complex trees.
  • Overwriting live configs can break services — copy to .new then mv.

Examples with Explanations

Files

cp file.txt file.bak
cp -i *.conf /etc/myapp/
cp -n important.dat /backup/     # do not overwrite
cp -u -v *.o build/

Directories

cp -a project/ project-backup/
cp -a src/. dest/                # copy *contents* into dest
cp -r --preserve=timestamps logs/ logs-copy/

Preserve attributes selectively

cp --preserve=mode,timestamps src dest
cp -a --no-preserve=ownership src dest   # useful as non-root

Parents and target dir

cp --parents etc/ssh/sshd_config /backup/
# creates /backup/etc/ssh/sshd_config
cp -t /backup/ a.txt b.txt

One-liners

cp -a /var/www/html/. /var/www/html.bak/
cp -ai src/* dest/            # interactive archive-ish for files
install -D -m 644 app.conf /etc/app/app.conf   # alternative for install paths

Notes / Pitfalls

  • Cross-filesystem copies rewrite data; hard links cannot span devices.
  • Sparse files: GNU cp has --sparse=always|auto|never — important for VM images.
  • Permissions: non-root cannot always preserve ownership; archive mode still copies content.
  • Trailing slash myths: prefer explicit src/. into existing dest/ when you mean “contents”.
  • cp does not delete extraneous files in dest (mirroring needs rsync --delete).

2026-relevant notes

  • For backups and sync, rsync -aHAX --info=progress2 is usually better than raw cp -a.
  • Reflink copies shine on btrfs/xfs with CoW; verify with stat/filefrag when testing.
  • In containers, watch UID maps — preserved numeric owners may not match host users.

Additional Resources