Python Copying Files and Directories with cp
The `cp` (copy) command duplicates files and directories across filesystems. Copying directories requires the recursive flag (`-r` or `-R`), and preserving file timestamps, permissions, and ownership is achieved via `-p` or the archive flag `-a`.
"`cp` is like taking a document to a photocopier: the original document stays untouched in your hands while an identical twin is created at the destination."
Deep Dive: How It Works
Recursive Copy (-r): Necessary when duplicating directories containing subfiles and nested folders.
Archive Mode (-a): Equivalent to `-dpR` (preserves symlinks, file modes, owners, timestamps, and recursion). Best for backups.
Update Mode (-u): Copies only when the source file is newer than the destination or when the destination is missing.
Syntax Blueprint
cp source.txt destination.txt cp -r source_dir/ backup_dir/ cp -a /var/www /backup/www # Archive copy
Use cp for individual files and cp -r / cp -a for recursive directory copying.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Trying to copy a directory with plain `cp` without `-r` or `-a`.Why it happens: cp omits directories by default to prevent accidental massive file duplication.
How to fix: Always provide `-r` or `-a` when copying folders.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Copy a File to a Backup
Create `/tmp/source.txt` containing "Production Code".
Copy it to `/tmp/source.bak` with `cp`.
Read `/tmp/source.bak` with `cat`.
Clean up test files.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.