Tar Command Tutorial With Examples

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/tar-command-tutorial-examples/
PINTEREST
PINTEREST
INSTAGRAM

Linux tar is very popular tools 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 folder. Typically .tar, .tar.gz and .tar.bz extensions are used to name compressed files. In this tutorial we will look in details 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 tar file.

Create Tar Archive

Create Tar Archive

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

Verbosity or Debug

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

Create Tar Archive With Gzip Compression

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

Create Tar Archive With Gzip Compression

Create Tar Archive With Gzip Compression

Create Tar Archive With Bzip2 Compression

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

Create Tar Archive With Bzip2 Compression

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. Main parameter used to extract a tar archive is 

Decompress or Untar Tar Archive

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.

Decompress or Untar tar.gz Archive

Decompress or Untar tar.gz Archive

Decompress or Untar tar.bz2 Archive

Same as gzip and just add parameter to specify bzip extracting.

Decompress or Untar tar.bz2 Archive

Decompress or Untar tar.bz2 Archive

List Content 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.

List Content Of Tar Archive

List Content Of Tar Archive

List Content Of Tar.Gz Archive

tar.gz contents can be listed like 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.

List Content Of Tar.Gz Archive

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.

List Content Of Tar.Bz2 Archive

List Content Of Tar.Bz2 Archive

Extract Single File From Tar Archive

One of the most wanted way to extract file from archive is extract only single file. We will extract 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

Extract Single File From Tar Archive

Extract Single File From Tar Archive

Extract Single File From Tar.Gz Archive

Extracting 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.

Extract Single File From Tar.Gz Archive

Extract Single File From Tar.Gz Archive

Extract Single File From Tar.Bz2 Archive

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

Extract Single File From Tar.Bz2 Archive

Extract Single File From Tar.Bz2 Archive

Extract Multiple File From Tar Archive

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

Extract Multiple File From Tar Archive

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 file hooks.c and cfg.c

Extract Multiple File From Tar.Gz Archive

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

Wildcards

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

Add Files and Directories To Tar Archive

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

Add Files and Directories To Tar Archive

Add Files and Directories To Tar Archive

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

Add Files and Directories To Tar.Gz Archive

Tar command currently do not have add capability for Gzip files.

Add Files and Directories To Tar.Bz2 Archive

Tar command currently do not have add capability for Bzip2 files.

Verify Tar Archive

Tar archives can be corrupted during transmission or there may be some media disk related issues. This situation can be checked by verifying tar archive files.

Get Size Of Tar Archive

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

Get Size Of Tar Archive

Get Size Of Tar Archive

Get Size Of Tar.Gz Archive

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

Get Size Of Tar.Gz Archive

Get Size Of Tar.Gz Archive

Get Size Of Tar.Bz2 Archive

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

Get Size Of Tar.Bz2 Archive

Get Size Of Tar.Bz2 Archive

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/tar-command-tutorial-examples/
PINTEREST
PINTEREST
INSTAGRAM

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)