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


Unix Power ToolsUnix Power ToolsSearch this book

38.11. Avoid Absolute Paths with tar

One problem with most versions of tar: they 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 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 (Section 31.2) when you create a tape, you're stuck. If you use relative paths (Section 31.2) (for example, tar c .), you can restore the files in any directory you want.

This means that you should:

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

% cd /
% tar c home/mike

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 an archive of . (the current directory). 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. [Note that it uses the subdirectory names, as we did in the second-previous example. When the files are extracted, they'll be restored to separate subdirectories, instead of all being mixed into the same . (current) directory. -- JP]

-- ML



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.