StudyLover
  • Home
  • Study Zone
  • Profiles
  • Typing Tutor
  • Contact us
  • Sign in
StudyLover Linux Command: cp
Download
  1. Linux
Linux Command: cd : Linux Command: mv
Linux

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 file

cp SOURCE1 SOURCE2 DIR/        # copy many files into a directory

cp -r SRC_DIR DEST_DIR         # copy a directory (recursive)


1) Everyday examples

cp notes.txt notes_backup.txt              # copy and rename

cp *.pdf ~/Documents/                      # copy many files via glob

cp -r project ~/backup/                    # copy folder recursively

cp -ri project ~/backup/                   # recursive + interactive (asks before overwrite)

cp -u report.docx ~/shared/                # copy only if source is newer or missing

cp -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/dir


2) 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/src

printf "A\n" > a.txt

printf "B\n" > b.txt

mkdir pics && printf "IMG" > pics/p1.jpg

 
# 1) basic copies

cp a.txt ../dst/

cp a.txt ../dst/a2.txt

ls -l ../dst

 
# 2) directories

cp -r pics ../dst/

ls -l ../dst/pics

 
# 3) safe overwrites

printf "A-NEW\n" > a.txt

cp -i a.txt ../dst/a.txt    # answer y/n to learn behavior

 
# 4) preserve metadata

touch -t 202401011200 b.txt

cp -p b.txt ../dst/

ls -l b.txt ../dst/b.txt    # compare times/perms

 
# 5) update-only & verbose

sleep 1 && printf "B-NEW\n" >> b.txt

cp -uv b.txt ../dst/        # copies because source is newer

 
# 6) backup old dest

cp --backup=numbered a.txt ../dst/a.txt

ls ../dst | grep a.txt


Quick 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.

 

Linux Command: cd Linux Command: mv
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website