The cp Command

The cp command is used to copy files and directories. The copies become independent of the originals (i.e., a subsequent change in one will not affect the other).

cp's basic syntax is

cp [options] name new_name

As a safety precaution, by default cp only copies files and not directories. If a file with the same name as that assigned to the copy of a file (or a directory with the same name as that assigned to the copy of a directory) already exists, it will be overwritten (i.e., its contents will be lost). However, the owner, group and permissions for the copy become the same as those of the file with the same name that it replaced. The last access time of the source file and the last modification time of the new file are set to the time the copying was performed.

When a copy is made of a file or directory, the copy must have a different name than the original if it is to be placed in the same directory as the original. However, the copy can have the same name if it is made in a different directory. Thus, for example, a file in the current directory (i.e., the directory in which the user is currently working) named file1 could be copied with the same name into another directory, such as into /home/john/, as follows:

cp file1 /home/john/file1

Any number of files can be simultaneously copied into another directory by listing their names followed by the name of the directory. cp is an intelligent command and knows to do this when only the final argument (i.e., piece of input data) is a directory. The files copied into the directory will all have the same names as the originals. Thus, for example, the following would copy the files named file2, file3 and file4 into a directory named dir1:

cp file2 file3 file4 dir1

The -r (i.e., recursive) option, which can also be written with an upper case R, allows directories including all of their contents to be copied. (Directories are not copied by default in order to make it more difficult for users to accidentally overwrite existing directories which have the same name as that assigned to the copy being made and which might contain critical directory structures or important data.) Thus, for example, the following command would make a copy of an existing directory called dir2, inclusive of all it contents (i.e., files, subdirectories, their subdirectories, etc.), called dir3:

cp -r dir2 dir3

The -i (i.e., interactive) option prompts the user in the event that any name assigned to a copy is already in use by another file and that file would thus be overwritten. Entering the letter y (either lower case or upper case) in response to the prompt causes the command to continue; any other answer prevents the command from overwriting the file. Thus, for example, if it is desired to make a copy of a directory called dir4 and call it dir5 and if a directory named dir4 already exists, the following would prompt the user prior to replacing any files with identical names in the latter directory:

cp -ri dir4 dir5

The -a option preserves as much of the structure and attributes of the original directory and its contents as possible in the new directory and is thus useful for creating archives. It is similar to the -r option in that it copies directories recursively; however, it also never follows symbolic links. It is equivalent to the -rdp combination of options.

All the files in a directory can be copied to another directory by using the star wildcard. The star character represents any single character or any combination of characters. Thus, for example, the following would copy all of the files in a directory named dir6 into another existing directory called dir7:

cp dir6/* dir7

cp can also be used with the star wildcard or other pattern matching characters to selectively copy files and directories. For example, to copy all of the files in the current directory that have the filename extension .html into another existing directory called dir8, the following would be used:

cp *.html dir8

In this case, the star wildcard represents anything whose name ends with the .html extension.

Among the other options for cp are -b, which makes backup copies of each destination file, -f (i.e., force), which removes destination files that cannot be opened and tries again, -s, which makes symbolic links instead of copying, -u (i.e., update), which copies only if the source file is newer than the destination file or if the destination file is missing, -v (i.e., verbose), which makes brief comments about what is going on, and -x, which tells cp to stay on the same filesystem.

Created January 21, 2006.
Copyright © 2006 The Linux Information Project. All Rights Reserved.