next up previous contents
Next: Removing Directories and Up: Moving and Copying Previous: Moving Files and

Copying Files

If we copy a file we create a completely independent copy of the file possibly in a different directory or with a different name. We copy files using the cpgif command. As the mv command the cp command takes a source and destination as parameters. Let us now create a copy of the file 'd' in the 'programs' directory:

$cd programs
$ls -F
d   f*
$cp d e
$ls -F
d   e   f*
$

If we want to copy a file to a different directory we can simply specify that directory as the second parameter to the cp command:

$ls -F ..
a          b          c          data/      junk/      programs/
$cp d ..
$ls -F ..
a          b          c          d          data/      junk/      programs/
$

If we want to change the name of the file while copying we just append it to the destination directory name:

$cp d ../p
$ls -F ..
a          c          data/      p
b          d          junk/      programs/
$

Now let us remove those files and try to copy a whole directory:

$cd ..
$ls -F
a          c          data/      p
b          d          junk/      programs/
$rm d  p
$ls -F
a          b          c          data/      junk/      programs/
$cp data  safedata
cp: data: omitting directory
$

As we see cp cannot simply copy a directory. We have to explicitly tell it to recursively copy a directory, its files and subdirectories:

$cp -R data  safedata
$ls -F
a          b          c          data/      junk/      programs/  safedata/
$ls -F safedata
g          h          i          personal/
$ls -F safedata/personal
m  n  o
$

We have created a complete copy of the directory data and all its subdirectories. How do we now get rid of them though?



Mark O. Stitson
Wed Sep 25 10:45:32 BST 1996