Go to the previous, next section.

Changing Directory

(This message will disappear, once this node revised.)

The `--directory=directory' (`-C directory') option causes tar to change its current working directory to directory. Unlike most options, this one is processed at the point it occurs within the list of files to be processed. Consider the following command:

tar --create --file=foo.tar -C /etc passwd hosts -C /lib libc.a

This command will place the files `/etc/passwd', `/etc/hosts', and `/lib/libc.a' into the archive. However, the names of the archive members will be exactly what they were on the command line: `passwd', `hosts', and `libc.a'. The `--directory=directory' (`-C directory') option is frequently used to make the archive independent of the original name of the directory holding the files.

Note that `--directory=directory' (`-C directory') options are interpreted consecutively. If `--directory=directory' (`-C directory') option specifies a relative file name, it is interpreted relative to the then current directory, which might not be the same as the original current working directory of tar, due to a previous `--directory=directory' (`-C directory') option.

When using `--files-from=file-of-names' (`-T file-of-names') (

FIXME: pxref Reading Names from a File
), you can put `-C' options in the file list. Unfortunately, you cannot put `--directory' options in the file list. (This interpretation can be disabled by using the `--null' option.)

Changing the Working Directory Within a List of File Names

(This message will disappear, once this node revised.)

To change working directory in the middle of a list of file names, either on the command line or in a file specified using `--files-from=file-of-names' (`-T file-of-names'), use `--directory=directory' (`-C directory'). This will change the working directory to the directory directory after that point in the list. For example,

tar --create iggy ziggy --directory=baz melvin

will place the files `iggy' and `ziggy' from the current directory into the archive, followed by the file `melvin' from the directory `baz'. This option is especially useful when you have several widely separated files that you want to store in the same directory in the archive.

Note that the file `melvin' is recorded in the archive under the precise name `melvin', not `baz/melvin'. Thus, the archive will contain three files that all appear to have come from the same directory; if the archive is extracted with plain `tar --extract', all three files will be written in the current directory.

Contrast this with the command

tar -c iggy ziggy bar/melvin

which records the third file in the archive under the name `bar/melvin' so that, if the archive is extracted using `tar --extract', the third file will be written in a subdirectory named `bar'.

`--directory=`directory''
`-C `directory''
Changes the working directory.

FIXME: need to test how extract deals with this, and add an example

Go to the previous, next section.