cp copies files and directories. It duplicates content from one location to another.
Basic Usage
cp source destination
Copy a File
cp file.txt backup.txt
Creates a copy named backup.txt.
Copy to a Directory
cp file.txt ~/Documents/
Copies file.txt into the Documents folder.
Copy Multiple Files
cp file1.txt file2.txt file3.txt ~/Documents/
All three files go into Documents.
Or use wildcards:
cp *.txt ~/Documents/
Copies all .txt files.
Copy and Rename
cp original.txt ~/Documents/newname.txt
Copies and renames in one command.
Copy Directories (-r)
Directories require the -r (recursive) flag:
cp -r folder/ ~/backup/
Without -r, you get an error:
cp: folder is a directory (not copied)
Common Options
| Option | Effect |
|---|---|
-r |
Copy directories recursively |
-i |
Prompt before overwriting |
-n |
Don't overwrite existing files |
-v |
Verbose, show what's being copied |
-p |
Preserve timestamps and permissions |
Safe Copying (-i)
Ask before overwriting:
cp -i file.txt ~/Documents/
If a file exists there:
overwrite /Users/john/Documents/file.txt? (y/n)
Prevent Overwriting (-n)
cp -n file.txt ~/Documents/
Silently skips if the destination exists.
Verbose Mode (-v)
cp -v file.txt ~/Documents/
Shows what's happening:
file.txt -> /Users/john/Documents/file.txt
Preserve Metadata (-p)
cp -p file.txt backup.txt
Keeps original:
- Modification time
- Access time
- Permissions
- Ownership (if you're root)
Combine Options
cp -rvi folder/ ~/backup/
Recursive + verbose + interactive.
Copy Everything in a Directory
cp -r folder/* ~/destination/
Copies contents, not the folder itself.
vs:
cp -r folder ~/destination/
Copies the folder including the folder.
Practical Examples
Backup a config file before editing:
cp ~/.zshrc ~/.zshrc.backup
Copy project to new location:
cp -r ~/Projects/app ~/Projects/app-v2
Copy only certain file types:
cp *.jpg ~/Pictures/
cp *.{jpg,png,gif} ~/Pictures/
Copy with timestamp in filename:
cp database.db "database-\$(date +%Y%m%d).db"
Copying to Remote Servers
For remote copying, use scp (secure copy):
scp file.txt user@server:/path/
scp -r folder/ user@server:/path/
Common Errors
"No such file or directory":
- Source file doesn't exist
- Destination path doesn't exist
"Is a directory":
- Trying to copy a directory without
-r
"Permission denied":
- You don't have read access to source
- You don't have write access to destination
cp vs mv
| Command | Effect |
|---|---|
cp file.txt backup/ |
Creates a copy, original stays |
mv file.txt backup/ |
Moves file, original is gone |
Use cp when you want to keep the original.
Quick Reference
| Command | Result |
|---|---|
cp a.txt b.txt |
Copy and rename |
cp a.txt folder/ |
Copy to folder |
cp -r dir/ dest/ |
Copy directory |
cp -i a.txt b.txt |
Prompt before overwrite |
cp -n a.txt b.txt |
Never overwrite |
cp *.txt folder/ |
Copy matching files |
Keep Learning
File operations are essential Terminal skills. The free course covers copying, moving, and more.
Check it out at Mac Terminal for Humans.