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
|
|
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
|
|
Output:
|
|
Copies file1.txt to file2.txt. The simplest usage.
Example 2: Copy with Different Name
|
|
Output:
|
|
Creates a copy with a different name.
Example 3: Copy to Different Directory
|
|
Output:
|
|
Copies file.txt to the /home/user/backup/ directory.
Example 4: Copy Multiple Files at Once
|
|
Output:
|
|
Copies three files to the /backup/ directory at once.
Example 5: Copy Directory Recursively
|
|
Output:
|
|
Copies folder1 to folder2 including all contents.
Example 6: Copy with Confirmation (-i)
|
|
Output:
|
|
Prompts for confirmation if destination.txt already exists. Press y to overwrite, n to cancel.
Example 7: Preserve Attributes (-p)
|
|
Output:
|
|
Preserves timestamps, permissions, and ownership.
Example 8: Archive Mode (-a)
|
|
Output:
|
|
Copies entire directory with all attributes preserved. Perfect for backups. -a is equivalent to -dpr.
Example 9: Verbose Output (-v)
|
|
Output:
|
|
Shows which files are being copied. Useful for large operations.
Example 10: Copy with Wildcards
|
|
Output:
|
|
Copies all .txt files to the backup directory.
Example 11: Copy Only Updated Files (-u)
|
|
Output:
|
|
Copies only files that are newer than the destination. Great for syncing.
Example 12: Create Numbered Backups
|
|
Output:
|
|
Creates numbered backups if the file exists.
Tips & Notes
Use -i for Safety
Always use -i to avoid accidentally overwriting important files.
|
|
It prompts before overwriting. Good habit for beginners!
-r is Required for Directories
|
|
You must use -r (recursive) to copy directories. It’ll error otherwise.
Trailing Slash Matters
|
|
The trailing slash can change behavior in some contexts.
Set Up Aliases
|
|
This makes interactive mode the default. Safer!
Same Directory Copying
|
|
Can’t copy to the same name. Always use a different name.
Large File Operations
|
|
For large copies, use -v for progress or consider rsync.
Practical Usage
Backup Configuration Files
|
|
Backup config files before editing. -p preserves timestamps.
Duplicate Projects
|
|
Full project backup with all attributes preserved.
Timestamped Backups
|
|
Creates dated backups like myproject_20251026.
Prepare for Multi-Server Deployment
|
|
Or using a loop:
|
|
Safe Copy in Scripts
|
|
Duplicate Template Files
|
|
Preserve Symbolic Links
|
|
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!