|Previous||Table of Contents||Next|
The external cp command copies a file. It reads the contents of a file and creates a new file or overwrites an existing file. There are two basic formats of the cp command that allow you to:
Following is the general format of the cp command.
cp [ -ip ] source_file destination_file cp [ -ipr ] source_file_list destination_directory cp -r [ -ip ] source_directory destination_directory
The following list describes the options that may be used to control how cp functions.
|-i||Interactive confirmation is required. You are prompted if the copy will overwrite an existing file. If your answer contains a y, the copy is performed. Otherwise, the copy is not performed.|
|-p||Preserve the characteristics of the source_file. Copy the contents, modification times, and permission modes of the source_file to the destination files.|
|-r||Recursively copy any source directories. If a directory is given as the source file, then all of its files and subdirectories are copied. The destination must be a directory.|
The following list describes the arguments that may be passed to the cp command.
|source_file||The existing file that will be copied|
|source_file_list||The name of files and/or directories to be copied to a new destination directory|
|destination_file||The name of the file the new copy will be named|
|destination_directory||The name of the directory where a copy of the files will be made|
|source_directory||The name of the directory from where copies are read|
The first format copies one ordinary file to a new file. The source_file is the pathname of the file being copied. The destination_file is the pathname of the file being copied to by cp.
The second format will copy one or more files to a specific directory. The source_file_list is a list of pathnames of the files being copied. If you specify a directory with the -r option, the entire directory tree is copied to the destination directory.
The third format allows you to copy an entire directory structure to a new directory.
If the destination_file exists and you have write permissions, cp will overwrite the contents of the file. The access permissions and ownership of the overwritten file will be used for the new destination_file.
If the destination_file does not exist, the access permissions of the source_file are used. Your ownership and group IDs are used for the new file if you are the user performing the cp command.
You cannot copy a file to itself.
Some common formats are:
cp letter letter.bak # make a backup copy of letter cp letter ../tmp # copy letter to temporary working directory cp letter ../LTRS # copy letter to original location (mv is better) cp /dev/tty note # copy input from keyboard to file "note"
Refer to the ln, mv, rm, chmod, chown, and ls commands described in modules 75, 91, 114, 17, 18, and 84.
The cp command is used to make a copy of an existing file. It provides a way to create backup copies of files. If you accidently erase the current copy you are working on, you can fall back to the last backup copy you made.
You can copy a file to a temporary work area ($HOME/tmp is a good place) to perform changes. Then when you finish with the updates, copy the file back to its orginal location. You may want to write a shell script to perform these few tasks.
You may also want to use it to back up an entire work directory to a backup directory. Even though this uses up disk space, it may be well worth the cost. The trade off depends on the importance of the work you are performing versus the time it would take to recover or recreate the files if they are lost.
In this activity you use the cp command to copy a file. Begin at the shell prompt.
cj> ls bin calendar db file1 letters stuff
|Previous||Table of Contents||Next|