cp — Linux basic command
Linux / Basic Linux commands / cp.html
A command that copies files or directories.
Basic Information
| Command Name | cp (copy) |
|---|---|
| One-line Introduction | A command that copies files or directories. |
| Provided by | GNU coreutils (installed by default in all Linux distributions) |
| Official Documentation | https://man7.org/linux/man-pages/man1/cp.1.html |
| Supported Operating Systems | Linux, macOS, BSD, WSL |
| Installation Status | Installed by default (no separate installation needed) |
| Category | Linux basic commands (tags: file copy, directory, Linux, basic) |
What is this command?
cp is an abbreviation for copy, a command that copies files or directories. The original remains, and a copy is created.
It provides various options from single file copying to recursive directory copying, preserving attributes, preventing overwriting, etc.
What problems does it solve?
- Backs up files.
- Duplicates and modifies configuration files.
- Copies an entire directory to another location.
- Copies files safely without overwriting.
Main Features
Single File Copy
Copies a file to a different name or location.
- Usage Scenario: When file backup or duplication is needed.
- Result Obtained: A copy identical to the original is created.
- Limitations: If a file with the same name exists in the destination, it will be overwritten.
Recursive Directory Copy (-r)
Copies the directory and all its sub-items together.
- Usage Scenario: When cloning an entire project folder.
- Result Obtained: The subdirectory and file structure is copied as-is.
- Limitations: Copying a directory without -r will cause an error.
Preserving Attributes (-p)
Preserves attributes such as permissions, owner, and modification time.
- Usage Scenario: When attributes need to be preserved in distribution scripts.
- Result Obtained: The copy has the same attributes as the original.
- Limitations: Changing the owner requires root privileges.
Preventing Overwriting (-n)
Skips if a file with the same name exists in the destination.
- Usage Scenario: When you want to avoid accidentally overwriting important files.
- Result Obtained: Existing files are protected.
- Limitations: Skipped files have no notification, so manual checking is required.
Installation and Execution
Check Default Installation
It is included by default in all Linux distributions.
which cp
cp --version
Separate Installation (Lightweight Environments)
It is usually already installed.
sudo apt install coreutils # Debian/Ubuntu
sudo dnf install coreutils # Fedora
Actual Usage Examples
Basic File Copy
Copies a file.
# Copy file
cp report.txt report-backup.txt
# Copy to another directory
cp report.txt /var/backups/
# Copy multiple files at once
cp file1.txt file2.txt /tmp/
Recursive Directory Copy
Copies an entire directory.
# Copy entire directory (-r option required)
cp -r project/ project-backup/
# Copy while preserving attributes
cp -rp /etc/nginx/ /tmp/nginx-backup/
Safe Copy (Preventing Overwriting)
Copies while protecting existing files.
# Skip if file with same name exists
cp -n config.txt /etc/app/
# Add backup timestamp
cp config.txt config.txt.$(date +%Y%m%d).bak