Next: Absolute File Names, Up: Crossing File System Boundaries [Contents][Index]
To change the working directory in the middle of a list of file names, either on the command line or in a file specified using --files-from (-T), use --directory (-C). This will change the working directory to the specified directory after that point in the list.
Changes the working directory in the middle of a command line.
All filename arguments appearing after this option will be taken relative to directory. For example,
$ tar -c -f jams.tar grape prune -C food cherry
will place the files grape and prune from the working directory into the archive jams.tar, followed by the file cherry from the directory food. This option is especially useful when you have several widely separated files that you want to store in the same archive.
Note that the file cherry is recorded in the archive under the precise name cherry, not food/cherry. 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 working directory.
Contrast this with the command,
$ tar -c -f jams.tar grape prune -C food red/cherry
which records the third file in the archive under the name red/cherry so that, if the archive is extracted using ‘tar --extract’, the third file will be written in a subdirectory named red.
You can use the --directory option to make the archive independent of the original name of the directory holding the files. The following command places the files /etc/passwd, /etc/hosts, and /lib/libc.a into the archive foo.tar:
$ tar -c -f foo.tar -C /etc passwd hosts -C /lib libc.a
However, the names of the archive members will be exactly what they were on the command line: passwd, hosts, and libc.a. They will not appear to be related by file name to the original directories where those files were located.
Note that --directory options are interpreted consecutively. If
--directory specifies a relative file name, it is interpreted
relative to the then working directory, which might not be the same as
the original working directory of tar, due to a previous
--directory option.
There is an important exception, though: the -C option does not affect filename arguments to another options that follow it, in particular -f. This means that the example above can be written as
$ tar -c -C /etc -f foo.tar passwd hosts -C /lib libc.a
and it will not change its overall effect: the files
passwd and hosts will be read from the directory
/etc, but the archive foo.tar will be created in the
directory which was the working directory when the tar
command was started.
The command-line options not affected by -C are:
When using --files-from (see Reading Names from a File), you can put certain
tar options (including -C) in the file list as well.
For instance, the file list for the above example will be:
-C /etc passwd hosts --directory=/lib libc.a
To use it, you would invoke tar as follows:
$ tar -c -f foo.tar --files-from list
For a full list of options that can be used in file lists, Position-Sensitive Options.
The interpretation of options in file lists is disabled by --verbatim-files-from and --null options.
Next: Absolute File Names, Up: Crossing File System Boundaries [Contents][Index]