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
|
0 1 2 3 4 5 6 |
cp [options] source destination |
- 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)
|
0 1 2 3 4 5 6 |
cp important_notes.txt backup_notes.txt |
→ Creates backup_notes.txt as exact copy in same folder.
Copy to different folder:
|
0 1 2 3 4 5 6 |
cp report.pdf ~/Documents/backups/ |
→ Puts copy inside ~/Documents/backups/ with same name report.pdf
2. Copy Multiple Files at Once
Last argument must be a directory:
|
0 1 2 3 4 5 6 |
cp file1.txt file2.jpg photo.png ~/Pictures/ |
→ Copies all three into ~/Pictures/
Using wildcard (very powerful):
|
0 1 2 3 4 5 6 7 |
cp *.txt ~/backup/text_files/ cp report* ~/work/reports_2026/ |
→ * = 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):
|
0 1 2 3 4 5 6 |
cp -r my_project ~/backup/my_project_copy |
→ Copies entire my_project folder + everything inside (files, subfolders, etc.)
Common mistake: forgetting -r → error!
|
0 1 2 3 4 5 6 |
cp my_project ~/backup/ # → error: omitting directory 'my_project' |
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:
|
0 1 2 3 4 5 6 |
cp -av source_folder/ destination_folder/ |
→ Archive mode + verbose → safe, preserves everything, shows progress
5. Real-Life Examples You Will Use
Example 1: Backup your config file safely
|
0 1 2 3 4 5 6 |
cp -i ~/.bashrc ~/.bashrc_backup_$(date +%Y-%m-%d) |
→ Makes dated backup, asks if already exists
Example 2: Copy all Python scripts to new project
|
0 1 2 3 4 5 6 7 |
mkdir ~/new_project cp *.py ~/new_project/ |
Example 3: Duplicate entire website folder for testing
|
0 1 2 3 4 5 6 |
cp -a /var/www/my-site /var/www/my-site-test |
→ Exact copy including permissions/timestamps
Example 4: Copy only newer files (sync style)
|
0 1 2 3 4 5 6 |
cp -u -v /home/webliance/photos/*.jpg ~/backup/photos/ |
Example 5: Avoid overwriting important file
|
0 1 2 3 4 5 6 |
cp -n resume.pdf ~/Dropbox/ |
→ If resume.pdf already in Dropbox, skips silently
6. Quick Practice Session (Do This Now!)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# Create playground mkdir -p cp_practice/original cp_practice/backup cd cp_practice/original touch file1.txt file2.txt secret.pdf mkdir subfolder echo "Important data" > subfolder/data.txt # Simple file copy cp file1.txt ../backup/ # Multiple files cp *.txt ../backup/ # Folder copy (forget -r → error!) cp subfolder ../backup/ # → error! cp -r subfolder ../backup/ # Safe + verbose + preserve cp -av file2.txt ../backup/ # Interactive test cp -i secret.pdf ../backup/ # if exists, asks you y/n # Check results ls -l ../backup/ ls -lR ../backup/ # recursive list |
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! 🐧📋 😄
