Frequently, you will find yourself wanting to determine exactly what a
particular archive contains. You can use the --list (-t) operation
to get the member names as they currently appear in the archive, as well
as various attributes of the files at the time they were archived. For
example, you can examine the archive collection.tar that you
created in the last section with the command,
$ tar --list --file=collection.tar
The output of tar would then be:
blues
folk
jazz
The archive bfiles.tar would list as follows:
./birds
baboon
./box
Be sure to use a --file=archive-name (-f archive-name) option just as with --create (-c)
to specify the name of the archive.
If you use the --verbose (-v) option with --list, then
tar will print out a listing reminiscent of ‘ls -l’,
showing owner, file size, and so forth.
If you had used --verbose (-v) mode, the example above would look
like:
$ tar --list --verbose --file=collection.tar folk
-rw-r--r-- myself user 62 1990-05-23 10:55 folk
It is important to notice that the output of tar --list
--verbose does not necessarily match that produced by tar
--create --verbose while creating the archive. It is because
GNU tar, unless told explicitly not to do so, removes some directory
prefixes from file names before storing them in the archive
(See absolute, for more information). In other
words, in verbose mode GNU tar shows file names when creating
an archive and member names when listing it. Consider this
example:
$ tar cfv archive /etc/mail
tar: Removing leading `/' from member names
/etc/mail/
/etc/mail/sendmail.cf
/etc/mail/aliases
$ tar tf archive
etc/mail/
etc/mail/sendmail.cf
etc/mail/aliases
This default behavior can sometimes be inconvenient. You can force
GNU tar show member names when creating archive by supplying
--show-stored-names option.
--show-stored-names
Print member (as opposed to file) names when creating the archive.
You can specify one or more individual member names as arguments when
using ‘list’. In this case, tar will only list the
names of members you identify. For example, tar --list --file=afiles.tar apple would only print apple.
Because
tar preserves paths, file names must be specified as they appear
in the archive (ie., relative to the directory from which the archive
was created). Therefore, it is essential when specifying member names
to tar that you give the exact member names. For example,
tar --list --file=bfiles birds would produce an error message
something like ‘tar: birds: Not found in archive’, because there is
no member named birds, only one named ./birds. While the
names birds and ./birds name the same file, member
names are compared using a simplistic name comparison, in which an exact
match is necessary. See absolute.
However, tar --list --file=collection.tar folk would respond
with folk, because folk is in the archive file
collection.tar. If you are not sure of the exact file name, try
listing all the files in the archive and searching for the one you
expect to find; remember that if you use --list with no file
names as arguments, tar will print the names of all the members
stored in the specified archive.