From: www.itworld.com

Copying files from here to there

by Sandra Henry-Stocker

May 11, 2005 —

 

There's more than one way to copy a file from one place to another whether the destination is on the same system or across the expanse of the Internet. Whether you're replicating a directory structure or backing up a single file for safe keeping, your choices of how to move the bits around include quite a few options.




Using cp



The cp command is the obvious choice for copying a file from one directory to another or making a backup copy of a file in the same directory. The cp command's options, however, also include two choices for recursion: -r and -R. Using one of these options with cp, you can copy a directory from one place in your file system to another with no more trouble than copying a single file.



For example, the command shown below copies the entirety of the /usr/local/bin directory to /backup/bin.


boson# cp -r /usr/local/bin /backup/bin


However, this command can be just a tad tricky. If the /backup/bin directory already exists, for example, your files will be copied to /backup/bin/bin instead of /backup/bin. A more reliable command to use -- provided you don't need the new directory to have a different basename (in this case "bin"), would be to copy the directory with this command:


boson# cp -r /usr/local/bin /backup


On the other hand, this command is going to misbehave if you don't happen to have a /backup directory. In this case, your files would end up in /backup and not in /backup/bin.



If you wanted to handle this kind of copying using a script, you might want to verify the starting conditions before you execute your copy command. For example, you might do something like this:

#!/bin/bash

if [ ! -d /backup ]; then
    ans=`ckyorn -p "create /backup?"`
    case $ans in
      [Yy]*) mkdir /backup;;
      [Nn]*) echo Exiting -- no /backup directory found
             exit;;
    esac
    mkdir /backup
fi

if [ -d /backup/bin ]; then
    cp -r bin /backup/bin
else
    cp -r bin /backup
fi


That's a lot of extra code just to make sure the copy is going to behave as expected, but the script can check faster than you can. You might as well let it clear the path for your copying to go smoothly.



Using tar



Another way to copy an entire directory is to employ what is referred to as a "tar to tar" operation. In other words, you run a tar command (one that uses the c option to create a tar archive) and pipe it to another tar command (one that uses the x option to extract the files. A tar to tar command might look like this:

boson# cd /usr/local/bin
boson# tar cpBf - * | (cd /backup/bin; tar xBf -)


If you put these commands in a script, you will still need to first verify that /backup/bin exists. On the other hand, you won't end up with your files in /backupo/bin/bin no matter what you do.



A benefit of tar to tar operations such as this is that is they preserve the file attributes and copy sumbolic links as links instead of copying the file the link points to -- a factor that can make a significant difference in the size of the resulting directory.



Another advantage of tar to tar copies is that they can be run over your network. If you want to copy /usr/local/bin on one system to /backup/bin on another system, you can use a command such as this:

boson# cd /usr/local
boson# tar cpBf - bin | ssh coyote "cd /backup; tar xf -"


Using tar and dd



A third option for copying files from one system to another involves tar and the dd command. This operation looks similar to the tar to tar command shown above but, instead of replicating the directory on the second box, we're going to create a tar file. This would allow us, for example, to create multiple copies of the directory we wish to back up by giving each archive a different name.

boson# cd /usr/local
boson# dt=`date +%y%m%d`
boson# tar cpBf - bin | ssh coyote "cd /backup; dd of=bin_${dt}.tar"


In this case, the archives will have names such as bin_050512.tar.

Copying single files or entire directories from one system to another is easy and trouble-free if you apply a few precautions and use the appropriate commands.