Back to the command reference

cp

FilesBeginner In simulator In real Linux

Copy files and directories

Name: cp = copy

You can practise it right away in the simulator, and use it in real Linux mode too.

Description

cp copies files. The last argument is the destination: if it is an existing directory the sources are copied into it, otherwise the copy gets that name.

Directories need -r. By default the copy belongs to you and gets the current time; -p or -a preserve the original mode, ownership and timestamps.

Syntax

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

Common options

-r / -R / --recursiveCopy directories recursively
-a / --archiveArchive: copy recursively, keeping permissions, times, owners and links (for backups)
-i / --interactiveAsk before overwriting
-n / --no-clobberNever overwrite an existing file
-u / --updateCopy only when the source is newer or the destination is missing
-f / --forceIf a destination cannot be opened, remove it and try again
-v / --verboseExplain what is being done
-pPreserve mode, ownership and timestamps
-l / --linkMake hard links instead of copying
-s / --symbolic-linkMake symbolic links instead of copying
-L / --dereferenceCopy what symbolic links point to
-P / --no-dereferenceCopy symbolic links as links
-t / --target-directory DIRCopy everything into DIR (the destination first, handy with xargs)
-b / --backupBack up a file before overwriting it, as name~

Examples

  • $ cp README.md README.bak

    Make a backup copy of a file

    Explain
  • $ cp data/*.csv documents/

    Copy several files into a directory

    Explain
  • $ cp -r projects projects-copy

    Copy a whole directory

    Explain

Related