Did You Know?
Donate to TecMint

LFCS - Linux Foundation Certified SysAdmin - Exam Preparation Guide

18 Tar Command Examples in Linux

Download Your Free eBooks NOW - 10 Free Linux eBooks for Administrators

The Linux “tar” stands for tape archive, which is used by large number of Linux/Unix system administrators to deal with tape drives backup. 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. The tar is most widely used command to create compressed archive files and that can be moved easily from one disk to anther disk or machine to machine.

Linux Tar Command Examples

Linux Tar Command Examples

In this article we will going to review and discuss various tar command examples including how to create archive files using (tar, tar.gz and tar.bz2) compression, how to extract archive file, extract a single file, view content of file, verify a file, add files or directories to archive file, estimate the size of tar archive file, etc.

The main purpose of this guide is to provide various tar command examples that might be helpful you to understand and become expert in tar archive manipulation.

1. Create tar Archive File

The below example command will create a tar archive file tecmint-14-09-12.tar for a directory /home/tecmint in current working directory. See the example command in action.

# tar -cvf tecmint-14-09-12.tar /home/tecmint/


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

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

2. Create tar.gz Archive File

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

# tar cvzf MyImages-14-09-12.tar.gz /home/MyImages
# tar cvzf MyImages-14-09-12.tgz /home/MyImages


3. Create tar.bz2 Archive 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. The following example of command will create a Phpfiles-org.tar.bz2 file for a directory /home/php. (Note: tar.bz2 and tbz is similar as tb2).

# tar cvfj Phpfiles-org.tar.bz2 /home/php
# tar cvfj Phpfiles-org.tar.tbz /home/php
# tar cvfj Phpfiles-org.tar.tb2 /home/php


4. Untar tar Archive File

To untar or extract a tar file, just issue following command using option x (extract). For example the below command will untar the file public_html-14-09-12.tar in present working directory. If you want untar in a different directory then use option as -C (specified directory).

## Untar files in Current Directory ##
# tar -xvf public_html-14-09-12.tar

## Untar files in specified Directory ##
# tar -xvf public_html-14-09-12.tar -C /home/public_html/videos/


5. Uncompress tar.gz Archive File

To Uncompress tar.gz archive file, just run following command. If would like to untar in different directory just use option -C and the path of the directory,  like we shown in the above example.

# tar -xvf thumbnails-14-09-12.tar.gz


6. Uncompress tar.bz2 Archive File

To Uncompress highly compressed tar.bz2 file, just use the following command. The below example command will untar all the .flv files from the archive file.

# tar -xvf videos-14-09-12.tar.bz2


7. List Content of tar Archive File

To list the content of tar archive file, just run the following command with option t (list content). The below command will list the content of uploadprogress.tar file.

# tar -tvf uploadprogress.tar

-rw-r--r-- chregu/staff   2276 2011-08-15 18:51:10 package2.xml
-rw-r--r-- chregu/staff   7877 2011-08-15 18:51:10 uploadprogress/examples/index.php
-rw-r--r-- chregu/staff   1685 2011-08-15 18:51:10 uploadprogress/examples/server.php
-rw-r--r-- chregu/staff   1697 2011-08-15 18:51:10 uploadprogress/examples/info.php
-rw-r--r-- chregu/staff    367 2011-08-15 18:51:10 uploadprogress/config.m4
-rw-r--r-- chregu/staff    303 2011-08-15 18:51:10 uploadprogress/config.w32
-rw-r--r-- chregu/staff   3563 2011-08-15 18:51:10 uploadprogress/php_uploadprogress.h
-rw-r--r-- chregu/staff  15433 2011-08-15 18:51:10 uploadprogress/uploadprogress.c
-rw-r--r-- chregu/staff   1433 2011-08-15 18:51:10 package.xml

8. List Content tar.gz Archive File

Use the following command to list the content of tar.gz file.

# tar -tvf staging.tecmint.com.tar.gz

