How to tar, untar Files, and View the Contents of tar Files in Linux?

The most frequently used and well-known Linux distributions offer built-in “tar (tape archive)” command line utility. Its main purpose is to create, extract and view the tar files. The tar files take less storage and are generally beneficial for distribution and backup purposes. 

When created, they can be easily extracted into the desired directory, and their content can be viewed. 

This guide enlists the complete procedure to tar, untar and view the content of files under Linux with the following outcomes:

How to Tar Files Under Linux?

The “tar” utility supports a large list of compression formats such as “.gzip”, “.xz”, “.gz”, “bzip2”, and many others. The basic syntax to create a tar archive in Linux is defined below:

$ tar cf [file_name.tar]

The syntax contains the following components:

  • tar: Displays the “tar” command line utility.
  • c: Creates the tar file.
  • f: Specifies the archive file with its name 
  • file_name.tar: Shows the newly created tar archive.

For practical implementation, let’s see the following examples.

Example 1: Create a “.tar” File

Execute the “tar” command followed by the combination of its “c(create)”, and “f(file)” options to create a “SampleArchive” tar file of the “Sample” directory:

$ tar cf SampleArchive.tar Sample

The “SampleArchive” has been created in the present working directory.

Example 2: Create a “tar.gz” File

To create another compression tar archive having a “.gz” extension, use the “tar” command with the “czvf” flag. The “z” is used to create gzip archive:

$ tar czf ArchiveTest.tar.gz Test

The compressed “TestArchive.tar.gz” has been created.

Example 3: Create a “tar.xz/.bz2/.tbz/.bz” File

Use the “-j” flag with the “tar” command and the combined “cvf” flags is helpful to create the “tar.xz” archive file in this way:

$ tar cjf Extra1.tar.xz Extra

The “Extra1.tar.xz” compressed archive file of the “Extra” directory has been created.

How to View Content of Tar Files?

Once the tar files are created, their content can be easily viewed using the below-mentioned basic tar command syntax:

$ tar -tf [file_name.tar]
            OR
$ tar --list --file=[file_name.tar]

The command contains the following flags:

  • t or “-list”: Displays the tar file contents 
  • f or “–file”: Specifies the file

Example 1: View “.tar” File

Simple use the above “tar” command syntax with the desired tar file name to view its content:

$ tar -tf SampleArchive.tar

The output shows the “SampleArchive.tar” file content in the terminal.

Example 2: View “tar.gz”  File

For checking the compressed “tar.gz” file format content, execute the “tar” command with the “z(specifies .gz format)” flag in this way:

$ tar -tzf ArchiveTest.tar.gz

The terminal displays the “ArchiveTest.tar.gz” file content.

Example 3: View “tar.xz/.bz2/.tbz” File

Run the “tar” command with the “j(denotes .xz,.bz2,.tbz format)” flag to view the content of the compressed “Extra1tar.xz” file content:

$ tar -tjf Extra1.tar.xz

How to Untar Files in Linux?

The “tar” command uses the following basic syntax to extract the tar files in the current or the specified directory:

$ tar xf [file_name.tar]

The “x” flag in the above syntax is used to extract the tar file.

Example 1: Untar the “.tar” File

To untar the “SampleArchive.tar” compressed tar file into the present working directory, use the “tar” command in this way:

$ tar xf SampleArchive.tar

The “ls(list)” command verifies that the “SampleArchive.tar” of the “Sample” directory has been untared.

Example 2: Untar the “tar.gz” File

Simple use the “z” and “C(specifies path)” flags to extract the “ArchiveTest.gz” file into the “Documents” directory by utilizing the following command:

$ tar -xzf ArchiveTest.tar.gz -C /home/itslinuxfoss/Documents/

The “ArchiveTest.gz” of “Test” has been successfully extracted into the “Documents” directory.

Example 3: Untar the “tar.xz/.bz2/.tbz/.bz” File

Execute the “tar” command with the “j” flag for extracting the “Extra1.tar.xz” file from “home” into the “Downloads” directory:

$ tar -xjf Extra1.tar.xz -C /home/itslinuxfoss/Downloads/

The output verifies that “Extra1.tar.gz” has been opened into the “Downloads” directory.

Note: Click Here to read more detail on untar a file. 

Conclusion

Linux and most of its distributions come with a pre-installed “tar” command to create tar, untar and view the tar file content. This command offers a wide range of options that perform the task based on their names. This guide has illustrated a brief description of tar, untar and view tar file s content under Linux.