[Chapter 20] 20.10 Avoid Absolute Paths with tar
Author: Jerry Peek, Tim O'Reilly & Mike Loukides
Date: 1998-10-23T15:45:02Z

#

| Chapter 20 Backing Up Files |

## 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 tars 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

20.9 When a Program Doesn't Understand Wildcards |  | 20.11 Getting tar's Arguments in the Right Order