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
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:
Verbose Output: To get detailed information such as file permissions, sizes, and modification times, use the -v option:
tar -tvf example.tar
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.