Copying files from here to there
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
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?
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
Quick, practical advice for IT pros. Made fresh daily.
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.













