files

cp

Copies files and directories from one location to another. Preserves or transforms metadata depending on flags.

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

Common flags

Flag Purpose
-r Recursively copy directories
-a Archive mode: preserve attributes, links, and recurse
-i Prompt before overwriting an existing file
-u Copy only when SOURCE is newer than DEST or DEST is missing
-p Preserve mode, ownership, and timestamps
-v Verbose: print each file as it is copied

Examples

cp file.txt backup.txt

Duplicate a single file

cp -a src/ dst/

Recursively copy a directory tree preserving attributes

cp -uv *.log /mnt/backup/

Sync only newer log files verbosely

cp -i important.conf important.conf.bak

Interactively back up a config to avoid clobbering

Gotcha

Unlike rsync, a trailing slash on SOURCE does NOT change cp's behavior — `cp -a src/ dst` and `cp -a src dst` behave identically. To copy just a directory's contents, use `cp -a src/. dst/` or `cp -a src/* dst/` (the latter misses dotfiles).

Related commands

← All Linux commands · Linux cheat sheet