Files & folders

Linux cp command

Copy files. Add -r when copying a directory and its contents.

Availability: Common Linux commands; shell built-ins and external implementations may differ by distribution.中文

Syntax

cp [OPTION]... SOURCE... DESTINATION

Examples

Copy a file and ask before overwriting.

cp -i report.txt ./archive/

Copy a directory tree while preserving attributes.

cp -a project/ backup/project/

Parameters, options and clauses

Option / clauseDescription
-a, --archivePreserve attributes and copy recursively; equivalent to -dR --preserve=all.
-r, -R, --recursiveCopy directories recursively.
-i, --interactivePrompt before overwriting a destination.
-n, --no-clobberDo not overwrite an existing destination.
-u, --updateCopy when the source is newer or destination is missing.
-p, --preserve=mode,ownership,timestampsPreserve selected file attributes.
-v, --verboseExplain what is being copied.
Safety note

Review a command and its target before running it. Commands that alter files, services, accounts, disks, permissions or databases may be difficult to reverse. Test in a safe environment and keep backups.

Official reference ↗Back to directory