Cp (Unix)
   HOME

TheInfoList



OR:

In
computing Computing is any goal-oriented activity requiring, benefiting from, or creating computing machinery. It includes the study and experimentation of algorithmic processes, and development of both hardware and software. Computing has scientific, e ...
, cp is a
command Command may refer to: Computing * Command (computing), a statement in a computer language * COMMAND.COM, the default operating system shell and command-line interpreter for DOS * Command key, a modifier key on Apple Macintosh computer keyboards * ...
in various
Unix Unix (; trademarked as UNIX) is a family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, and ot ...
and
Unix-like A Unix-like (sometimes referred to as UN*X or *nix) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Unix-li ...
operating system An operating system (OS) is system software that manages computer hardware, software resources, and provides common services for computer programs. Time-sharing operating systems schedule tasks for efficient use of the system and may also in ...
s for
copying Copying is the duplication of information or an artifact based on an instance of that information or artifact, and not using the process that originally generated it. With analog forms of information, copying is only possible to a limited degree o ...
files and
directories Directory may refer to: * Directory (computing), or folder, a file system structure in which to store computer files * Directory (OpenVMS command) * Directory service, a software application for organizing information about a computer network's u ...
. The command has three principal modes of operation, expressed by the types of arguments presented to the program for copying a file to another file, one or more files to a directory, or for copying entire directories to another directory. The utility further accepts various command line option flags to detail the operations performed. The two major specifications are
POSIX The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines both the system- and user-level application programming interf ...
''cp'' and
GNU GNU () is an extensive collection of free software (383 packages as of January 2022), which can be used as an operating system or can be used in parts with other operating systems. The use of the completed GNU tools led to the family of operat ...
''cp''. GNU ''cp'' has many additional options over the POSIX version. The command is also available in the EFI shell.


History

cp was part of
Version 1 Unix The term "Research Unix" refers to early versions of the Unix operating system for DEC PDP-7, PDP-11, VAX and Interdata 7/32 and 8/32 computers, developed in the Bell Labs Computing Sciences Research Center (CSRC). History The term ''Resear ...
. The version of cp bundled in
GNU GNU () is an extensive collection of free software (383 packages as of January 2022), which can be used as an operating system or can be used in parts with other operating systems. The use of the completed GNU tools led to the family of operat ...
coreutils The GNU Core Utilities or coreutils is a package of GNU software containing implementations for many of the basic tools, such as cat, ls, and rm, which are used on Unix-like operating systems. In September 2002, the ''GNU coreutils'' were cr ...
was written by Torbjorn Granlund, David MacKenzie, and Jim Meyering.


Operating modes

cp has three principal modes of operation. These modes are inferred from the type and count of arguments presented to the program upon invocation. *When the program has two arguments of path names to files, the program copies the contents of the first file to the second file, creating the second file if necessary. *When the program has one or more arguments of path names of files and following those an argument of a path to a directory, then the program copies each source file to the destination directory, creating any files not already existing. *When the program's arguments are the path names to two directories, cp copies all files in the source directory to the destination directory, creating any files or directories needed. This mode of operation requires an additional option flag, typically ''r'', to indicate the recursive copying of directories. If the destination directory already exists, the source is copied into the destination, while a new directory is created if the destination does not exist.


Usage

Copying a file to another file: cp fHip--] sourcefile targetfile Copying file(s) to a directory cp fHip -sourcefile... targetdirectory Copying a directory to a directory (-r or -R must be used) cp -r, -R fHip - sourcedirectory... targetdirectory


Option flags

*-f (force) – specifies removal of the target file if it cannot be opened for write operations. The removal precedes any copying performed by the cp command. * -H (dereference) – makes the cp command follow
symbolic link In computing, a symbolic link (also symlink or soft link) is a file whose purpose is to point to a file or directory (called the "target") by specifying a path thereto. Symbolic links are supported by POSIX and by most Unix-like operating system ...
s (symlinks) so that the destination has the target file rather than a symlink to the target. *-i (interactive) – prompts with the name of a file to be overwritten. This occurs if the ''TargetDirectory'' or ''TargetFile'' parameter contains a file with the same name as a file specified in the ''SourceFile'' or ''SourceDirectory'' parameter. If one enters y (or the locale's equivalent of y), the cp command continues. Any other answer prevents the cp command from overwriting the file. *-n (no
clobbering In software engineering and computer science, clobbering a file, processor register or a region of computer memory is the process of overwriting its contents completely, whether intentionally or unintentionally, or to indicate that such an acti ...
) – prevents accidentally overwriting any files *-p (preserve) – the -p'' flag preserves the following characteristics of each source path in the corresponding target: the time of the ''last data modification'' and the ''time of the last access'', the ownership (only if it has permissions to do this), and the file permission-bits. *-R or -r (recursive) – copy directories recursively


