Previous: short create, Up: create [Contents][Index]

#### 2.6.5 Archiving Directories

You can archive a directory by specifying its directory name as a file name argument to `tar`. The files in the directory will be archived relative to the working directory, and the directory will be re-created along with its contents when the archive is extracted.

To archive a directory, first move to its superior directory. If you have followed the previous instructions in this tutorial, you should type:

    $ cd ..
    $

This will put you into the directory which contains `practice`, i.e., your home directory. Once in the superior directory, you can specify the subdirectory, `practice`, as a file name argument. To store `practice` in the new archive file `music.tar`, type:

    $ tar --create --verbose --file=music.tar practice

`tar` should output:

    practice/
    practice/blues
    practice/folk
    practice/jazz
    practice/collection.tar

Note that the archive thus created is not in the subdirectory `practice`, but rather in the current working directory—the directory from which `tar` was invoked. Before trying to archive a directory from its superior directory, you should make sure you have write access to the superior directory itself, not only the directory you are trying archive with `tar`. For example, you will probably not be able to store your home directory in an archive by invoking `tar` from the root directory; See absolute. (Note also that `collection.tar`, the original archive file, has itself been archived. `tar` will accept any file as a file to be archived, regardless of its content. When `music.tar` is extracted, the archive file `collection.tar` will be re-written into the file system).

If you give `tar` a command such as

    $ tar --create --file=foo.tar .

`tar` will report ‘`tar: ./foo.tar is the archive; not dumped`’. This happens because `tar` creates the archive `foo.tar` in the current directory before putting any files into it. Then, when `tar` attempts to add all the files in the directory `.` to the archive, it notices that the file `./foo.tar` is the same as the archive `foo.tar`, and skips it. (It makes no sense to put an archive into itself.) GNU `tar` will continue in this case, and create the archive normally, except for the exclusion of that one file. (Please note: Other implementations of `tar` may not be so clever; they will enter an infinite loop when this happens, so you should not depend on this behavior unless you are certain you are running GNU `tar`. In general, it is wise to always place the archive outside of the directory being dumped.)

Previous: short create, Up: create [Contents][Index]