Chapter 11: Bash Copy (cp)

Bash Copy (cp) — the cp command, which stands for “copy”.

This is one of the big four file-handling commands in Bash/Linux (along with mv, rm, and mkdir). cp is your safe way to duplicate files or entire folders without touching or deleting the original — like making photocopies of your important notes before lending them to a friend.

Think of it as:

  • Original = master copy
  • cp = Xerox machine that makes identical duplicates

Very important teacher warning upfront: Unlike mv (which moves/renames), cp leaves the source untouched. But if you copy to a destination that already exists, it overwrites without asking by default — that’s why we always learn safe options first!

Basic Syntax

Bash
  • source → what you want to copy (file or folder)
  • destination → where to put the copy (new filename or folder)
  • Multiple sources → last argument must be a directory

1. Copy a Single File (Simplest Case)

Bash

→ Creates backup_notes.txt as exact copy in same folder.

Copy to different folder:

Bash

→ Puts copy inside ~/Documents/backups/ with same name report.pdf

2. Copy Multiple Files at Once

Last argument must be a directory:

Bash

→ Copies all three into ~/Pictures/

Using wildcard (very powerful):

Bash

→ * = any characters, so copies all .txt or files starting with “report”

3. Copy a Directory (Folder) — Must Use -r or -R

By default cp refuses to copy folders (says “omitting directory”) — use -r (recursive):

Bash

→ Copies entire my_project folder + everything inside (files, subfolders, etc.)

Common mistake: forgetting -r → error!

Bash

Fix: always add -r when source is a directory.

4. Most Important & Safe Options (Memorize These!)

Option Long form What it does (very useful!) Example Command
-r or -R –recursive Copy directories recursively (must for folders) cp -r src dest
-i –interactive Ask before overwriting existing files (SAFEST!) cp -i file.txt dest/ → prompts if exists
-f –force Force overwrite (no prompt, even if -i) cp -f old.txt new.txt
-v –verbose Show what is being copied (like progress) cp -v *.jpg ~/Pictures/
-p –preserve Preserve timestamps, permissions, owner cp -p important.doc backup/
-a –archive Almost perfect copy: -p + recursive + symlinks safe cp -a project/ project_backup/ (most recommended)
-u –update Copy only if source is newer or missing at dest cp -u *.log /var/log/archive/
-b –backup Make backup (~) before overwrite cp -b file.txt dest/ → creates file.txt~ if exists
-n –no-clobber Never overwrite existing files cp -n file.txt dest/ → skips if exists
–parents Keep directory structure in copy cp –parents dir1/dir2/file.txt dest/

Best daily combo for folders:

Bash

→ Archive mode + verbose → safe, preserves everything, shows progress

5. Real-Life Examples You Will Use

Example 1: Backup your config file safely

Bash

→ Makes dated backup, asks if already exists

Example 2: Copy all Python scripts to new project

Bash

Example 3: Duplicate entire website folder for testing

Bash

→ Exact copy including permissions/timestamps

Example 4: Copy only newer files (sync style)

Bash

Example 5: Avoid overwriting important file

Bash

→ If resume.pdf already in Dropbox, skips silently

6. Quick Practice Session (Do This Now!)

Bash

See how safe and controlled it feels with -i, -v, -a?

7. Teacher Warnings (Very Important!)

  • cp file dest/file → overwrites without asking → dangerous for important data!
  • Never cp -r / something/ by mistake (can fill disk or worse)
  • For huge folders → consider rsync later (smarter, resumable)
  • cp follows symlinks by default → use -P or -a carefully if you have links
  • No recycle bin — but -b makes backups (~ files)

Summary Table – cp at a Glance

Goal Best Command Why it’s good
Copy single file cp source.txt dest.txt Basic & fast
Copy folder safely cp -a source/ dest/ Preserves everything + recursive
Ask before overwrite cp -i … Prevents accidents
See progress cp -v … Know what’s happening
Only if newer cp -u … Good for backups/updates
Never overwrite cp -n … Super safe
Make backup before overwrite cp -b … Creates file~ automatically

Got it, boss? cp is your duplication superpower — use -i or -n when learning, -a when you want perfect copies.

Any part confusing? Want more on -a vs -p? Or “difference between cp and rsync”? Or next command like “mv deep dive”?

Just say — teacher is ready in Hyderabad! Keep copying safely! 🐧📋 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *