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
tar tf backup.tar

Command Explanation:
  • tar: Utility used to create, extract, or list the contents of tar archives.
  • tf: Flags used with tar:
    • t: List the contents of the tar archive.
    • f: Specify the name of the tar archive file.
  • backup.tar: Name of the tar archive being listed.

Shell Script Output in the Image: The script lists the contents of the tar archive `backup.tar`. The directory structure and files included are:
project/
project/docs/
project/docs/index.html
project/docs/doc_plan.html
project/mktg/
project/mktg/index.html
project/mktg/requirements.html
project/qa/
project/qa/test_plan.html
project/qa/index.html
project/sw/
project/sw/index.html
project/sw/product_spec.html
project/index.html

This output shows that the archive contains directories (`project/docs/`, `project/mktg/`, etc.) and various `.html` files stored within those directories.
  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.

Use the tar command to list the file names in an archive

To list the file names in an archive using the `tar` command in Unix, you use the `-t` (list) option. Here's the syntax:
tar -tf archive_name.tar

Explanation:
  • -t: List the contents of the archive.
  • -f archive_name.tar: Specifies the archive file.

Example: Suppose you have an archive file called `example.tar`. To list the file names inside it:
tar -tf example.tar

Additional Options:
  1. Verbose Output: To get detailed information such as file permissions, sizes, and modification times, use the -v option:
    tar -tvf example.tar
        
  2. Compressed Archives: If the archive is compressed, include the appropriate decompression flag:
    • For .tar.gz or .tgz:
      tar -tzf archive_name.tar.gz
              
    • For .tar.bz2:
      tar -tjf archive_name.tar.bz2
              
    • For .tar.xz:
      tar -tJf archive_name.tar.xz
              

Note: Ensure you have the necessary permissions to access the archive file and the correct decompression utilities installed for compressed archives.

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.

SEMrush Software 7 SEMrush Banner 7