cp Command Guide
The cp command copies files and directories. Learn how to duplicate data safely with various options for different use cases.
5 min read•Last updated: 2024
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Basic
cp file.txt copy.txtCopy filecp file.txt dir/Copy to directorycp -r dir1/ dir2/Copy directoryOptions
-iPrompt before overwrite-nDo not overwrite-uCopy only if newerPreserve
-pKeep permissions/timestamps-aArchive mode (preserve all)-vVerbose outputAdvanced
cp -l file linkCreate hard linkcp -s file linkCreate symbolic linkcp *.txt dir/Copy multiple filesDownloadable Image Preview
Failed to generate preview
Basic Usage
Copy a file to a new location or with a new name.
bash
# Copy file to new name
cp file.txt copy.txt
# Copy file to directory
cp file.txt /path/to/directory/
# Copy multiple files to directory
cp file1.txt file2.txt /path/to/directory/Common Options
cp Options
| -r, -R | Copy directories recursively |
| -i | Prompt before overwriting |
| -n | Do not overwrite existing files |
| -u | Copy only when source is newer |
| -v | Verbose output |
| -p | Preserve mode, ownership, timestamps |
| -a | Archive mode (preserve all attributes) |
| -l | Create hard links instead of copying |
| -s | Create symbolic links instead of copying |
Copying Directories
bash
# Copy directory recursively
cp -r source_dir/ dest_dir/
# Copy with verbose output
cp -rv source_dir/ dest_dir/
# Archive mode (preserves everything)
cp -a source_dir/ dest_dir/Warning
Without
-r, cp will skip directories. Always use -r when copying directories.Preventing Overwrites
bash
# Ask before overwriting
cp -i file.txt /destination/
# Never overwrite
cp -n file.txt /destination/
# Only copy if source is newer
cp -u file.txt /destination/Preserving File Attributes
bash
# Preserve mode, ownership, timestamps
cp -p file.txt backup.txt
# Archive mode (equivalent to -dR --preserve=all)
cp -a source_dir/ backup_dir/Tip
Use
cp -a for backups to preserve all file attributes including symbolic links.Creating Links
bash
# Create hard link
cp -l file.txt hardlink.txt
# Create symbolic link
cp -s file.txt symlink.txt
# Or use ln command
ln file.txt hardlink.txt
ln -s file.txt symlink.txtPractical Examples
Backup a configuration file
bash
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bakCopy with timestamp in filename
bash
cp file.txt "file_$(date +%Y%m%d).txt"Copy directory structure only (without files)
bash
find source_dir -type d -exec mkdir -p dest_dir/{} \;Copy specific file types
bash
# Copy all .txt files
cp *.txt /destination/
# Copy with find
find . -name "*.jpg" -exec cp {} /destination/ \;Progress indicator for large files
bash
# Using rsync instead
rsync -ah --progress source dest
# Or use pv
pv source.iso > dest.isoCopy to remote via SSH
bash
scp file.txt user@host:/path/to/destination/
scp -r directory/ user@host:/path/to/destination/cp vs rsync
For large copies or syncing, rsync is often better:
bash
# rsync advantages:
# - Shows progress
# - Can resume interrupted transfers
# - Only copies changed files
rsync -avh --progress source/ dest/Summary
cp is fundamental for file management. Key takeaways:
- Use
cp -rfor directories - Use
cp -ito prevent accidental overwrites - Use
cp -afor backups (preserves everything) - Use
cp -uto update only newer files - Consider
rsyncfor large transfers