Navigating the Linux File System
◾
83
If the mv operation would cause the file to be moved to the destination location where a
file of the same name already exists, the user is prompted to see if the mv should take place
and thus overwrite the existing file in the destination directory.
You can force mv to oper-
ate without prompting the user by supplying the -f option. The -i option will prompt the
user before overwriting the file (-i is the default so it does not need to be specified).
The copy command is
cp
. It’s format is much the same as mv
cp [
options
]
source destination
Unlike mv, the file is physically copied so that two versions will now exist. There are
three different combinations of source and destination that can be used. First, the destina-
tion specifier can be another directory in which case the file is
copied into the new direc-
tory and the new file is given the same name as the original file. Second, if destination is
both a directory and file name, then the file is copied into the new directory and given a
different name. Third, the destination is a filename in which case the file is copied into the
current directory under the new filename.
Let us look at some examples.
cp foo.txt ~zappaf/foo1.txt
will copy the file foo.txt from the current working directory to zappaf’s home directory,
but name the new file foo1.txt rather than foo.txt.
Source can consist of multiple files by using wildcards. The following command pro-
vides an example:
cp *.txt ~
In this command, all files in the current directory that end with a .txt extension are
copied to the user’s home directory.
Note that if the source and destination contain directories and they are the same direc-
tory, then cp will only work if the destination filename is different
from the source file-
name. For instance,
cp foo1.txt .
will not work because you cannot copy foo1.txt onto itself.
The copy command has a number of different options available. The most common
options are shown in Table 3.4.
Without the -p option, all copied items are given the current user’s ownership. For
instance, if the files foo1.txt, foo2.txt, and foo3.txt in zappaf are all owned by zappaf with
the group cit371, the operation
cp ~zappaf/foo*.txt .
84
◾
Linux with Operating System Concepts
will copy each of these three files to your current working directory,
and change the owner
and group to be yours. Similarly, creation date and time are given the current date and time.
But if you use -p, the copied items have the same owner, group, creation date and time, and
permissions as the original files. This may be useful when you are copying files as root.
As noted in Table 3.4, cp has -i and -f options that are essentially the same as those found
with the mv command. However, cp has another option worth discussing, -r. What does
a
Do'stlaringiz bilan baham: