home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 20.9 When a Program Doesn't Understand Wildcards Chapter 20
Backing Up Files
Next: 20.11 Getting tar's Arguments in the Right Order
 

20.10 Avoid Absolute Paths with tar

One problem with most versions of tar : it can't change a file's pathname when restoring. Let's say that you put your home directory in an archive (tape or otherwise) with a command like this:

% 

tar c /home/mike

What will these files be named when you restore them, either on your own system or on some other system? They will have exactly the same pathnames that they had originally. So if /home/mike already exists, it will be destroyed. There's no way to tell tar that it should be careful about overwriting files; there's no way to tell tar to put the files in some other directory when it takes them off the tape, etc. If you use absolute pathnames ( 14.2 ) when you create a tape, you're stuck. If you use relative paths ( 14.2 ) (for example, tar c . ), you can restore the files in any directory you want. [GNU tar (on the CD-ROM) converts absolute pathnames to relative, by default. Most other tar s don't do that, though, so I don't advise relying on the feature. -JP ]

This means that you should:

  • Avoid using absolute paths when you create an archive (see below).

  • Use tar t to see what files are on the tape before restoring the archive.

  • Use GNU tar (on the CD-ROM). It can ignore the leading / as it extracts files.

Rather than giving a command like tar c /home/mike , do something like:

% 

cd /home/mike


% 

tar c .

Or, even more elegant, use -C on the tar command line:

% 

tar c -C /home/mike .

This command tells tar to cd to the directory /home/mike before creating the archive. If you want to archive several directories, you can use several -C options:

% 

tar c -C /home/mike ./docs  -C /home/susan ./test

This command archives mike 's docs directory and susan 's test directory.

- ML


Previous: 20.9 When a Program Doesn't Understand Wildcards UNIX Power Tools Next: 20.11 Getting tar's Arguments in the Right Order
20.9 When a Program Doesn't Understand Wildcards Book Index 20.11 Getting tar's Arguments in the Right Order

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System