What cp does
Copies data from a source to a destination. By default it overwrites silently (no prompt), so use the safety flags while learning.
cp SOURCE DEST # copy/rename a single filecp SOURCE1 SOURCE2 DIR/ # copy many files into a directorycp -r SRC_DIR DEST_DIR # copy a directory (recursive)1) Everyday examples
cp notes.txt notes_backup.txt # copy and renamecp *.pdf ~/Documents/ # copy many files via globcp -r project ~/backup/ # copy folder recursivelycp -ri project ~/backup/ # recursive + interactive (asks before overwrite)cp -u report.docx ~/shared/ # copy only if source is newer or missingcp -v a.txt b.txt ~/dest/ # verbose (show what’s happening)cp --backup=numbered file.txt ~/dest/ # keep old dest as file.txt.~1~, ~2~, ...With spaces in names:
cp "My File.txt" "New Name.txt"Hidden files: (globs usually skip dotfiles)
cp -a .* * /path/to/dir # careful; better:shopt -s dotglob && cp -a * /path/to/dir2) Must-know
options (GNU cp on Linux)
·
-r / -R — recursive (needed
to copy directories).
·
-a (archive) — best
for full directory copies: preserves symlinks, permissions, ownership, timestamps, xattrs;
also recursive.
(Ownership preserve may require sudo.)
·
-i — interactive: ask before overwrite (safer while
learning).
·
-n — no clobber: never overwrite existing files.
·
-u — update: copy only if source is newer or destination
absent.
·
-v — verbose: print each operation.
·
-p — preserve mode/ownership/timestamps (subset of -a).
·
-d / -P — preserve symlinks
as symlinks (included in -a).
·
-L — follow symlinks (copy the target data).
·
-t
DIR — specify target directory
first, then list sources (handy in scripts).
·
-T — treat destination as a file, not
directory (avoids surprises).
·
--parents — recreate the full path under dest:
·cp --parents src/a/b/c.txt /tmp/ # creates /tmp/src/a/b/c.txt
·
--backup[=numbered] — keep existing dest by renaming it.
·
--reflink=auto — fast copy-on-write on Btrfs/XFS if supported (instant, space-saving).
·
--sparse=always — keep sparse files efficient.
Safety combo for
students: cp -ai
(archive + interactive).
3) Reading results & behaviors
·
Overwrite
by default: cp won’t ask unless you use -i or -n.
·
Directories: require -r (or -a).
·
Trailing
slashes: cp -r src/ dest/ copies contents into dest/src/ if dest exists.
cp -r src dest (without trailing slash) behaves similarly; use -T to force treat dest as a file.
·
Permissions: if you want exact metadata preserved, use -a or -p.
4) Common pitfalls (and fixes)
·
“omg
it overwrote my file” → use -i (ask) or -n (no
overwrite) or --backup.
· “No such file or directory” → check path/typo; tab-complete; ensure parent folder exists.
·
Weird
names starting with - → use -- to end options or -t:
·cp -- -weirdname.txt dest/
·cp -t dest -- -weirdname.txt other.txt
·
Dotfiles
not copied with * → globs skip hidden files unless you include them explicitly or enable
dotglob.
·
Ownership
not preserved as normal user →
need sudo or stick to -p for
what you can preserve.
5) When not to
use cp
·
Large,
incremental directory syncs:
prefer rsync -a
--delete SRC/ DEST/ (faster,
resumable, selective).
·
Remote
copies: use scp or rsync -e ssh.
·
Backups
with history: consider rsync with snapshots or version control (git).
6) Exam/Interview bullets
·
Syntax: cp
SRC DST or cp SRC1 SRC2 ... DIR/; directories need -r.
·
Preserve
everything: -a (archive: recursive + metadata + symlinks).
·
Avoid
overwrites: -i (ask) or -n
(no clobber); --backup to keep old versions.
·
Copy
only newer: -u.
·
Verbose: -v; -t DIR useful in scripts.
·
Symlinks: -d/-P keep links; -L
dereferences.
·
Copy-on-write: --reflink=auto (if FS supports).
7) Mini-lab (15–20 min)
mkdir -p ~/lab/cp/{src,dst} && cd ~/lab/cp/srcprintf "A\n" > a.txtprintf "B\n" > b.txtmkdir pics && printf "IMG" > pics/p1.jpg # 1) basic copiescp a.txt ../dst/cp a.txt ../dst/a2.txtls -l ../dst # 2) directoriescp -r pics ../dst/ls -l ../dst/pics # 3) safe overwritesprintf "A-NEW\n" > a.txtcp -i a.txt ../dst/a.txt # answer y/n to learn behavior # 4) preserve metadatatouch -t 202401011200 b.txtcp -p b.txt ../dst/ls -l b.txt ../dst/b.txt # compare times/perms # 5) update-only & verbosesleep 1 && printf "B-NEW\n" >> b.txtcp -uv b.txt ../dst/ # copies because source is newer # 6) backup old destcp --backup=numbered a.txt ../dst/a.txtls ../dst | grep a.txtQuick reference
·
Copy file → file:
cp src.txt dst.txt
·
Copy many → dir: cp a b c dir/
·
Copy directory: cp -r src/ dir/
·
Preserve
everything: cp -a src/
dir/
·
Ask before
overwrite: cp -i file
dir/
·
Only if newer: cp -u file dir/
·
Keep old dest: cp --backup file dir/
Want this as a one-page printable
with cp + mv comparisons? I can format it neatly.