The cp command

The cp command



What does it do ?

It allows you to copy one or more existing files.
It is most often used to make a backup copy of a file or to copy a file from one directory to another.


How is it used ?

cp copy_from_file copy_to_file
cp copy_from_file(s) copy_to_directory


Examples and remarks

** NOTE **
It is important to keep in mind that if, prior to the cp command, there exists a file with the same copy_to_file name, it will be overwritten and trashed.
Also keep in mind that complete pathnames are needed for all copy_from_files and copy_to_directories if they are not within your current working directory, regardless of whether they are located in your account or someone else's.

cp assignment.c assignment.bak
This command makes a copy of assignment.c and stores it in the current working directory as assignment.bak.

cp /home/smith/csc121/assign5 .
This command copies the file assign5 from smith's csc121 directory into your current working directory (which can be abbreviated by using the single dot - .). The copy maintains the same name of assign5.

cp assign5 /home/your_login/subdirectories (if any)
This command performs the same copy procedure as the previous example. It allows you to copy the same file assign5, but from within smith's csc121 directory. The copy_to_file directory name must include the complete path to the directory in your account in which you want the file to be copied. The copy maintains the same name of assign5.

It is also possible to copy more than one file at a time.

cp /home/smith/csc121/assign4 assign5 .
This command allows you to copy two files, assign4 and assign5, from smith's csc121 directory into your current working directory. Each file will maintain its same name.

cp *.c assignments
This command allows you to copy all files ending in .c that are in your current working directory into the assignments directory, which itself is a subdirectory of the current working directory. Each file will maintain its same name.

For more information regarding the cp command, you can access the man pages by typing
man cp at the UNIX prompt.



next table of contents