How to Archive Directories using TAR

When the names of files or members specify directories, the operation of tar is more complex. Generally, when a directory is named, tar also operates on all the contents of the directory, recursively. Thus, to tar, the file name `/’ names the entire file system.

To archive the entire contents of a directory, use `–create’ (`-c’) or `–append’ (`-r’) as usual, and specify the name of the directory. For example, to archive all the contents of the current directory, use `tar –create –file=archive-name .’. Doing this will give the archive members names starting with `./’. To archive the contents of a directory named `foodir’, use `tar –create –file=archive-name foodir’. In this case, the member names will all start with `foodir/’.

tar -cf archive.tar foodir

If you give tar a command such as `tar –create –file=foo.tar .’, it will report `tar: foo.tar is the archive; not dumped’. This happens because the archive `foo.tar’ is created 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, and skips it. (It makes no sense to put an archive into itself.) GNU tar will continue in this case, and create the archive as normal, except for the exclusion of that one file. Other versions of tar, however, are not so clever, and will enter an infinite loop when this happens, so you should not depend on this behavior. In general, make sure that the archive is not inside a directory being dumped.

When extracting files, you can also name directory archive members on the command line. In this case, tar extracts all the archive members whose names begin with the name of the directory. As usual, tar is not particularly clever about interpreting member names. The command `tar –extract –file=archive-name .’ will not extract all the contents of the archive, but only those members whose member names begin with `./’.

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.

cd ..

Once in the superior directory, you can specify the subdirectory as a file name a

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

OUTPUT:
---------
practice/
practice/blues
practice/folk
practice/jazz
practice/collection

Note that the archive thus created is not in the subdirectory `practice', but rather in the 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. Trying to store your home directory in an archive by invoking tar from the root directory will probably not work.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: