linux · difficulty ◆
cp — copy files and directories
Make a copy of a file — or a whole tree — in one command.
$ cpWhat it does
cp copies a source file (or directory) to a destination. The simplest form, `cp source.txt dest.txt`, creates a second file with identical contents. When the destination is an existing directory, cp places a copy of the source inside it, keeping the original name. It is the everyday way to duplicate files before editing them, back them up, or stage them for a move.
Why it matters
Copying is one of the most common file operations, and cp is the tool that does it safely and predictably. Unlike a text editor's 'save as', cp works on any file type — binaries, archives, directories — and preserves metadata when you ask it to. Knowing cp well (especially -r for directories and -p for permissions) keeps you from accidentally clobbering files or losing attributes.
Example
$ cp notes.md notes-backup.md$ ls -l notes*
-rw-r--r-- 1 kmail kmail 1234 Aug 14 09:00 notes-backup.md
-rw-r--r-- 1 kmail kmail 1234 Aug 14 09:00 notes.mdCopies notes.md to a new file notes-backup.md. Both now exist with identical contents.
$ cp -r src/ src-copy/$ ls src-copy/
app.ts utils.ts index.html-r (recursive) copies a whole directory tree. Without it, cp refuses to copy a directory.
Common flags
- -r / -R
- recursive — copy a directory and everything inside it
- -p
- preserve permissions, ownership, and timestamps
- -i
- interactive — prompt before overwriting an existing file
- -v
- verbose — print each file as it is copied
- -a
- archive — recursive copy that preserves everything (like -r -p)
- -u
- update — only copy when the source is newer than the destination
History
Origin
cp has been part of Unix since the very first edition in 1971, alongside mv and rm. Its name is simply short for 'copy'. The core behavior — read the source and write a new file at the destination — has stayed essentially unchanged for over fifty years, which is a testament to how well the original design fit the job.
Fun fact
The -a (archive) flag is a GNU convenience that bundles -r with -p and a few other preservation options. It is the flag most people reach for when they want a 'true' copy of a directory — one that keeps permissions and timestamps intact rather than stamping them with the current time.