Home·Terminal commands

cp

Files and directories

Usage

cp -r [source] [dest]

Copies files or whole trees; whether the destination already exists decides if the source becomes that name or lands inside it.

Common flags

FlagMeaning
-rCopy directories and their contents
-aArchive: -r plus permissions, times and symlinks kept
-pKeep mode, owner and timestamps
-iAsk before overwriting
-nNever overwrite a file that already exists
-vName each file as it is copied
-uGNU: copy only when the source is newer

Examples

cp -a site/ backup/

Tree copied with permissions and times intact.

cp config.yml config.yml.bak

Quick backup beside the original.

cp -r src/. dest/

Copies the contents of src into an existing dest.

Moving, deleting and finding. Some of these do not ask twice, which is why -i becomes a habit.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does cp do?

Copies files or whole trees; whether the destination already exists decides if the source becomes that name or lands inside it.

Q. How do I type it?

cp -r [source] [dest] — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

7 are listed here; the full set is in man cp. This command sits under Files and directories.

Related commands

man page: man cp