cp command (Linux / bash / sh / zsh)
What it does
Copy files or directory trees to another location. When people search for the cp command linux, they usually want cp's everyday flags, safe examples, and how it differs from related tools. Below you'll find syntax, options, and copyable examples for Bash/sh/zsh.
Copy files or directory trees to another location.
Syntax
cp [OPTION]... SOURCE... DESTINATION
Common options
| Flag / option | Meaning |
|---|---|
| -r | Copy directories recursively. |
| -i | Prompt before overwriting. |
| -a | Preserve attributes and copy recursively. |
| -v | Print each copied path. |
Practical examples
Copy a file
cp report.txt backup/
Copies one file into backup.
Copy a tree
cp -a src/ src-backup/
Creates an attribute-preserving directory copy.
Confirm overwrite
cp -i config.ini /etc/app/
Prompts if the destination file exists.
Linux ↔ Windows equivalent
Closest match: Copy-Item (Windows / PowerShell). Copy items between locations supported by PowerShell providers.
Common mistakes
- Forgetting `-r` or `-a` when copying directories.
- Reversing source and destination and overwriting the wrong file.
Related commands
- mvMove or rename files and directories.
- scpCopy files securely between local and SSH-accessible hosts.
- Copy-ItemCopy items between locations supported by PowerShell providers.
- lsList directory entries and inspect file metadata.
- cdChange the shell's current working directory.
- dirList files and subdirectories with names, dates, sizes, and attributes.
- findWalk directory trees and select entries using tests and actions.
- Get-ChildItemEnumerate items and child items in filesystem, registry, and other providers.
FAQ
What does cp do in Linux?
Copy files or directory trees to another location.
How do I use cp from the terminal?
Start with `cp [OPTION]... SOURCE... DESTINATION`, then choose only the options needed for the task.
What is a practical cp example?
Try `cp report.txt backup/` — Copies one file into backup.
Can I use cp in a shell script?
Yes. cp works in Bash, sh, and Zsh, but scripts should handle quoting, errors, and command output explicitly.
Browse the full Linux and Windows command reference to search more bash, CMD, PowerShell, Conda, and GitHub CLI commands.