cp
Basic Usage of cp - Copying Files and Directories
The cp command is used to copy files and directories in Linux systems. It is one of the most important commands for file management and system administration.
cp source destination
Example:
cp notes.txt backup.txt
cpstands for “copy”- Creates a duplicate of a file or directory
- Original file remains unchanged
- Commonly used for backups and file management
Copying a Single File
To copy one file into another file:
cp file1.txt file2.txt
Result:
file1.txtremains unchangedfile2.txtbecomes a copy offile1.txt
Example:
Before:
file1.txt
After:
file1.txt
file2.txt
- If destination file does not exist, it is created
- If destination file already exists, it is overwritten
Copying Files into Another Directory
To copy a file into a directory:
cp notes.txt Documents/
- Copies
notes.txtintoDocuments - File keeps original name
- Destination directory must already exist
Example result:
Documents/notes.txt
Copying Multiple Files
The cp command can copy multiple files at once.
cp file1.txt file2.txt file3.txt backup/
- Copies all specified files into
backup/ - Last argument must be a directory
- Useful for batch operations
Interactive Copy Mode
To prevent accidental overwriting:
cp -i file.txt backup.txt
Example prompt:
cp: overwrite 'backup.txt'?
-imeans interactive mode- Asks for confirmation before overwrite
- Useful during manual administration tasks
Verbose Output
To display detailed copy operations:
cp -v file.txt backup.txt
Example output:
'file.txt' -> 'backup.txt'
-vmeans verbose mode- Displays each copied file
- Helpful for troubleshooting and scripts
Copying Directories
By default, cp cannot copy directories directly.
Attempting:
cp Documents backup/
Produces error:
cp: -r not specified; omitting directory 'Documents'
To copy directories recursively:
cp -r Documents backup/
-rmeans recursive- Copies directory and all contents
- Includes subdirectories and files
Recursive Copy Example
Directory structure:
Documents/
├── notes.txt
├── projects/
│ └── app.py
Command:
cp -r Documents backup/
Result:
backup/Documents/
├── notes.txt
├── projects/
│ └── app.py
- Entire directory tree is duplicated
- Common for backups and migrations
Preserving File Attributes
To preserve metadata during copy:
cp -p file.txt backup.txt
Preserved attributes include:
- timestamps
- ownership
- permissions
This is important in:
- backups
- migrations
- server administration
Archive Mode
Administrators often use archive mode for backups.
cp -a source/ backup/
-ameans archive mode- Preserves directory structure
- Preserves permissions and timestamps
- Preserves symbolic links
Archive mode is equivalent to several options combined together.
Copying Symbolic Links
Example symbolic link:
latest -> releases/v2
Normal copy behavior may follow the target file.
To preserve symbolic links:
cp -a source/ backup/
- Keeps symlinks intact
- Important during deployments and backups
Preventing Overwrites
To avoid replacing existing files:
cp -n file.txt backup.txt
-nmeans no-clobber- Existing files remain untouched
- Useful in backup scripts
Updating Only Newer Files
To copy only newer files:
cp -u source.txt backup.txt
-umeans update- Copies only if source is newer
- Saves time during synchronization
Copying with Wildcards
Linux shells support wildcard expansion.
Copy all .log files:
cp *.log backup/
Copy all .txt files:
cp *.txt archive/
*matches multiple files- Useful for bulk operations
- Common in scripts and automation
Combining Multiple Options
Linux commands allow multiple options together.
Example:
cp -rv Documents backup/
Breakdown:
-r= recursive copy-v= verbose output
Another example:
cp -aiv source/ backup/
This enables:
- archive mode
- interactive prompts
- verbose output
Common Administrative Examples
Backup configuration file:
cp /etc/nginx/nginx.conf nginx.conf.backup
Copy website files:
cp -r /var/www/html backup/
Preserve permissions during migration:
cp -a /srv/data /backup/
Copy all logs:
cp /var/log/*.log logs/
Practical Script Example (Step-by-Step Explanation)
Script
#!/bin/bash
SOURCE="/var/log"
DESTINATION="/backup/logs"
mkdir -p $DESTINATION
cp -rv $SOURCE/* $DESTINATION
echo "Backup completed"
Step 1: Shebang
#!/bin/bash
- Defines Bash interpreter
- Ensures script runs correctly
Step 2: Defining source directory
SOURCE="/var/log"
- Stores source path in variable
- Makes script easier to modify
Example value:
/var/log
Step 3: Defining destination directory
DESTINATION="/backup/logs"
- Stores backup location
- Used later during copy operation
Step 4: Creating destination directory
mkdir -p $DESTINATION
Breakdown:
mkdircreates directories-pcreates parent directories if needed- Prevents errors if directory already exists
Example result:
/backup/logs
Step 5: Copying files recursively
cp -rv $SOURCE/* $DESTINATION
Breakdown:
-renables recursive copy-vdisplays verbose output$SOURCE/*selects all files inside source directory$DESTINATIONis target location
Example output:
'/var/log/syslog' -> '/backup/logs/syslog'
Step 6: Printing completion message
echo "Backup completed"
- Displays informational message
- Helps confirm successful execution
Output:
Backup completed
What this script does
Step-by-step flow:
- Defines source directory
- Defines backup destination
- Creates destination if missing
- Copies files recursively
- Displays completion message
Why this matters in production
This pattern is common in:
- backup automation
- deployment scripts
- log archiving
- system migrations
- disaster recovery procedures
The combination of:
mkdir -p- recursive copy
- verbose output
creates reliable automation workflows.
Common Beginner Mistakes
Trying to copy directory without -r:
cp folder backup/
Result:
cp: -r not specified; omitting directory 'folder'
Another mistake:
Overwriting files accidentally:
cp file.txt important.txt
Safer version:
cp -i file.txt important.txt
Another mistake:
Using wildcards incorrectly:
cp *.txt
This fails because destination is missing.
Correct version:
cp *.txt backup/
Summary
In this guide, you learned:
- how to copy files using
cp - recursive directory copying
- preserving permissions and timestamps
- interactive and verbose modes
- wildcard usage
- archive mode
- preventing overwrites
- update-only copying
- practical backup scripting
These skills are essential for:
- Linux administration
- file management
- backup automation
- deployment workflows
- server maintenance
Additional cp parameters not covered in this guide include:
--parents: Preserve full directory paths
--backup: Create backup before overwrite
--remove-destination: Remove existing destination first
--sparse: Handle sparse files efficiently
--strip-trailing-slashes: Remove trailing slashes
--help: Display help information
--version: Display version information