Managing Disk Space   «Prev  Next»
Lesson 7Listing an archive
Objective Use tar command to list the file names in an archive.

list file names with tar

In the previous lesson, you learned that you can list file names as they are put into an archive. But after the archive is created, you may want to list the file names again. For this situation, use tar with the t option, like this:
% tar tf archive . The t option, which must go first, produces a “table of contents” for the specified archive. The archive can be either the name of an existing tar file or the name of a device in which a tape is loaded. As before, the f option is used when supplying an archive name. You can also add the v option. When used with t, the v option produces output in a format like ls –l.
The following diagram shows the tar command with the t option.

Using tar tf to view an archive

t and f options are supplied as a group
  1. The t and f options are supplied as a group. t means “table of contents,” and f is needed when you supply an archive name.
  2. tar reads file names from an existing archive named backup.tar.
  3. The output of tar shows which files are bundled in the archive. In this case, backup.tar contains the project directory tree.

How do I use the tar command to list the file names in an archive?

You can use the tar command to list the file names in an archive by using the -t (or --list) option. Here is the basic syntax to list the files in a tar archive:
tar -tf archive.tar

This will display a list of the files in the archive, along with any directories or subdirectories that they are contained in. If the archive is compressed, such as with gzip or bzip2, the command will automatically decompress the archive and list its contents.
Here is a breakdown of the command options:
  1. tar: the command to run the tar utility
  2. -t or --list: the option to list the contents of the archive
  3. -f: specifies that the following argument is the name of the archive file

Replace "archive.tar" with the name of the actual archive file that you want to list the contents of.
You can also use other options to modify the output format, such as -v to show verbose output, or --strip-components to remove leading directory components from the file names.

Archive Listing - Exercise

The following exercise tests your understanding of lists in Unix.
Archive Listing - Exercise
In the next lesson, you will learn to extract files from an archive.