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 -)

Sign up for ITworld's Daily newsletter
Follow ITworld on Twitter @IT_world

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.
peer-to-peer

Esther Schindler
If the comments are ugly, the code is ugly

claird
SVG a graphics format for 21st century

pasmith
Take Chrome OS for a test spin

Sandra Henry-Stocker
Solaris Tip: Have Your Files Changed Since Installation?

sjvn
64-bits of protection?

jfruh
Android fragments vs. the iPhone monolith

mikelgan
What Gizmodo missed about the Pro WX Wireless USB disk drive

 

Sidekick: The Good News & the Bad News
Either way you look at it Microsoft Data Center management did not follow standards or best practices in this failure. In which case it makes me wonder more about the outsourcing of corporate data much less personal data.
- mburton325

Join the conversation here

The Daily Tip

The Daily TipQuick, practical advice for IT pros. Made fresh daily.

Hot tips:

Want to cash in on your IT savvy? Send your tip to tips@itworld.com. If we post it, we'll send you a $25 Amazon e-gift card.

Newsletters

Subscribe to ITWORLD TODAY and receive the latest IT news and analysis.

I would like to receive offers via email from ITworld partners.
By clicking submit you agree to the terms and conditions outlined in ITworld's privacy policy.
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