![]() Example usage of cp command | |
Original author(s) | AT&T Bell Laboratories |
---|---|
Developer(s) | Variousopen-source andcommercial developers |
Initial release | November 3, 1971; 53 years ago (1971-11-03) |
Written in | Plan 9:C |
Operating system | Unix,Unix-like,Plan 9,Inferno,KolibriOS |
Platform | Cross-platform |
Type | Command |
License | coreutils:GPLv3 Plan 9:MIT License |
Incomputing,cp
is acommand in variousUnix andUnix-likeoperating systems forcopyingfiles anddirectories. 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.[1]
The utility further accepts various command line option flags to detail the operations performed. The two major specifications arePOSIXcp andGNUcp. GNUcp has many additional options over the POSIX version.[2]
The command is also available in theEFI shell.[3]
cp was part ofVersion 1 Unix.[4] The version ofcp
bundled inGNUcoreutils was written by Torbjorn Granlund, David MacKenzie, and Jim Meyering.[5]
cp has three principal modes of operation. These modes are inferred from the type and count of arguments presented to the program upon invocation.
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
-f
(force) – specifies removal of the target file if it cannot be opened for write operations. The removal precedes any copying performed by thecp
command.-H
(dereference) – makes thecp
command followsymbolic links (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 theTargetDirectory orTargetFile parameter contains a file with the same name as a file specified in theSourceFile orSourceDirectory parameter. If one entersy
(or the locale's equivalent ofy
), thecp
command continues. Any other answer prevents thecp
command from overwriting the file.-n
(noclobbering) – 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 thelast data modification and thetime 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 recursivelyCreating 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 filesjones to /home/nick/clients/jones andsmith 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 directoryclients, including all its files, subdirectories, and the files in those subdirectories, to the directorycustomers/clients. Some Unix systems behave differently in this mode, depending on the termination of directory paths. Usingcp -R /home/nick/clients/ /home/nick/customers
on a GNU system it behaves as expected; however, on a BSD system, it copies all thecontents of the "clients" directory, instead of the directoryclients 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.
cp
– Shell and Utilities Reference,The Single UNIX Specification, Version 4 fromThe Open Groupcp(1)
– FreeBSD General CommandsManualcp(1)
– NetBSD General CommandsManualcp(1)
– OpenBSD General CommandsManualcp(1)
– Solaris 11.4 User Commands ReferenceManualcp(1)
– Linux User CommandsManualcp(1)
– Plan 9 Programmer's Manual, Volume 1