-rw-r--r-- root/root         0 2012-08-30 04:03:57 staging.tecmint.com-access_log
-rw-r--r-- root/root       587 2012-08-29 18:35:12 staging.tecmint.com-access_log.1
-rw-r--r-- root/root       156 2012-01-21 07:17:56 staging.tecmint.com-access_log.2
-rw-r--r-- root/root       156 2011-12-21 11:30:56 staging.tecmint.com-access_log.3
-rw-r--r-- root/root       156 2011-11-20 17:28:24 staging.tecmint.com-access_log.4
-rw-r--r-- root/root         0 2012-08-30 04:03:57 staging.tecmint.com-error_log
-rw-r--r-- root/root      3981 2012-08-29 18:35:12 staging.tecmint.com-error_log.1
-rw-r--r-- root/root       211 2012-01-21 07:17:56 staging.tecmint.com-error_log.2
-rw-r--r-- root/root       211 2011-12-21 11:30:56 staging.tecmint.com-error_log.3
-rw-r--r-- root/root       211 2011-11-20 17:28:24 staging.tecmint.com-error_log.4

9. List Content tar.bz2 Archive File

To list the content of tar.bz2 file, issue the following command.

# tar -tvf Phpfiles-org.tar.bz2

drwxr-xr-x root/root         0 2012-09-15 03:06:08 /home/php/
-rw-r--r-- root/root      1751 2012-09-15 03:06:08 /home/php/iframe_ew.php
-rw-r--r-- root/root     11220 2012-09-15 03:06:08 /home/php/videos_all.php
-rw-r--r-- root/root      2152 2012-09-15 03:06:08 /home/php/rss.php
-rw-r--r-- root/root      3021 2012-09-15 03:06:08 /home/php/index.php
-rw-r--r-- root/root      2554 2012-09-15 03:06:08 /home/php/vendor.php
-rw-r--r-- root/root       406 2012-09-15 03:06:08 /home/php/video_title.php
-rw-r--r-- root/root      4116 2012-09-15 03:06:08 /home/php/report.php
-rw-r--r-- root/root      1273 2012-09-15 03:06:08 /home/php/object.html

10. Untar Single file from tar File

To extract a single file called cleanfiles.sh from cleanfiles.sh.tar use the following command.

# tar -xvf cleanfiles.sh.tar cleanfiles.sh
# tar --extract --file=cleanfiles.sh.tar cleanfiles.sh


11. Untar Single file from tar.gz File

To extract a single file tecmintbackup.xml from tecmintbackup.tar.gz archive file, use the command as follows.

# tar -zxvf tecmintbackup.tar.gz tecmintbackup.xml
# tar --extract --file=tecmintbackup.tar.gz tecmintbackup.xml


12. Untar Single file from tar.bz2 File

To extract a single file called index.php from the file Phpfiles-org.tar.bz2 use the following option.

# tar -jxvf Phpfiles-org.tar.bz2 home/php/index.php
# tar --extract --file=Phpfiles-org.tar.bz2 /home/php/index.php


13. Untar Multiple files from tar, tar.gz and tar.bz2 File

To extract or untar multiple files from the tar, tar.gz and tar.bz2 archive file. For example the below command will extract “file 1” “file 2” from the archive files.

# tar -xvf tecmint-14-09-12.tar "file 1" "file 2" 

# tar -zxvf MyImages-14-09-12.tar.gz "file 1" "file 2" 

# tar -jxvf Phpfiles-org.tar.bz2 "file 1" "file 2"

14. Extract Group of Files using Wildcard

To extract a group of files we use wildcard based extracting. For example, to extract a group of all files whose pattern begins with .php from a tar, tar.gz and tar.bz2 archive file.

# tar -xvf Phpfiles-org.tar --wildcards '*.php'

# tar -zxvf Phpfiles-org.tar.gz --wildcards '*.php'

# tar -jxvf Phpfiles-org.tar.bz2 --wildcards '*.php'


15. Add Files or Directories to tar Archive File

