Terminal GuideTerminal Guide

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 readLast updated: 2024
Dai Aoki

Dai Aoki

CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm

Quick Reference

Basic

cp file.txt copy.txtCopy file
cp file.txt dir/Copy to directory
cp -r dir1/ dir2/Copy directory

Options

-iPrompt before overwrite
-nDo not overwrite
-uCopy only if newer

Preserve

-pKeep permissions/timestamps
-aArchive mode (preserve all)
-vVerbose output

Advanced

cp -l file linkCreate hard link
cp -s file linkCreate symbolic link
cp *.txt dir/Copy multiple files

Downloadable 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, -RCopy directories recursively
-iPrompt before overwriting
-nDo not overwrite existing files
-uCopy only when source is newer
-vVerbose output
-pPreserve mode, ownership, timestamps
-aArchive mode (preserve all attributes)
-lCreate hard links instead of copying
-sCreate 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.
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.txt

Practical Examples

Backup a configuration file

bash
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

Copy 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.iso

Copy 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 -r for directories
  • Use cp -i to prevent accidental overwrites
  • Use cp -a for backups (preserves everything)
  • Use cp -u to update only newer files
  • Consider rsync for large transfers

Related Articles