Examples

Creating a copy of a file in the current directory: cp prog.c prog.bak This copies prog.c to prog.bak. If the prog.bak file does not already exist, the cp command creates it. If it does exist, the cp command replaces its contents with the contents of the prog.c file. Copy two files in the current directory into another directory: cp jones smith /home/nick/clients This copies the files ''jones'' to /home/nick/clients/jones and ''smith'' to /home/nick/clients/smith. Copy a file to a new file and preserve the modification date, time, and access control list associated with the source file: cp -p smith smith.jr This copies the smith file to the smith.jr file. Instead of creating the file with the current date and time stamp, the system gives the smith.jr file the same date and time as the smith file. The smith.jr file also inherits the smith file's access control protection. Copy a directory, including all its files and subdirectories, to another directory: cp -R /home/nick/clients /home/nick/customers This copies the directory ''clients'', including all its files, subdirectories, and the files in those subdirectories, to the directory ''customers/clients''. Some Unix systems behave differently in this mode, depending on the termination of directory paths. Using cp -R /home/nick/clients/ /home/nick/customers on a GNU system it behaves as expected; however, on a BSD system, it copies all the ''contents'' of the "clients" directory, instead of the directory ''clients'' itself. The same happens in both GNU and BSD systems if the path of the source directory ends in . or .. (with or without trailing slash). The copying of a file to an existing file is performed by opening the existing file in update mode, thereby preserving the files inode, which requires write access and results in the target file retaining the permissions it had originally.


Related Unix commands

*
cpio cpio is a general file archiver utility and its associated file format. It is primarily installed on Unix-like computer operating systems. The software utility was originally intended as a tape archiving program as part of the Programmer's Workb ...
– copy an entire directory structure from one place to another *
tar Tar is a dark brown or black viscous liquid of hydrocarbons and free carbon, obtained from a wide variety of organic materials through destructive distillation. Tar can be produced from coal, wood, petroleum, or peat. "a dark brown or black bit ...
– create an archive of files * link – system call to create a link to a file or directory * ln – create a link to a file or directory * mv – move a file or directory * rm – remove a file or directory *
unlink In the mathematics, mathematical field of knot theory, an unlink is a Link (knot theory), link that is equivalent (under ambient isotopy) to finitely many disjoint circles in the plane. Properties * An ''n''-component link ''L'' ⊂&nbs ...
– system call to remove a file or directory *
chmod In Unix and Unix-like operating systems, is the command (computing), command and system call used to change the File-system permissions, access permissions and the #Special modes, special mode flags (the setuid, ''setuid'', ''setgid'', and stick ...
– change the mode (aka permissions) on a file or directory *
chown The command , an abbreviation of ''change owner'', is used on Unix and Unix-like operating systems to change the owner of file system files, directories. Unprivileged (regular) users who wish to change the group membership of a file that they ...
– change ownership on a file or directory *
chgrp The (from change group) command may be used by unprivileged users on various operating systems to change the group associated with a file system object (such as a computer file, directory, or link) to one of which they are a member. A file sys ...
– change group on a file or directory *
uucp UUCP is an acronym of Unix-to-Unix Copy. The term generally refers to a suite of computer programs and protocols allowing remote execution of commands and transfer of files, email and netnews between computers. A command named is one of the prog ...
– unix to unix copy *
scp SCP may refer to: Organizations Political parties * Soviet Communist Party, the leading political party in the former Soviet Union * Syrian Communist Party * Sudanese Communist Party * Scottish Christian Party Companies * Seattle Computer Produ ...
– secure copy over SSH * progress, Linux tool to show progress for cp, mv, dd.


See also

*
GNU Core Utilities The GNU Core Utilities or coreutils is a package of GNU software containing implementations for many of the basic tools, such as cat, ls, and rm, which are used on Unix-like operating systems. In September 2002, the ''GNU coreutils'' were cr ...
*
List of Unix commands This is a list of Unix commands as specified by IEEE Std 1003.1-2008, which is part of the Single UNIX Specification (SUS). These commands can be found on Unix operating systems and most Unix-like operating systems. List See also * List of G ...
*
rsync rsync is a utility for efficiently transferring and synchronizing files between a computer and a storage drive and across networked computers by comparing the modification times and sizes of files. It is commonly found on Unix-like operat ...


References


External links

* * * * * * * {{Core Utilities commands Standard Unix programs Unix SUS2008 utilities File copy utilities Plan 9 commands Inferno (operating system) commands IBM i Qshell commands