Introduction

Hello! Today I’ll explain the cp command.

The cp command copies files and directories. It’s one of the most essential file operations - you’ll use it daily for backups, duplicating project files, and more.

Think of it as the command-line version of copy-paste. I use this a lot.

What is the cp Command

The cp command is an external command for copying files and directories. “cp” stands for “copy”.

It creates a duplicate of a file while keeping the original intact. You can rename files during copy, copy to different directories, or preserve file attributes.

Basic Syntax

1
cp [options] source destination

You can specify multiple source files. In that case, the destination must be a directory.

Main Options

Option Description
-i Prompt before overwrite (interactive)
-r, -R Copy directories recursively
-p Preserve attributes (timestamps, permissions)
-a Archive mode (equivalent to -pdr)
-u Copy only when source is newer (update)
-v Verbose output
-n Do not overwrite existing files
-l Create hard links
-s Create symbolic links

Usage Examples

Example 1: Basic File Copy

1
cp file1.txt file2.txt

Output:

1
(No output on success)

Copies file1.txt to file2.txt. The simplest usage.

Example 2: Copy with Different Name

1
cp document.txt document_backup.txt

Output:

1
(No output on success)

Creates a copy with a different name.

Example 3: Copy to Different Directory

1
cp file.txt /home/user/backup/

Output:

1
(No output on success)

Copies file.txt to the /home/user/backup/ directory.

Example 4: Copy Multiple Files at Once

1
cp file1.txt file2.txt file3.txt /backup/

Output:

1
(No output on success)

Copies three files to the /backup/ directory at once.

Example 5: Copy Directory Recursively

1
cp -r folder1 folder2

Output:

1
(No output on success)

Copies folder1 to folder2 including all contents.

Example 6: Copy with Confirmation (-i)

1
cp -i source.txt destination.txt

Output:

1
cp: overwrite 'destination.txt'?

Prompts for confirmation if destination.txt already exists. Press y to overwrite, n to cancel.

Example 7: Preserve Attributes (-p)

1
cp -p original.txt copy.txt

Output:

1
(No output on success)

Preserves timestamps, permissions, and ownership.

Example 8: Archive Mode (-a)

1
cp -a /var/www/project /backup/project

Output:

1
(No output on success)

Copies entire directory with all attributes preserved. Perfect for backups. -a is equivalent to -dpr.

Example 9: Verbose Output (-v)

1
cp -v file1.txt file2.txt file3.txt /backup/

Output:

1
2
3
'file1.txt' -> '/backup/file1.txt'
'file2.txt' -> '/backup/file2.txt'
'file3.txt' -> '/backup/file3.txt'

Shows which files are being copied. Useful for large operations.

Example 10: Copy with Wildcards

1
cp *.txt /backup/

Output:

1
(No output on success)

Copies all .txt files to the backup directory.

Example 11: Copy Only Updated Files (-u)

1
cp -u source/* destination/

Output:

1
(No output on success)

Copies only files that are newer than the destination. Great for syncing.

Example 12: Create Numbered Backups

1
cp --backup=numbered file.txt file.txt

Output:

1
2
3
(No output on success)
ls
file.txt  file.txt.~1~

Creates numbered backups if the file exists.

Tips & Notes

Use -i for Safety

Always use -i to avoid accidentally overwriting important files.

1
cp -i source.txt destination.txt

It prompts before overwriting. Good habit for beginners!

-r is Required for Directories

1
2
3
4
5
# This will error
cp folder1 folder2

# Correct way
cp -r folder1 folder2

You must use -r (recursive) to copy directories. It’ll error otherwise.

Trailing Slash Matters

1
2
3
4
5
6
7
8
# Copies folder1 itself into destination
cp -r folder1 destination/

# Copies contents of folder1 into destination
cp -r folder1/ destination/

# Or
cp -r folder1/* destination/

The trailing slash can change behavior in some contexts.

Set Up Aliases

1
2
# Add to ~/.bashrc
alias cp='cp -i'

This makes interactive mode the default. Safer!

Same Directory Copying

1
2
3
4
5
# This will error
cp file.txt file.txt

# OK: Use different name
cp file.txt file_copy.txt

Can’t copy to the same name. Always use a different name.

Large File Operations

1
2
3
4
5
# Show progress
cp -rv large_directory/ /backup/

# Faster alternative for large operations
rsync -av source/ destination/

For large copies, use -v for progress or consider rsync.

Practical Usage

Backup Configuration Files

1
cp -p ~/.bashrc ~/.bashrc.backup

Backup config files before editing. -p preserves timestamps.

Duplicate Projects

1
cp -a /var/www/project /var/www/project_backup

Full project backup with all attributes preserved.

Timestamped Backups

1
cp -r myproject myproject_$(date +%Y%m%d)

Creates dated backups like myproject_20251026.

Prepare for Multi-Server Deployment

1
2
3
4
# Copy config for each server
cp config.yml config_server1.yml
cp config.yml config_server2.yml
cp config.yml config_server3.yml

Or using a loop:

1
2
3
for server in server1 server2 server3; do
    cp config.yml config_${server}.yml
done

Safe Copy in Scripts

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#!/bin/bash
set -e

SOURCE="/path/to/source"
DEST="/path/to/destination"

if [ -e "$SOURCE" ]; then
    cp -av "$SOURCE" "$DEST"
    echo "Copy completed: $SOURCE -> $DEST"
else
    echo "Error: $SOURCE does not exist" >&2
    exit 1
fi

Duplicate Template Files

1
2
3
4
5
# Create new file from template
cp template.html new_page.html

# Copy multiple templates
cp templates/*.html pages/
1
2
# -a option preserves symbolic links
cp -a linked_dir /backup/

Summary

Key points about the cp command:

  • Basic command for copying files and directories
  • -i: Prompt before overwrite (super important!)
  • -r: Required for copying directories
  • -p: Preserve timestamps and permissions
  • -a: Archive mode (perfect for backups)
  • -v: Verbose output
  • -u: Copy only newer files
  • Common combinations: cp -av, cp -i, cp -rp

File copying is essential. Make -i a habit to protect your important files!