Template:Short description Template:Lowercase {{#invoke:Infobox|infobox}}Template:Template other{{#invoke:Check for unknown parameters | check | showblankpositional=1 | unknown = Template:Main other | preview = Page using Template:Infobox software with unknown parameter "_VALUE_"|ignoreblank=y | AsOf | author | background | bodystyle | caption | collapsetext | collapsible | developer | discontinued | engine | engines | genre | included with | language | language count | language footnote | latest preview date | latest preview version | latest release date | latest release version | latest_preview_date | latest_preview_version | latest_release_date | latest_release_version | licence | license | logo | logo alt | logo caption | logo upright | logo size | logo title | logo_alt | logo_caption | logo_upright | logo_size | logo_title | middleware | module | name | operating system | operating_system | other_names | platform | programming language | programming_language | released | replaced_by | replaces | repo | screenshot | screenshot alt | screenshot upright | screenshot size | screenshot title | screenshot_alt | screenshot_upright | screenshot_size | screenshot_title | service_name | size | standard | title | ver layout | website | qid }}Template:Main other cp is a shell command for copying files and directories.

If the user has write access to a target file, the command copies the content by opening it in update mode. This preserves the file's inode instead of creating a new file with default permissions.

The command was part of Version 1 Unix,<ref name="reader">Template:Cite tech report</ref> and is specified by POSIX. The implementation from GNU has many additional options beyond the POSIX specification.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> The command is bundled in GNU Core Utilities<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> and is available in the EFI shell.<ref name="EFI-Shells-and-Scripting">{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

OptionsEdit

  • -f (force) – specifies removal of each target file if it cannot be opened for write operations; removal precedes any copying
  • -H (dereference) – follows symbolic links so that the destination has the target file rather than a link to the target
  • -i (interactive) – prompts user to overwrite each target file that clashes with a source file
  • -n (no clobbering) – prevents overwriting files
  • -p (preserve) – preserves metadata of each source file in the destination; including: time of last modification and last access, ownership, and file permissions
  • -R or -r (recursive) – copy directories recursively

ModesEdit

The command has three principal modes of operation as inferred from command-line arguments.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

Copy fileEdit

For a path to an existing file followed by a path that does not refer to an existing directory, the file at the first path is copied to the second path.

cp [-fHip][--] sourcefile targetfile

Copy files to directoryEdit

For one or more paths to existing files followed by a path to an existing directory, the files are copied to the directory.

cp [-fHip] [--] sourcefile... targetdirectory

Copy directoryEdit

With the recurse command-line option, typically <syntaxhighlight lang="text" class="" style="" inline="1">-r</syntaxhighlight>, a path to an existing directory and a second path, the files of the directory are copied to the second path. If the second path refers to nothing, the source directory is copied to that path. If the second path refers to an existing directory, the source directory is copied into the destination directory as a subdirectory.

cp -r|-R [-fHip] [--] sourcedirectory... targetdirectory

ExamplesEdit

This copies file prog.c to file prog.bak. If prog.bak does not already exist, this creates it. If it does exist, its content will be replaced.

cp prog.c prog.bak

This copies the files jones and smith into the pre-existing directory clients.

cp jones smith clients

This copies file smith to a file named smith.jr. Instead of creating a file with the current date and time stamp, the command copies the date and time from the original. The copy also receives other metadata from the original including access control protection.

cp -p smith smith.jr

This reclusively copies the directory clients, including its files, subdirectories, and the files in those subdirectories, to a new directory customers/clients.

cp -R clients customers

Some implementations behave differently in recursive mode, depending on the termination of the directory path. Using cp -R clients/ customers in the GNU implementation behaves as above. However, with a BSD implementation, it copies the contents of the clients directory, instead of the directory clients itself. The same happens in both GNU and BSD implementations if the path of the source directory ends in . or .. (with or without trailing slash).

See alsoEdit

Template:Sister project

|CitationClass=web }}</ref><ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

ReferencesEdit

Template:Reflist

External linksEdit

Template:Unix commands Template:Plan 9 commands Template:Core Utilities commands