Skip to main content

Command referencecp

cp command (Linux / bash / sh / zsh)

Linuxbashshzshfilebeginner

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 / optionMeaning
-rCopy directories recursively.
-iPrompt before overwriting.
-aPreserve attributes and copy recursively.
-vPrint 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.

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.