DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Working with files and directories

Copying a file

To copy one or more files, use the cp(C) command, which takes one of the following formats:

cp filename copyname
cp filename ... pathname

In the first format, filename (with optional path) is the name of the existing file that you want to be copied; copyname (with optional path) is the name you want the copy to be created with.

If you are using the second format to copy a group of files, you can only specify a directory, pathname, as the destination of the specified files. filename and copyname cannot be the same if they are both in the same directory.

When you copy a file you are creating a duplicate of it, which occupies additional space in the filesystem. Although the contents of the new file are the same as those of the original file, the new copy has its own inode number; any operations carried out on it have no impact on the original.

For example, to copy the file project1 from your current directory to the directory /u/workgrp, type the following command:

   $ cp project1 /u/workgrp
The copy will retain the name project1, but will have a different pathname.

You can copy a file to your current directory by typing a command line like the following:

   $ cp ../../a.out .
In this case, the file called a.out is located two levels above the current working directory, and is to be copied to the current location (as indicated by the ``.'' notation).


NOTE: When copying a file, be careful not to overwrite an existing file. To avoid this, do not create a copy with the same name as an existing file, as this will overwrite (clobber) the contents of the existing file. This can be avoided by setting the noclobber variable. See ``More about redirecting input and output'' for details.

When you copy a file, you automatically become its new owner. Accordingly, you must have read permission on a file before you can copy it. You can place files in any directory for which you have write permission. If you want to create a copy of a file without changing its ownership, use the command copy -o instead of cp; this preserves the owner and group of the file. For example, to copy /tmp/johnsfile to your home directory without changing the ownership of the file, type the following:

   $ copy -o /tmp/johnsfile johnsfile
For information on how you can assign the ownership of a file to someone else, see ``Giving a file to someone else'' and ``Access control for files and directories''.
Next topic: Moving or renaming a file
Previous topic: Looking at the beginning and end of a file

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003