Cp (Unix)
Encyclopedia
cp is a UNIX
command
used to copy a file
. Files can be copied either to the same directory or to a completely different directory, possibly on a different file system
or hard disk drive. If the file is copied to the same directory, the new file must have a different name to the original; in all other cases, the new file may have the same or a different name. The original file remains unchanged.
The cp command now has a number of variations; two major variations are POSIX
cp and GNU
cp. GNU cp has many additional options.
To copy a file to a directory
To copy a directory to a directory (-r or -R must be used)
-L (dereference) – makes the cp command follow symbolic links (symlinks) so that the destination has the target file rather than a symlink to the target.
-i (interactive) – prompts you 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 you enter y or the locale's equivalent of y, the cp command continues. Any other answer prevents the cp command from overwriting the file.
-p (preserve) – duplicates the following characteristics of each SourceFile/SourceDirectory in the corresponding TargetFile and/or TargetDirectory:
-R or -r (recursive) – copy directories (recursively copying all the contents)
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.
To copy a file in your current directory into another directory, enter:
This copies the jones file to /home/nick/clients/jones.
To copy a file to a new file and preserve the modification date, time, and access control list associated with the source file, enter:
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.
To copy all the files in a directory to a new directory, enter:
This copies only the files in the clients directory to the customers directory.
To copy a directory, including all its files and subdirectories, to another directory, enter:
This copies the clients directory, including all its files, subdirectories, and the files in those subdirectories, to the customers/clients directory. Be careful about including a trailing slash in the source directory, however. If you run
To copy a specific set of files to another directory, enter:
This copies the jones, lewis, and smith files in your current working directory to the /home/nick/clients directory.
To use pattern-matching characters to copy files, enter:
This copies the files in the programs directory that end with .c to the current directory, signified by the single . (dot). You must type a space between the c and the final dot.
Copying a file to an existing file is done by opening the existing file in update mode which requires write access and results in the target file retaining the permissions it had originally.
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...
command
Command (computing)
In computing, a command is a directive to a computer program acting as an interpreter of some kind, in order to perform a specific task. Most commonly a command is a directive to some kind of command line interface, such as a shell....
used to copy a file
Computer file
A computer file is a block of arbitrary information, or resource for storing information, which is available to a computer program and is usually based on some kind of durable storage. A file is durable in the sense that it remains available for programs to use after the current program has finished...
. Files can be copied either to the same directory or to a completely different directory, possibly on a different file system
File system
A file system is a means to organize data expected to be retained after a program terminates by providing procedures to store, retrieve and update data, as well as manage the available space on the device which contain it. A file system organizes data in an efficient manner and is tuned to the...
or hard disk drive. If the file is copied to the same directory, the new file must have a different name to the original; in all other cases, the new file may have the same or a different name. The original file remains unchanged.
The cp command now has a number of variations; two major variations are POSIX
POSIX
POSIX , an acronym for "Portable Operating System Interface", is a family of standards specified by the IEEE for maintaining compatibility between operating systems...
cp and GNU
GNU
GNU is a Unix-like computer operating system developed by the GNU project, ultimately aiming to be a "complete Unix-compatible software system"...
cp. GNU cp has many additional options.
Usage
To copy a file to another fileTo copy a file to a directory
To copy a directory to a directory (-r or -R must be used)
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.-L (dereference) – makes the cp command follow symbolic links (symlinks) so that the destination has the target file rather than a symlink to the target.
-i (interactive) – prompts you 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 you enter y or the locale's equivalent of y, the cp command continues. Any other answer prevents the cp command from overwriting the file.
-p (preserve) – duplicates the following characteristics of each SourceFile/SourceDirectory in the corresponding TargetFile and/or TargetDirectory:
- The time of the last data modification and the time of the last access.
- The user ID and group ID (only if it has permissions to do this)
- The file permission bits and the SUIDSetuidsetuid and setgid are Unix access rights flags that allow users to run an executable with the permissions of the executable's owner or group...
and SGIDSetuidsetuid and setgid are Unix access rights flags that allow users to run an executable with the permissions of the executable's owner or group...
bits.
-R or -r (recursive) – copy directories (recursively copying all the contents)
Examples
To make a copy of a file in the current directory, enter: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.
To copy a file in your current directory into another directory, enter:
This copies the jones file to /home/nick/clients/jones.
To copy a file to a new file and preserve the modification date, time, and access control list associated with the source file, enter:
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.
To copy all the files in a directory to a new directory, enter:
This copies only the files in the clients directory to the customers directory.
To copy a directory, including all its files and subdirectories, to another directory, enter:
This copies the clients directory, including all its files, subdirectories, and the files in those subdirectories, to the customers/clients directory. Be careful about including a trailing slash in the source directory, however. If you run
cp -R /home/nick/clients/ /home/nick/customers
on a GNU-based system, it does the same thing as without the slash; however, if you run the same thing on a BSD-based system, it will copy all the contents of the "clients" directory over, instead of the "clients" directory itself. The same will happen in both GNU-based and BSD-based systems if the path of the source directory ends in . or .. (with or without trailing slash).To copy a specific set of files to another directory, enter:
This copies the jones, lewis, and smith files in your current working directory to the /home/nick/clients directory.
To use pattern-matching characters to copy files, enter:
This copies the files in the programs directory that end with .c to the current directory, signified by the single . (dot). You must type a space between the c and the final dot.
Copying a file to an existing file is done by opening the existing file in update mode which requires write access and results in the target file retaining the permissions it had originally.
Related Unix commands
- cpioCpiocpio 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 Workbench , and has been a component of virtually every...
– copy an entire directory structure from one place to another - tar – create an archive of files
- linkLink (Unix)The link utility is a Unix command line program that creates a hard link from an existing directory entry to a new directory entry. It does no more than call the link system function. It does not perform error checking before attempting to create the link. It returns an exit status that indicates...
– system call to create a link to a file or directory - lnLn (Unix)ln is a standard Unix command used to create links to files.- Link files :Links allow more than one file name to refer to the same file, elsewhere.There are two types of links, both of which are created by ln:...
– create a link to a file or directory - mvMvmv is a Unix command that moves one or more files or directories from one place to another. Since it can "move" files from one filename to another, it is also used to rename files. Using mv requires the user to have write permission for the directories which the file will move between...
– move a file or directory - rmRm (Unix)rm is a basic UNIX command used to remove objects such as files, directories, device nodes, symbolic links, and so on from the filesystem...
– remove a file or directory - unlinkUnlink (Unix)In Unix-like operating systems, unlink is a system call and a command line utility to delete files. The program directly interfaces the system call, which removes the file name and directories like rm and rmdir...
– system call to remove a file or directory - chmodChmodThe chmod command is a Unix command that lets a user tell the system how much access it should permit to a file. It changes the file system modes of files and directories. The modes include permissions and special modes...
– change the mode (aka permissions) on a file or directory - chownChownThe chown command is used on Unix-like systems to change the owner of a file. In most implementations, it can only be executed by the superuser. Unprivileged users who wish to change the group of a file that they own may use chgrp.-Usage examples:These examples illustrate typical syntax and use...
– change ownership on a file or directory - chgrpChgrpThe chgrp command is used by unprivileged users on Unix-like systems to change the group associated with a computer file...
– change group on a file or directory - uucpUUCPUUCP is an abbreviation for 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. Specifically, a command named uucp is one of the programs in the suite; it...
– unix to unix copy - scpSecure copySecure Copy or SCP is a means of securely transferring computer files between a local and a remote host or between two remote hosts. It is based on the Secure Shell protocol....
– secure copy over SSH