To add files or directories to existing tar archived file we use the option r (append). For example we add file xyz.txt and directory php to existing tecmint-14-09-12.tar archive file.

# tar -rvf tecmint-14-09-12.tar xyz.txt

# tar -rvf tecmint-14-09-12.tar php

drwxr-xr-x root/root         0 2012-09-15 02:24:21 home/tecmint/
-rw-r--r-- root/root  15740615 2012-09-15 02:23:42 home/tecmint/cleanfiles.sh
-rw-r--r-- root/root    863726 2012-09-15 02:23:41 home/tecmint/openvpn-2.1.4.tar.gz
-rw-r--r-- root/root  21063680 2012-09-15 02:24:21 home/tecmint/tecmint-14-09-12.tar
-rw-r--r-- root/root   4437600 2012-09-15 02:23:41 home/tecmint/phpmyadmin-
-rw-r--r-- root/root     12680 2012-09-15 02:23:41 home/tecmint/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
-rw-r--r-- root/root 0 2012-08-18 19:11:04 xyz.txt
drwxr-xr-x root/root 0 2012-09-15 03:06:08 php/ 
-rw-r--r-- root/root 1751 2012-09-15 03:06:08 php/iframe_ew.php 
-rw-r--r-- root/root 11220 2012-09-15 03:06:08 php/videos_all.php 
-rw-r--r-- root/root 2152 2012-09-15 03:06:08 php/rss.php 
-rw-r--r-- root/root 3021 2012-09-15 03:06:08 php/index.php 
-rw-r--r-- root/root 2554 2012-09-15 03:06:08 php/vendor.php 
-rw-r--r-- root/root 406 2012-09-15 03:06:08 php/video_title.php

16. Add Files or Directories to tar.gz and 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 tbe following error.

# tar -rvf MyImages-14-09-12.tar.gz xyz.txt

# tar -rvf Phpfiles-org.tar.bz2 xyz.txt

tar: This does not look like a tar archive
tar: Skipping to next header
tar: Error exit delayed from previous errors

17. How To Verify tar, tar.gz and tar.bz2 Archive File

To verfify any tar or compressed archived file we use option as W (verify). To do, just use the following examples of command. (Note : You cannot do verification on a compressed ( *.tar.gz, *.tar.bz2 ) archive file).

# tar tvfW tecmint-14-09-12.tar

tar: This does not look like a tar archive
tar: Skipping to next header
tar: Archive contains obsolescent base-64 headers
tar: VERIFY FAILURE: 30740 invalid headers detected
Verify -rw-r--r-- root/root    863726 2012-09-15 02:23:41 /home/tecmint/openvpn-2.1.4.tar.gz
Verify -rw-r--r-- root/root  21063680 2012-09-15 02:24:21 /home/tecmint/tecmint-14-09-12.tar
tar: /home/tecmint/tecmint-14-09-12.tar: Warning: Cannot stat: No such file or directory
Verify -rw-r--r-- root/root   4437600 2012-09-15 02:23:41 home/tecmint/phpmyadmin-
tar: /home/tecmint/phpmyadmin- Warning: Cannot stat: No such file or directory
Verify -rw-r--r-- root/root     12680 2012-09-15 02:23:41 home/tecmint/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
tar: /home/tecmint/rpmforge-release-0.5.2-2.el5.rf.i386.rpm: Warning: Cannot stat: No such file or directory
Verify -rw-r--r-- root/root         0 2012-08-18 19:11:04 xyz.txt
Verify drwxr-xr-x root/root         0 2012-09-15 03:06:08 php/

18. Check the Size of the tar, tar.gz and tar.bz2 Archive File

To check the size of any tar, tar.gz and tar.bz2 archive file, use the following command. For example the below command will display the size of archvie file in Kilobytes (KB).

# tar -czf - tecmint-14-09-12.tar | wc -c

# tar -czf - MyImages-14-09-12.tar.gz | wc -c

