cp


The cp command is used to copy a file from one location to another location. You do not alter the original file when you perform a copy. Provided that you have access to the destination directory to which you wish to copy a file, you can place as many copies of a file in that destination as you wish.

The following bullet list describes some of the types of copies you can perform with cp . Because there are many file types in UNIX (as covered in Chapter 20) and you have many ways to specify path names for the source and destination files being copied , this list might help you understand the many ways that cp can be used:

  • Copy a source file to a new file name .

  • Copy several source files to a different directory.

  • Copy several source files to the same directory.

  • Copy an entire directory to a different directory.

  • Copy several directories to different directories.

The following is an example of copying a file to a new file name within the same directory:

 $  cp krsort krsort.sav  

What if the file krsort.sav already exists? The answer is that it is replaced by the new krsort.sav being copied. To prevent such mishaps (officially called an overwrite) from occurring, you use the -i option to cp . -i asks you whether you wish to overwrite the file before the copy takes place. If your response is affirmative , then the old file is overwritten with the new file.

The following example first shows a listing of the contents of a directory. Using cp with the -i option, we copy the file krsort.c to krsortorig.c , a file that already exists. By responding n when asked whether we want to over write krsortorig.c, the file is not overwritten and no copy takes place:

 $  ls -l  total 168 -rwxr-xr-x   1 denise   users      34592 Oct 31 11:27 krsort -rwxr-xr-x   1 denise   users       3234 Oct 31 11:27 krsort.c -rwxr-xr-x  1 denise   users      32756 Oct 31 11:27 krsort.dos -rw-r--r--   1 denise   users       9922 Oct 31 11:27 krsort.q -rwxr-xr-x  1 denise   users     3085 Oct 31 11:27 krsortorig.c $  cp -i krsort.c krsortorig.c  overwrite krsortorig.c? (y/n)  n  $ 

cp - Copy files and directories.

Options

 

-i

Interactive copy whereby you are prompted to confirm that you wish to overwrite an existing file.

 

-f

Force existing files to be overwritten by files being copied if there is a conflict in file names occurs.

 

-p

Preserve permissions when copying.

 

-r

Copy recursively.

 

-R

Copy recursively, except when permissions are different.



HP-UX 11i Systems Administration Handbook and Toolkit
HP-UX 11i Systems Administration Handbook and Toolkit (2nd Edition)
ISBN: 0131018833
EAN: 2147483647
Year: 2003
Pages: 301

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net