10 useful tar command examples for linux admin

About tar command : 

The Linux “tar command” stands for tape archive, which is used to deal with the tape drive backups.

The tar command used to rip a collection of files and directories into highly compressed archive file commonly called tarball or tar, gzip and bzip in Linux.

tar files easily moved from one disk to another or machine to machine.

The GNU tar command included with Linux distributions has integrated compression. It can create a .tar archive and then compress it with gzip or bzip2 compression in a single command. That’s why the resulting file is a .tar.gz file or .tar.bz2 file.


Create a tar archive file :  

Here we are creating one tar file with name “pdf.tar” with help of tar command.

  • tar  -cvf  pdf.tar   test1.pdf  test2.pdf test3.pdf
  • tar -cvf  pdf.tar  *.pdf


Let’s discuss the each option we have used in the above command for creating tar archive file.

  •   c – Creates a new .tar archive file
  •   v – Verbosely show the .tar file progress
  •   f  – File name type of the archive file.


List content of tar archive file :
In this ex. we used the option ‘ t ‘ to display the contents of file, users info,  size, permissions, date of file, time etc.
  •   tar  -tvf   pdf.tar
  •   t  –  list the contents of .tar file


Untar or Extract tar archive file :  

In below eg. we untar or extract the file by using command.

  •    tar  -xvf  pdf.tar   -C   /home/admin/Documents/
  •    x –  untar or extract the files.
  •    C (capital) – used to extract the files in another directory or folder.


Create a tar.gz file :

To create a compressed gzip archive file we use the option as z. For example the below command will create a compressed pdf.tar.gz file for the directory /home/admin/mypdfs. (Note : tar.gz and tgz both are similar).

  •   tar  -zcvf   pdf.tar.gz /home/admin/mypdfs
  •   z – used to compress tar with .gz extention
  •   c – create tar file
  •   v – display progress.
  •   f  – File name type of the archive file.


Create tar.bz2  file:

The bz2 feature compress and create archive file less than the size of the gzip. The bz2 compression takes more time to compress and decompress files as compared to gzip which takes less time. To create highly compressed tar file we use option as j. (Note: tar.bz2 and tbz is similar as tb2).

  •  tar  -jcvf  pdf.tar.bz2  /home/mypdfs/
  •  j – used to compressed tar with .bz2 extention.


Uncompressed tar.gz or  tar.bz2  file : 

We uncompressed the tar.gz file using command,

  •  tar  -xvf  pdf.tar.gz  or   tar  -xzvf   pdf.tar.gz  -C        /home/admin/Documents/
         Note :- ‘ x ‘ can extract both time of file tar.gz  &  tar.bz2. no need to specify flag for different extensions.

List content of tar.gz and tar.bz2  file :

To list contents of tar.gz & tar.bz2 we using same flag for both i.e. ‘ t
  •   tar  -tvf  pdf.tar.gz   or   tar  -tvf  pdf.tar.bz2


Untar single file from tar file :

We are now untar the single file from tar file, here we need to just specify the file name which you want to extract next to the  tar.gz file or tar.bz2 file.
  •  tar  -xvf  pdf.tar.gz  intro-linux.pdf


  •  tar   -xvf  pdf.tar.bz2  bash-linux.pdf


Extract  Group of files using wildcards :

Sometime we need bunch of files only from compressed file so we used here –wildcards flag and then gives the astric with file extension.

  •  tar  -xvf   pdf.tar.bz2  –wildcards  “*.pdf”


Add files or directories to  tar.gz & tar.bz2 files :

The tar command don’t have a option to add files or directories to a existing compressed tar.gz and tar.bz2 archive file. If we do try will get the following error.
  •   tar  -rvf   pdf.tar.gz  linus.png


Add files or directories to tar files :

To add files or directories to existing tar archived file we use the option r (append).
  •  tar  -rvf  pdf.tar  linus.png


tar usage and  flags :

  • c – create a archive file.
  • x – extract a archive file.
  • v – show the progress of archive file.
  • f – filename of archive file.
  • t – viewing content of archive file.
  • j – filter archive through bzip2.
  • z – filter archive through gzip.
  • r – append or update files or directories to existing archive file.
  • W – Verify a archive file.
  • wildcards – Specify patters in unix tar command.


This is all about the tar command which we are using in live scenario so i hope you guys like the information, please feel free to revert if you have any specific questions or doubts i will definitely reply for all your queries and suggestions.

Latest Comments
  1. Sushant thorat July 9, 2016
    • Prashant July 9, 2016
  2. Harry December 13, 2016
  3. SRINIVAS DARIPELLI February 10, 2017

Leave a Reply

Your email address will not be published. Required fields are marked *