Section 11.8:Manipulating Files at a Shell Prompt
11.8.1 Copying Files
Like so many Linux features, you have a variety of options from which to choose when you want
to manipulate files and directories. You can also use wildcards when you are copying, moving, or
deleting files and directories.
To copy a file, type the following command:
cp <source> <destination>
So, to copy the file sneakers.txt to the directory tigger in your login directory, move to your
login directory and type:
cp sneakers.txt tigger
Notice that you also used relative pathnames to copy the file. You can use both relative and absolute
pathnames with cp. Our login directory is the parent of the directory tigger; tigger is one di-
rectory down from our login directory.
Read the cp man page (man cp) for a full list of the options available with cp. Among the options
you can use with cp are the following:
•
-i — interactive. Prompts you to confirm if the file is going to overwrite a file in your destination.
This is a handy option because it can help prevent you from making mistakes.
•
-r — recursive. Rather than just copying all the files and directories, this will copy the whole
directory tree, subdirectories and all.
•
-v — verbose. shows the progress of the files being copied.
If you use cp with no options, you will not see much when the command is executed. Using an
option, such as -i, can make the process a little more useful. If you want to copy a file to a location
that already has a file with the same name, you will be asked first if you really want to overwrite (or
replace) the file that is already there.
Now that you have the file sneakers.txt in the tigger directory, use cp -i to copy the file
again to the same location.
[newuser@localhost newuser]$
cp -i sneakers.txt tigger
cp: overwrite 'tigger/sneakers.txt'?
To overwrite the file that is already there, press
the file, press
and
[N]
.
[Enter]
[Y]
and then
[Enter]
. If you do not want to overwrite
133
Need help?
Do you have a question about the LINUX 7.2 and is the answer not in the manual?
Questions and answers