# tar -czf - Phpfiles-org.tar.bz2 | wc -c

Tar Usage and Options

  1. c – create a archive file.
  2. x – extract a archive file.
  3. v – show the progress of archive file.
  4. f – filename of archive file.
  5. t – viewing content of archive file.
  6. j – filter archive through bzip2.
  7. z – filter archive through gzip.
  8. r – append or update files or directories to existing archive file.
  9. W – Verify a archive file.
  10. wildcards – Specify patters in unix tar command.

That’s it for now, hope the above tar command examples are enough for you to learn and for more information please use man tar command. If we’ve missed any example please do share with us via comment box and please don’t forget to share this article with your friends. This is the best way to say thanks…..

Ravi Saive

Owner at TecMint.com
Simple Word a Computer Geek and Linux Guru who loves to share tricks and tips on Internet. Most Of My Servers runs on Open Source Platform called Linux.

Your name can also be listed here. Work as a Paid freelancer/writer at TecMint.
Download Free eBooks
Advanced Bash-Scripting Guide
Linux Bible
A Newbie's Getting Started Guide to Linux
Ubuntu Linux Toolbox: 1000+ Commands

57 Responses

  1. Rajkumar.m says:

    Very Helpfull

    • ayush pratap singh says:

      Dear Ravi,

      Please tell me the -A option example use to append tar files to an archive.

      • Ravi Saive says:

        Dear Ayush,

        You can include additional files to an already created tar archive, just use the following command example to add or append files to any existing tar file.

        # $ tar rvf tecmint.tar newfile
  2. Miguel Mateos says:

    I found this article very helpful, especially item #16. Thank you!

  3. Zakaria says:


  4. flatcap says:

    The examples in section 13 won’t work as you suggest.
    You haven’t escaped the spaces in the filenames, so tar will try to add three files:
    “file”, “1”, “file” (again) and “2”.
    I suggest changing the example to use “file1″ and “file2″.

  5. pen says:

    Excellent tips, one more for the road: How to copy a directory structure

    # tar -cf – olddir | ( cd newdir; tar xvf – )

    • John Lauro says:

      That is handy, and often times faster than cp -r. Taking it one step further…

      tar -cf – dir | ssh user@remotehost “cd /newparent ; tar xvf -”

      rsync is faster keeping directories in sync, but tar is much faster the first time, especially with lots of small files.

      or to go the other way, and clone a directory from remote to local:
      ssh user@remotehost “cd /parent ; tar cf – dir” | tar xf -

  6. Christopher says:

    Number 18 does not do what you say it does. You say that it displays the size of an archive file in kilobytes, but what it actually does is creates a new gzip-compressed archive file (to an unnamed pipe) containing a single file (the archive file that you are supposedly “checking”) and displays the size of that new archive file in bytes (not in kilobytes).

    Here’s an easier and faster way to display the size of an archive file (or any other type of file) in kilobytes:

    ls -lk archive.tar
    ls -lk archive.tar.gz
    ls -lk archive.tar.bz2

    (The -k option is not standard but is supported by at least the Linux and BSD versions of ls.)

  7. pablo7 says:

    Don’t you need to add the “j” option to extract the bzipped tar file in example 6? I get errors when I don’t.

    Thanks for the all the examples. I wish more were included in manpages.

    • Christopher says:

      Some versions of tar will detect the compression type and will automatically decompress it with the correct program (gzip or bzip2). Others will always assume it’s a regular, uncompressed tar file unless you pass it a “z” or “j” option. Some older versions of GNU tar even used the “I” (capital i) instead of “j” for bzip2 compression, and this is mentioned in the man page. The most portable method (I believe) is to decompress the file and pipe it directly to tar for extraction/listing:

      zcat archive.tgz | tar -xf –
      bzcat archive.tbz | tar -xf -

  8. Abdul says:

    how about excluding some files and directories and ignoring files or directories when backing-up.

  9. Ivan says:

    Any way to exclude a folder or subfoler or file with tar itself. (other than changing it’s chmod to 666)

    • Ravi Saive says:

      The tar command has options to exclude certain files. For example to exclude file ‘abc’ and ‘xyz’ you can use use the command as follows.

      $ tar -zcvf /home/tecmint.tar.gz --exclude='abc' --exclude='xyz' /home/tecmint
      • Arvind says:

        Any way to exclude a folder or subfoler or file with tar itself.

        You have mentioned a option to exclude a file, not folder.

        Please correct it..

    • Ravi Saive says:

      Same command is used to exclude files and folders.

  10. Volka Racho says:

    Is there a way to resume a tar extract for a large file which was interupted due to script time out from the apache webserver?

  11. L Reddy says:

    My english not good pls excuse me, my ? is
    I want take daily backup and incremental backup but i don’t want more than 10 days backup file pls write cmd

  12. Rishi Raj says:

    Very helpful article. Thanks for writing and sharing.

    I’ll check whether ‘z’ and ‘j’ switches for tar.gz and tar.bz2 are needed in the following commands, respectively:

    tar -xvf thumbnails-14-09-12.tar.gz

    tar -xvf videos-14-09-12.tar.bz2

  13. Syed Zabi says:

    Thank you its relly very helpful

  14. Durgaprasad.r says:

    Very nice article. It’s very clear and easily understandable. Thanks for sharing this.

  15. AjayKumarBasuthkar says:

    Really useful Ravi and all.

    Like it is possible to Add a file to existing archive, it is possible to subtract a file from archive. man page indicates –delete.

    tar -vf abcd.tar –delete path/of/file/in/archive/fileName

    • Ravi Saive says:

      Dear Ajay,

      I never tried –delete option, but I think the option would work. Why not give a try and tell us.

      • AjayKumarBasuthkar says:

        It works and so I have posted ravi :)

        • Ravi Saive says:

          Thanks Ajay, will include command to this list. If possible can you post the command with its output.

          • ajaykumar says:

            Hello ravi & all, here it is:

            #List tar ball contents.
            $ tar -tf trial.tar

            # remove del.tst from ‘trial.tar’
            $ tar -vf trial.tar –delete del.tst

            #List tar ball contents.
            $ tar -tf trial.tar

          • Ravi Saive says:

            Dear Ajay,

            Thanks for your efforts, I will include in the list.

  16. Imran khan says:

    Thanks for great information but can you help me to tar the following



  17. prakashs says:

    thanks bro……….

  18. Ranga says:

    Hi Ravi,

    I am getting the following error message while trying to execute:
    tar -xvf public_html-14-09-12.tar -C /home/public_html/videos/

    Error message:
    File -C not present in the archive.
    File /home/public_html/videos/ not present in the archive.

    Can you please let me know what I am doing wrong here.
    Thanks in advance.

  19. vinodkengg says:

    Hi Ravi ,
    I want to tar 10TB of data on tape drive , size of tape drive is 3 TB & using ultrium 5 .Can please specify the command which will help me take this on tape drive .

    Thanks on advance .

    • Ravi Saive says:

      The /dev/st0 is the default tape drive device name in Linux. So, to take tar backup of data on tape drive, you need to first rewind the tape using the following command.

      # mt -f /dev/st0 rewind

      Now backup data onto tape drive. For example, here I am taking backup of /data directory.

      # tar -czf /dev/st0 /data
      • vinodkengg says:

        if my data is already .gz extention with total data size is 2.9 TB then can we take full data on 3 TB drive with below command or only 1.5 tB data will come on tape tape drive .

        tar -cvf
        or we should use tar – czf also any compression need to set on tape
        I using ultrium 5 , Hp data 3TB tape

        Pls help me on this

        Thanks in advance .

  20. Franklin Elmo says:

    Thanks, Very useful Information

  21. Subbarao says:

    Tar command usually copies the structure too, could you please tell me how not to copy the structure…

  22. Diego says:

    Hi Ravi,

    Thank you for such a useful post. Nicely done, comprehensive and to the point.

    Best Regards,


  23. Simit says:


    Thanks for this post, it’s very well written and useful. i have a question…

    Do you know if it Is it OK to add files to a new tar archive that are already zipped (with gzip) prior to archiving? If I were to subsequently gzip the new tar archive, does that create any problems?

    In other words, would this be OK:

    tar -czvf example.tar.gz inputfile1.gz inputfile2.gz

    If I were to ‘untar’ and gunzip example.tar.gz (e.g. tar -xzvf example.tar.gz), would inputfile1 and inputfile2 be outputed as .gz or will they be gunzipped too? I’m worried double zipping might cause files to be corrupted.

    Many thanks, SImit

  24. Amrutha says:

    tar -czf file7.tar
    is not working?/

  25. Malleshwari says:

    Really helpful document.

    Thanks Ravi for sharing this.

  26. Prashant says:


    I want to create .tar.gz file.
    For that I used following command
    tar -czvf example.tar.gz /home/username/Desktop/Test
    I want only tar.gz file for my Test directory.
    But It creates directory structure like
    /home/username/Desktop/Test/ and all content inside Test directory in my tar.gz file.
    and I dont want “/home/username/Desktop/” directories in my tar.gz
    How to do this ? Please help me.

    Thanks in advanced.

    • Ravi Saive says:

      You need to use -C switch to do this, like shown below.

      # tar -C /home/username/Desktop/Test -cvf example.tar.gz .
      • Prashant says:

        Thanks Ravi ..Thanks for quick reply .. Its working ..
        But If I extract example.tar.gz by right click on the file it displays example directory. But this is not happening with command line.
        If I use tar -xzf command for extract the file it displays all content in my example.tar.gz on target directory but insted of that I want example directory first and all content should be inside it.
        same as like how right click and extract works.

        Can you please help me ?
        Your help is very important for me.

        Thanks in advanced !

        • Ravi Saive says:

          Use, the following command, I hope it will solve your problem.

          # tar -xvf example.tar.gz -C example
          • Prashant says:

            If I put this command then it is showing

            tar: /home/prashant/Desktop/abc/example: Cannot open: No such file or directory
            tar: Error is not recoverable: exiting now

            The example directory is not exist

  27. Jim says:

    I just noticed the names of your image and flv files… haha. Thanks for the clear examples!

  28. Ravishankar says:

    hello ravi, thanks for the important stuff which u have shared with us..i am new to the linux and now only i am learning i want your support to get good knowledge in linux. if Any notes please send me to my mail id

    • Ravi Saive says:

      Dear Ravi, it’s not possible for me to send out emails to individual email id’s, better you subscribe to our email updates to receive such usefull stuff directly in your mailbox..

  29. rk says:

    Plz guide
    I want to fetch the file without extracting(need to paramatrize further )
    tar -Otvf TarFile –wildcards “*.tar.gz” or tar -tvf TarFile –wildcards ‘*.tar.gz’

  30. rk says:

    i am not able to do this

  31. langacker says:

    Thanks! Great tutorial for linux beginner!

  32. Franklin says:

    Note to the author:

    some of the filename is you tar examples look like adult photos to me (e.g. Miley**photo101203.flv). Just want to let you know in case that was not on purpose…

    Thanks for the helpful tutorial.

    • Ravi Saive says:

      What a finding….:) even I don’t know that I’ve used such images in my tar command examples. Actually I handle one large photo site in our media company. I think I tried those commands on that server and taken those images as example for tar command…..

  33. Ahmed says:

    Salaam Ravi,

    Thanks for this valuable tricks and tips, personally it helped me a lot.
    By the way I am new bie to linux.

    Ahmed Somali

Leave a Reply

This work is licensed under a (cc) BY-NC | TecMint uses cookies. By using our services, you comply to use of our cookies. More info: Privacy Policy.
© 2012-2014 All Rights Reserved.