How To Untar Files In Linux?

The *.tar is a popular archive format used to compress files in Linux and Unix operating systems. The tar format is used with different compression algorithms like gz, bz2, etc. The most important function of the tar format is the ability to store multiple files and directories as a single file where it can be easily compressed. In the tutorial, we examine how to untar files with different compressions algorithms like gz, bz2 etc.

Untar Command Syntax

The tar command has the following syntax which can be used to untar files and folders in different ways.

tar OPTIONS TAR_FILE PATH
  • OPTIONS is used to untar different compressions formats.
  • TAR_FILE is the tar file.
  • PATH is optional and used in the tar file is extracted differently than the current working path.

Untar tar File

A tar file can be untared or extracted with the following command.

$ tar -xvf nmap.tar

Untar tar.gz File

The tar files can be compressed with the gzip as gz format. In the following example, we extract the tar.gz file.

$ tar -xvf nmap.tar.gz

Untar tar.bz2 File

The bz2 is another popular compression format where tar can be compressed with it. The *.tar.bz2 can be untarred like below.

$ tar -xvjf nmap.tar.bz2

Untar To Specified Path or Folder

The untar operation extract files to the current working directory by default. But we can also specify another directory to untar a tar archive. The path can be specified with the -C option like below.

$ tar -xvf nmap.tar.bz2 -C /home/ismail/Downloads/

List Contents of tar File

A typical tar file contains lots of files and directories. The contents of the tar file can be listed with the following command.

$ tar -tvf nmap.tar.gz
List Contents of tar File

List Contents of tar.gz File

$ tar -ztvf nmap.tar.gz

List Contents of tar.bz2 File

$ tar -jtvf nmap.tar.gz

Untar Single File

A single file can be untared from a tar archive. The file which will be extracted is added after the tar archive file.

$ tar -xvf nmap.tar.gz nmap/capsule.c

Untar Specific Extensions

Files with specific extensions can be untar using the tar command. In the following example, we untar files with *.txt extension.

$ tar -xvf nmap.tar.gz --wildcards "*.txt"

Untar Files with Specified Pattern

$ tar -xvf nmap.tar.gz --wildcards "Cache*"

Leave a Comment