5.H: Moving Files | 5: Managing Files and Directories | 5.F: Copying Files |
The "copy" command also lets you copy all of the files in a directory,
without having copy its subdirectories. Use cp
as you would
for files, but specify the first file name as directory/*.
For example, to copy the contents of your home directory to the directory
home.bk (also in your home directory):
> cp ~/* ~/home.bk
If you want to copy the subdirectories as well, use the "recursive"
option -r
:
> cp -r ~/* ~/home.bk
There is a potential problem here which can be catastrophic (at least to the computer). Let's say you make a directory dev.bk in the development directory, and that you want to copy all of the files in development into dev.bk as a backup. You try to do this in one fell swoop from within the development directory with the command
> cp -r * dev.bk
While this is neat, the command will copy dev.bk into itself,
and then continue copying it into itself until you stop it or until you run
out of disk space. (You can stop a command pressing the
control
and c
keys simultaneously.) To avoid
infinite copies, change to a directory
one level up the directory tree, and from there issue the "copy" command:
> cd ~
> cp -i -r development dev.bk
After you are more familiar with UNIX, you may want to save yourself
some typing by using wildcards, such as the *
in
these examples. The Edinburgh
tutorials are good for that.
Remember, it's a good idea to use the "list" command ls
before you copy in order to check if files will be overwritten.
5.H: Moving Files | 5: Managing Files and Directories | 5.F: Copying Files |