cp
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 destvscp -a src/ dest/vscp -a src/. dest/behave differently whendestexists.- Dry-run mentally or use
rsync -nfor complex trees. - Overwriting live configs can break services — copy to
.newthenmv.
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/Reflink (btrfs/xfs when available)
cp --reflink=auto big.img big-clone.imgFalls back to full copy if CoW clone is unsupported.
Preserve attributes selectively
cp --preserve=mode,timestamps src dest
cp -a --no-preserve=ownership src dest # useful as non-rootParents and target dir
cp --parents etc/ssh/sshd_config /backup/
# creates /backup/etc/ssh/sshd_config
cp -t /backup/ a.txt b.txtSymlinks
cp -P link dest_link # copy symlink itself
cp -L link dest_file # copy the referent’s dataOne-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 pathsNotes / Pitfalls
- Cross-filesystem copies rewrite data; hard links cannot span devices.
- Sparse files: GNU
cphas--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 existingdest/when you mean “contents”. cpdoes not delete extraneous files in dest (mirroring needsrsync --delete).
2026-relevant notes
- For backups and sync,
rsync -aHAX --info=progress2is usually better than rawcp -a. - Reflink copies shine on btrfs/xfs with CoW; verify with
stat/filefragwhen testing. - In containers, watch UID maps — preserved numeric owners may not match host users.
Additional Resources
man cp- GNU coreutils — cp