cp

Copy one or more files.

Format

   cp [options] source-file destination-file
   cp [options] source-file-list destination-directory

Summary

The cp utility copies one or more ordinary files, including text and executable program files. It has two modes of operation: The first copies one file to another, and the second copies one or more files to a directory.

Arguments

The source-file is the pathname of the ordinary file that cp is going to copy. The destination-file is the pathname that cp will assign to the resulting copy of the file.

The source-file-list is one or more pathnames of ordinary files that cp is going to copy. When you use the -r option, the source-file-list can also contain directories. The destination-directory is the pathname of the directory in which cp places the resulting copied files.

When you specify a destination-directory, cp gives each of the copied files the same simple filename as its source-file. If, for example, you copy the text file /home/turing/z123456/memo.416 to the /home/turing/z123456/archives directory, the copy will also have the simple filename memo.416, but the new pathname that cp gives it will be /home/turing/z123456/archives/memo.416.

Options

-i interactive This option causes cp to prompt the user whenever the copy will overwrite an existing file. After you enter y in response to the prompt, cp will continue. If you enter anything other than y, cp will not make the copy.
-r recursive Use this option when the destination is a directory. If any of the files in the source-file-list is a directory, the -r will cause cp to copy the contents of that directory and any of its subdirectories into the destination-directory. The subdirectories themselves are copied as well as the files they contain.

Notes

If the destination-file exists before you execute cp, cp overwrites the file, destroying the contents but leaving the access privileges and owner associated with the file as they were.

If the destination-file does not exist, cp uses the access privileges for the source-file. The user becomes the owner of the destination-file, and the user's group becomes the group associated with the destination-file.

Examples

The first command makes a copy of the file letter in the working directory. The name of the copy is letter.sav.

z123456@turing:~$ cp letter letter.sav

The next command copies all the files with filenames ending in .cpp in the directory /home/turing/z123456/CS241/Assign1 into the directory /home/turing/z123456/CS241/Assign2. Each copied file retains its simple filename but has a new absolute pathname.

z123456@turing:~/CS241$ cp Assign1/*.cpp Assign2

The next example copies the file memo from the /home/turing/z123456 directory to the working directory.

z123456@turing:~/CS241$ cp /home/turing/z123456/memo .

The final command copies two files named memo and letter into the user's home directory. The copies have the same simple filenames as the source files (memo and letter) but have different absolute pathnames. The absolute pathnames of the copied files are /home/turing/z123456/memo and /home/turing/z123456/letter.

z123456@turing:~/workfiles$ cp memo letter ~