cp Command
The cp command copies files and directories in Linux. It's one of the fundamental file operation commands used for duplicating data.
Syntax
cp [OPTION]... SOURCE... DESTINATION
Description
The cp command copies files or directories from source to destination. It can copy single files, multiple files, or entire directory trees.
Key behaviors:
- Preserves file content but creates new inode
- Can copy to same or different directory
- Requires appropriate permissions
- Can overwrite existing files (with warnings)
Common Options
| Option | Description |
|---|---|
-r, -R |
Copy directories recursively |
-i |
Interactive mode (prompt before overwrite) |
-v |
Verbose mode (show what's being copied) |
-f |
Force copy (don't prompt) |
-p |
Preserve file attributes |
-u |
Update (copy only if source is newer) |
-n |
No overwrite (don't overwrite existing files) |
Examples
Copy a single file
cp file1.txt file2.txt
Copies file1.txt to file2.txt in the same directory
Copy file to different directory
cp file.txt /home/user/Documents/
Copies file.txt to the Documents directory
Copy multiple files
cp file1.txt file2.txt file3.txt /destination/
Copies multiple files to destination directory
Copy directory recursively
cp -r source_directory destination_directory
Copies entire directory and all its contents
Interactive copy (prompt before overwrite)
cp -i file1.txt file2.txt
Prompts before overwriting existing file2.txt
Verbose copy
cp -v file1.txt file2.txt
Shows what files are being copied
Preserve file attributes
cp -p file1.txt file2.txt
Preserves timestamps, permissions, and ownership
Copy only if source is newer
cp -u source.txt destination.txt
Updates destination only if source is newer