Copying files from here to there

May 11, 2005, 10:29 AM —  ITworld.com — 

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.

ITworld.com

I like it!
Post a comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
Free books

Essential JavaFX
Get started building rich Web apps quickly with an introduction to the power of JavaFX key features -- scene node graphs, nodes as components, the coordinate system, layout options, colors and gradients, custom classes with inheritance, animation, binding, and event handlers.Enter now!

The Nomadic Developer
Consulting can be hugely rewarding, but it's easy to fail if you are unprepared. To succeed, you need a mentor who knows the lay of the land. Aaron Erickson is your mentor, and this is your guidebook. Enter now!

Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

Marketplace