Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Next: , Up: one

6.8.1 Changing the Working Directory

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.

-C directory
Changes the working directory in the middle of a command line.

For example,

     $ tar -c -f jams.tar grape prune -C food cherry

will place the files grape and prune from the current 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 current 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 orange-colored.

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 current directory, which might not be the same as the original current working directory of tar, due to a previous --directory option.

When using --files-from (see files), you can put various tar options (including -C) in the file list. Notice, however, that in this case the option and its argument may not be separated by whitespace. If you use short option, its argument must either follow the option letter immediately, without any intervening whitespace, or occupy the next line. Otherwise, if you use long option, separate its argument by an equal sign.

For instance, the file list for the above example will be:


To use it, you would invoke tar as follows:

     $ tar -c -f foo.tar --files-from list

Notice also that you can only use the short option variant in the file list, i.e., always use -C, not --directory.

The interpretation of --directory is disabled by --null option.

  Published under the terms of the GNU General Public License Design by Interspire