LinuxDevCenter.com

oreilly.comSafari Books Online.Conferences.

We've expanded our Linux news coverage and improved our search! Search for all things Linux across O'Reilly!

Search
Search Tips

advertisement

Listen Print Subscribe to Linux Subscribe to Newsletters

Unix Power Tools
Copying Directory Trees with (tar | tar)

by Jerry Peek
02/03/2000

The tar command isn't just for tape archives. It can copy files from disk to disk, too. And even if your computer has cp -r there are advantages to using tar.

The obvious way to copy directories with tar is to write them onto a tape archive with relative pathnames -- then read back the tape and write it somewhere else on the disk. But tar can also write to a UNIX pipe -- and read from a pipe. This looks like:

% reading-tar | writing-tar

with one trick: the writing-tar process has a different current directory (the place where you want the copy made) than the reading-tar. To do that, run the writing-tar in a subshell.

The argument(s) to the reading-tar can be directory(s) or file(s). Just be sure to use relative pathnames that don't start with a slash -- otherwise, the writing-tar will write the copies in the same place the originals came from!

"How about an example," you ask? The figure below has one. It copies from the directory /home/jane, with all its files and subdirectories. The copy is made in the directory /work/bkup/jane:


% mkdir /work/bkup/jane
% cd /home/jane
% tar cf - . | (cd /work/bkup/jane && tar xBf -)

The && operator tells the shell to start tar xBf only if the previous command (the cd) succeeded. That prevents tar writing files into the same directory it's reading from -- if the destination directory isn't accessible or you flub its pathname. If your tar has a B (reblocking) option, use it to help be sure that the copy is made correctly. If your tar doesn't have a reblocking option, you can use this trick suggested by Chris Torek:


% tar cf - . | cat | (cd /work/backup/jane && tar xbf 1 -)

You can use other options that your tar might have, like excluding files or directories, on the reading-tar, too. Some gotchas:

  • Symbolic links will be copied exactly. If they point to relative pathnames, the copied links might point to locations that don't exist. You can search for these symbolic links with find - type l.

  • A hard link will be copied as a file. If there are more hard links to that file in the files you're copying, they will be linked to the copy of the first link. That can be good because the destination might be on a different filesystem (a hard link to the original file can't work then). It can be bad if the link pointed to a really big file; the copy can take a lot of disk space. You can search for these hard links by:

    • Searching the directory from which you're copying with find - links +1 - type f to find all files that have more than one link, and

    • Running the reading-tar with its l (lowercase letter L) option to complain if it didn't copy all links to a file.

Figure: Copying /home/jane to /work/bkup with tar

Figure of copying directories with tar

If your system has rsh, you can run the writing-tar on a remote system. For example, to copy a directory to the computer named kumquat:


% rsh kumquat mkdir /work/bkup/jane
% tar cf - . | rsh kumquat 'cd /work/bkup/jane && tar xBf -'


Back More Unix Power Tools

 




Tagged Articles

Be the first to post this article to del.icio.us

Recommended for You

  1. Cover of Samba Pocket Reference
    Samba Pocket Reference
    Print: $9.95
  2. Cover of Essential CVS
    Essential CVS
    Print: $44.99
    Ebook: $35.99
  3. Cover of Linux System Administration
    Linux System Administration
    Print: $44.99
    Ebook: $35.99
  4. Cover of Debian System
    Debian System
    Print: $44.95

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com