Linux tar is a very popular tool used to compress and extract files and folders. Tar is used for a long time and its primary usage intention was using for tape archiving. Tar supports primarily tar format or extension which is used to create one file for all directories and files. But through the history tar have integrated compression protocol support like gz, bzip. Tar is generally used with these popular compression protocols to compress files and folders. Typically .tar, .tar.gz and .tar.bz extensions are used to name compressed files. In this tutorial, we will look in detail on how to use tar command for different situations and operations.

Create Tar Archive

As we stated before tar archive will create a single file for all directories and files. This is very useful for compression, transfer, backup, etc. Tar is generally used for transfer or backup operations too. In this example, we will create our desktop directory as a tar file.

$ tar -cvf tmux.tar tmux-2.2

Create Tar Archive

Here we have created a tmux.tar which provides all files and folders in tmux-2.2

List Extracted or Compressed Files and Folders Verbosely

To get details of operations verbose option is should be specified. In the previous example, we are already using the verbose option with -v .

$ tar -cvf tmux.tar tmux-2.2

Create Tar Archive With Gzip Compression

Gzip is a popular and good compression algorithm used for a lot of different purposes. For example browsers and web servers uses a gzip compression algorithm too. Tar and gzip can be used together with just a single command like below.

$ tar cvfz tmux.tar.gz tmux-2.2

Create Tar Archive With Gzip Compression

Create Tar Archive With Bzip2 Compression

Bzip2 is an alternative to the gzip compression algorithm. Bzip2 popular especially in Linux, Unix, and BSD platforms. It can be used the same as gzip.

$ tar cvfj tmux.tar.bz2 tmux-2.2

Create Tar Archive With Bzip2 Compression

Decompress and Untar Tar Archive

Up to now, we have compressed our files and directories. What about decompressing them? We will decompress all contents or the archives to the current working directory like below. The main parameter used to extract a tar archive is x .

$ tar xvf tmux.tar

Decompress or Untar Tar Archive

Decompress or Untar tar.gz Archive

Gzip compressed tar archives are similar while decompressing. We can add gzip parameter to the normal extract tar command like below.

$ tar xvfz tmux.tar.gz

Decompress or Untar tar.gz Archive

Decompress or Untar tar.bz2 Archive

Another popular compression algorithm used with tar archive is Bzip2. Bzip2 compressed tar archives uses *.tar.bz2 file extension by default. In Order to decompress or extract or untar a bzip2 archive file, we will use the xvfj options by providing the bzipped archive file name or path like below.

$ tar xvfj tmux.tar.bz2

Decompress or Untar tar.bz2 Archive

List or Display Contents Of Tar Archive

A tar archive generally holds a lot of files and folders and listing files of a tar archive is easy as compressing files and directories. We will provide -tvf option like below to list tmux.tar file contents.

$ tar -tvf tmux.tar

List Content Of Tar Archive

List Content Of Tar.Gz Archive

tar.gz contents can be listed below. It is the same as tar because. Tar detects the compression methods automatically. So we do not need to specify the compression algorithm specifically.

$ tar -tvf tmux.tar.gz

List Content Of Tar.Gz Archive

List Content Of Tar.Bz2 Archive

The same as tar and gz formats. We do not specify compression algorithm specifically. It is detected automatically.

$ tar -tvf tmux.tar.bz2

List Content Of Tar.Bz2 Archive

Extract Single File From Tar Archive

One of the most wanted ways to extract a file from an archive is to extract only a single file. We will extract a single file by given its name to the tar like below. We will use -xvf option and provide the tar file and the file we want to extract. In this example, we will extract tmux-2.2/hooks.c file from tar file named tmux.tar

$ tar -xvf tmux.tar tmux-2.2/hooks.c

Extract Single File From Tar Archive

Extract Single File From Tar.Gz Archive

Extracting a single file from a tar.gz archive is very similar to the previous example. We will add Gzip parameter which is z . We will use -zxvf option like below.

$ tar -zxvf tmux.tar.gz tmux-2.2/hooks.c

Extract Single File From Tar.Gz Archive

Extract Single File From Tar.Bz2 Archive

Extracting single file for Bzip2 archives is similar to tar and gzip. We will use -jxvf option like below.

$ tar -jxvf tmux.tar.bz2 tmux-2.2/hooks.c

Extract Single File From Tar.Bz2 Archive

Extract Multiple File From Tar Archive

It can be also extracted from multiple files from a tar archive. The files that will be extracted will be listed by splitting with spaces.

$ tar -xvf tmux.tar "tmux-2.2/hooks.c" "tmux-2.2/cfg.c"

Extract Multiple File From Tar Archive

Extract Multiple File From Tar.Gz Archive

We can extract multiple files from a gzip archive like a tar archive. We will just put file names we want to extract at the end of the command. In this example, we will extract the file hooks.c and cfg.c

$ tar -zxvf tmux.tar.gz "tmux-2.2/hooks.c" "tmux-2.2/cfg.c"

Extract Multiple File From Tar.Gz Archive

Extract Multiple File From Tar.Bz2 Archive

Multiple files of a bzip2 archive can be extracted similar to the tar and gzip. We will just provide bzip2 parameter with j .

$ tar -jxvf tmux.tar.bz2 "tmux-2.2/hooks.c" "tmux-2.2/cfg.c"

Extract Specified Files Types or Extensions with Wildcard

Wildcards are used to specify multiple files with a similar name. We just set fixed parts of names and then put wildcard for the variable part like below. The wildcard is expressed with * .

$ tar -zxvf tmux.tar.gz --wildcards '*.txt'

Add Files and Directories To Tar Archive

After creating a tar archive we may need to add extra files to the existing tar archive. For this operation, we will use r for append or add operation.

$ tar -rvf tmux.tar test1/

Add Files and Directories To Tar Archive

In this example, we have added a directory named test1 . Adding a file is the same.

Get Size Of Tar Archive

The size of the content can get with empirical ways like counting words. Below the result is in KB. The given size is the total size after uncompressing the whole content.

$ tar -czf - tmux.tar | wc -c

Get Size Of Tar Archive

Get Size Of Tar.Gz Archive

The size of the tar.gz archive can be listed below. But keep in mind that this is not an exact size. It is an approximation.

$ tar -czf - tmux.tar.gz | wc -c

Get Size Of Tar.Gz Archive

Get Size Of Tar.Bz2 Archive

The size of the tar.bz archive can be listed below. But keep in mind that this is not the exact size. It is an approximation.

$ tar -czf - tmux.tar.bz2 | wc -c