cp

Make a copy of a file, copy several files into a folder, or copy a whole folder with -r.

>_ Try it: a simulated Linux, right here
Loading the terminal…

Examples

  • cp todo.txt todo.bak
  • mkdir backup
  • cp -v todo.txt ideas.txt backup/
  • cp -r backup backup-2
  • ls backup-2
  • cp -n ideas.txt todo.txt

How it works

cp SOURCE DEST copies a file. If DEST is a folder that exists, the copy goes inside it with the same name; otherwise DEST is the new name.

Folders need -r (recursive): cp -r project project-backup copies the folder and everything in it. Careful: cp overwrites an existing file without asking, unless you add -n.

Syntax

cp [OPTION]... SOURCE... DEST

Options

OptionWhat it does
-rcopy folders and everything inside them
-vsay what was copied
-nnever overwrite a file that already exists

Good to know

  • To copy several files, the last name must be a folder: cp a.txt b.txt backup/.
  • A copy is a new file: changing todo.bak later doesn't touch todo.txt.