Linux command list

cp command: copy files and directories

__ __ _ _____
\ \ / /__| |_|_ _|__ _ __ _ __ ___
\ \ /\ / / _ \ '_ \| |/ _ \ '__| '_ ` _ \
\ V V / __/ |_) | | __/ | | | | | |
\_/\_/ \___|_.__/|_|\___|_| |_| |_| |_
 
A sandbox for trying cp. Nothing here can touch your real files.
user@webterm:~/project$
 

cp copies a file and leaves the original in place. Learn how to copy under a new name, copy into a directory, duplicate a whole directory with -r, and avoid the silent overwrite, by running the commands in a real terminal in your browser.

Updated: 2026-09-05

Syntax

cp [options] source destination

The source stays put and a copy appears at the destination. If the destination is a directory, the copy lands inside it under the same name.

Try it first

The terminal on this page holds a few files and a backup/ directory.

$ ls
backup/  config.json  README.md  src/

Start by copying a file under a new name.

$ cp config.json config.backup.json

cp prints nothing when it succeeds. ls is how you confirm it happened.

$ ls
backup/  config.backup.json  config.json  README.md  src/

Copying into a directory

Name a directory as the destination and the copy goes inside it.

$ cp README.md backup/
$ ls backup
config.json  README.md

Copying a whole directory

Hand cp a directory and it refuses.

$ cp src backup
cp: -r not specified; omitting directory 'src'

Walking a tree is a different job from copying one file. -r says yes, take everything inside too.

$ cp -r src backup
$ ls backup
config.json  README.md  src/
$ ls backup/src
index.js  utils.js

When you actually reach for it

Most of the time cp is how you make a state you can return to before you break something.

SituationWhat you type
Keep a copy before editing a configcp config.json config.json.bak
Start your own settings from a samplecp .env.example .env
Duplicate a directory to experiment incp -r project project-test
Keep the timestamps as wellcp -p config.json backup/

Things that trip people up

An existing file is overwritten silently. cp does not ask. Add -i when the destination holds anything you cannot lose, and it will prompt before replacing.

$ cp -i config.json backup/config.json

Whether the destination is a directory changes the result. cp a.txt backup copies into backup when that directory exists, and creates a file called backup when it does not. Writing the trailing slash as backup/ makes your intent visible to yourself.

The original never disappears. If you only want it somewhere else, use mv. Copying and then deleting by hand is how files get lost.

Practise it hands-on

webterm.appthis site

learn.webterm.appa separate site

>_WEBTERM LEARN

WebTerm Learn: from one command to actually using it

Commands stick when they show up in a real sequence of work, not one at a time. There is a course that builds them up in order.

See the course

Related commands

mv / rm / ls

Frequently asked questions

What does cp stand for?
Copy. The source stays where it is and a duplicate appears at the destination.
What happens if the destination file already exists?
It is overwritten without a prompt. Add -i when you are copying into a place that holds files you cannot lose.
Why do I need -r to copy a directory?
Copying a directory means walking everything inside it, which is a different job. Without -r, cp refuses, so you never duplicate a huge tree by accident.
What is the difference between cp and mv?
cp leaves the original behind, mv does not. Use cp to take a backup before you change something, and mv when you only want it somewhere else.
What happens if the destination is a directory name?
The file is copied inside it under the same name. If no such directory exists, you get a file with that name instead.