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
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.
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/
Name a directory as the destination and the copy goes inside it.
$ cp README.md backup/
$ ls backup
config.json README.md
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
Most of the time cp is how you make a state you can return to before you break something.
| Situation | What you type |
|---|---|
| Keep a copy before editing a config | cp config.json config.json.bak |
| Start your own settings from a sample | cp .env.example .env |
| Duplicate a directory to experiment in | cp -r project project-test |
| Keep the timestamps as well | cp -p config.json backup/ |
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.
webterm.appthis site
Terminal Fundamentals
Learn commonly used commands
learn.webterm.appa separate site

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