4.4.2.1. cp
The cp program can put a copy of a
file into the same directory or into another directory.
cp doesn't affect the original file,
so it's a good way to keep an identical backup of a file.
To copy a file, use the command:
cp old new
where old is a pathname to the original file
and new is the pathname you want for the copy.
For example, to copy the /etc/passwd file into a
file called password in your
working directory, you would enter:
$ cp /etc/passwd password
$
You can also use the form:
cp old olddir
This puts a copy of the original file old
into an existing directory olddir.
The copy will have the same filename as the original.
If there's already a file with the same name as the copy,
cp
replaces the old file with your new copy.
This is handy when you want to replace an old copy with a newer version,
but it can cause trouble if you accidentally overwrite a copy you wanted
to keep.
To be safe, use ls to list the
directory before you make a copy there.
Also, many versions of cp have an
-i (interactive) option that
asks you before overwriting an existing file.
You can copy more than one file at a time to a single directory
by listing the pathname of each file you want copied, with the
destination directory at the end of the command line.
You can use relative or absolute pathnames (see
Section 3.1 in Chapter 3)
as well as simple filenames.
For example, let's say your working directory is
/users/carol (from the filesystem diagram in
Figure 3-1).
To copy three files called ch1,
ch2, and ch3 from
/users/john to a subdirectory called
work (that's
/users/carol/work), enter:
$ cp ../john/ch1 ../john/ch2 ../john/ch3 work
Or, you could use wildcards and let the shell find all the
appropriate files.
This time, let's add the -i option
for safety:
$ cp -i ../john/ch[1-3] work
cp: overwrite work/ch2? n
There is already a file named ch2 in the
work directory.
When cp asks, answer
n to prevent copying
ch2.
Answering y would overwrite the old
ch2.
As you saw in
Section 3.1.5.2 in
Chapter 3,
the shorthand form . puts the copy in the working
directory, and .. puts it in the parent directory.
For example, the following puts the copies into the working directory:
$ cp ../john/ch[1-3] .
cp can also copy entire directory trees.
Use the option -R, for "recursive."
There are two arguments after the option: the pathname of the
top-level directory you want to copy from, and the pathname of the
place where you want the top level of the copy to be.
As an example, let's say that a new employee, Asha, has joined John and Carol.
She needs a copy of John's work directory in her
own home directory.
See the filesystem diagram in Figure 3-1.
Her home directory is /users/asha.
If Asha's own work
directory doesn't exist yet
(important!), she could type the following commands:
$ cd /users
$ cp -R john/work asha/work
Or, from her home directory, she could have typed
"cp -R ../john/work work".
Either way, she'd now have a new subdirectory
/users/asha/work with a copy of all files and
subdirectories from /users/john/work.
WARNING:
If you give cp -R the wrong pathnames,
it can copy a directory tree into itself--running forever until
your filesystem fills up!
If the copy seems to be taking a long time, stop
cp with CTRL-Z,
then explore the filesystem
(ls -RF is handy for this).
If all's okay, you can resume the copying by putting the
cp job in the background
(with bg) so it can finish its slow work.
Otherwise, kill cp and do some
cleanup--probably with rm -r, which
we mention in Section 4.4.5.2
later in this chapter.
(See Section 7.1
and Section 7.3
in Chapter 7.)