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.
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:
- tar: the command to run the tar utility
- -t or --list: the option to list the contents of the archive
- -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.