The Ultimate Tar Command Tutorial with 10 Practical Examples

by SathiyaMoorthy on April 26, 2010

On Unix platform, tar command is the primary archiving utility. Understanding various tar command options will help you master the archive file manipulation.

In this article, let us review various tar examples including how to create tar archives (with gzip and bzip compression), extract a single file or directory, view tar archive contents, validate the integrity of tar archives, finding out the difference between tar archive and file system, estimate the size of the tar archives before creating it etc.,

1. Creating an archive using tar command

Creating an uncompressed tar archive using option cvf

This is the basic command to create a tar archive.

$ tar cvf archive_name.tar dirname/

In the above command:

  • c – create a new archive
  • v – verbosely list files which are processed.
  • f – following is the archive file name

Creating a tar gzipped archive using option cvzf

The above tar cvf option, does not provide any compression. To use a gzip compression on the tar archive, use the z option as shown below.

$ tar cvzf archive_name.tar.gz dirname/
  • z – filter the archive through gzip

Note: .tgz is same as .tar.gz

Note: I like to keep the ‘cvf’ (or tvf, or xvf) option unchanged for all archive creation (or view, or extract) and add additional option at the end, which is easier to remember. i.e cvf for archive creation, cvfz for compressed gzip archive creation, cvfj for compressed bzip2 archive creation etc., For this method to work properly, don’t give – in front of the options.

Creating a bzipped tar archive using option cvjf

Create a bzip2 tar archive as shown below:

$ tar cvfj archive_name.tar.bz2 dirname/
  • j – filter the archive through bzip2

gzip vs bzip2: bzip2 takes more time to compress and decompress than gzip. bzip2 archival size is less than gzip.

Note: .tbz and .tb2 is same as .tar.bz2

2. Extracting (untar) an archive using tar command

Extract a *.tar file using option xvf

Extract a tar file using option x as shown below:

$ tar xvf archive_name.tar
  • x – extract files from archive

Extract a gzipped tar archive ( *.tar.gz ) using option xvzf

Use the option z for uncompressing a gzip tar archive.

$ tar xvfz archive_name.tar.gz

Extracting a bzipped tar archive ( *.tar.bz2 ) using option xvjf

Use the option j for uncompressing a bzip2 tar archive.

$ tar xvfj archive_name.tar.bz2

Note: In all the above commands v is optional, which lists the file being processed.

3. Listing an archive using tar command

View the tar archive file content without extracting using option tvf

You can view the *.tar file content before extracting as shown below.

$ tar tvf archive_name.tar

View the *.tar.gz file content without extracting using option tvzf

You can view the *.tar.gz file content before extracting as shown below.

$ tar tvfz archive_name.tar.gz

View the *.tar.bz2 file content without extracting using option tvjf

You can view the *.tar.bz2 file content before extracting as shown below.

$ tar tvfj archive_name.tar.bz2

4. Listing out the tar file content with less command

When the number of files in an archive is more, you may pipe the output of tar to less. But, you can also use less command directly to view the tar archive output, as explained in one of our previous article Open & View 10 Different File Types with Linux Less Command — The Ultimate Power of Less.

5. Extract a single file from tar, tar.gz, tar.bz2 file

To extract a specific file from a tar archive, specify the file name at the end of the tar xvf command as shown below. The following command extracts only a specific file from a large tar file.

$ tar xvf archive_file.tar /path/to/file

Use the relevant option z or j according to the compression method gzip or bzip2 respectively as shown below.

$ tar xvfz archive_file.tar.gz /path/to/file

$ tar xvfj archive_file.tar.bz2 /path/to/file

6. Extract a single directory from tar, tar.gz, tar.bz2 file

To extract a single directory (along with it’s subdirectory and files) from a tar archive, specify the directory name at the end of the tar xvf command as shown below. The following extracts only a specific directory from a large tar file.

$ tar xvf archive_file.tar /path/to/dir/

To extract multiple directories from a tar archive, specify those individual directory names at the end of the tar xvf command as shown below.

$ tar xvf archive_file.tar /path/to/dir1/ /path/to/dir2/

Use the relevant option z or j according to the compression method gzip or bzip2 respectively as shown below.

$ tar xvfz archive_file.tar.gz /path/to/dir/

$ tar xvfj archive_file.tar.bz2 /path/to/dir/

7. Extract group of files from tar, tar.gz, tar.bz2 archives using regular expression

You can specify a regex, to extract files matching a specified pattern. For example, following tar command extracts all the files with pl extension.

$ tar xvf archive_file.tar --wildcards '*.pl'

Options explanation:

  • –wildcards *.pl – files with pl extension

8. Adding a file or directory to an existing archive using option -r

You can add additional files to an existing tar archive as shown below. For example, to append a file to *.tar file do the following:

$ tar rvf archive_name.tar newfile

This newfile will be added to the existing archive_name.tar. Adding a directory to the tar is also similar,

$ tar rvf archive_name.tar newdir/

Note: You cannot add file or directory to a compressed archive. If you try to do so, you will get “tar: Cannot update compressed archives” error as shown below.

$ tar rvfz archive_name.tgz newfile
tar: Cannot update compressed archives
Try `tar --help' or `tar --usage' for more information.

9. Verify files available in tar using option -W

As part of creating a tar file, you can verify the archive file that got created using the option W as shown below.

$ tar cvfW file_name.tar dir/

If you are planning to remove a directory/file from an archive file or from the file system, you might want to verify the archive file before doing it as shown below.

$ tar tvfW file_name.tar
Verify 1/file1
1/file1: Mod time differs
1/file1: Size differs
Verify 1/file2
Verify 1/file3

If an output line starts with Verify, and there is no differs line then the file/directory is Ok. If not, you should investigate the issue.

Note: for a compressed archive file ( *.tar.gz, *.tar.bz2 ) you cannot do the verification.

Finding the difference between an archive and file system can be done even for a compressed archive. It also shows the same output as above excluding the lines with Verify.

Finding the difference between gzip archive file and file system

$ tar dfz file_name.tgz

Finding the difference between bzip2 archive file and file system

$ tar dfj file_name.tar.bz2

10. Estimate the tar archive size

The following command, estimates the tar file size ( in KB ) before you create the tar file.

$ tar -cf - /directory/to/archive/ | wc -c

The following command, estimates the compressed tar file size ( in KB ) before you create the tar.gz, tar.bz2 files.

$ tar -czf - /directory/to/archive/ | wc -c

$ tar -cjf - /directory/to/archive/ | wc -c

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 52 comments… read them below or add one }

1 krefik April 27, 2010 at 3:30 am

You missed few really useful examples, like ie.
tar cjf archive-$(date +%Y%m%d).tbz2 dir/
tar cjf archive-$(date +%Y%m%d).tbz2 dir/ -N ./archive-$(date -d yesterday +%Y%m%d)
for daily backups.

2 Ajeet S Raina April 27, 2010 at 4:08 am

I want to add up an important command too which most of us do face regularly:

Writing an archive in a different directory

You may also want to write an archive lto a different directory, like this:

tar czvf /tmp/mydirectory.tgz .

3 tej parkash April 27, 2010 at 7:36 am

tej@tej-desktop:~$ tar -czf – Music/ | wc -c
tej@tej-desktop:~$ tar -cjf – Music/ | wc -c

Why it is so. file tarred with j should be less in size????

4 Joe Black April 27, 2010 at 8:29 am

Tar command always seemed confusing to me. Your writeup clears any mis understanding. Nice work.

5 effe April 27, 2010 at 8:37 am

dear Ramesh,

first of all i’d like to thank you very much for your excellent work. being a newbie to BSD/linux thegeekstuff is my #1 choice to have a look at when it comes to help, understanding, and how-to… =:-) thank you very much again.

but right now, i am a bit confused: in 1. Creating an archvie using tar command, Note #2, you wrote that you like to keep the ‘cvf’ option unchanged for easy remembering, and you prefer to add additional options at the end. but if so, shouldn’t it be ‘cvfz’ for compressed gzip’s, and ‘cvfj’ for compressed bzip2′s? i mean to have ‘cvf’ together as you wrote one sentence before? did i miss something? or is it a simple spelling mistake?

again, sorry if it’s a stupid question, but keep in mind i’m still very new to tar (and all the other non-MS stuff), let’s call it doubtful, uncertain and precarious.

6 Karl O. Pinc April 27, 2010 at 9:57 am

No tar tutorial is complete without an example that does a cross-network copy.

This example uses GNU tar, with –numeric-owner and so can, as in this example, be used to copy an entire system onto a system with a blank disk drive that’s been put into another computer. The target system needs to have the new blank disk partitioned (fdisk, cfdisk, gparted, etc.), and filesystems created (mke2fs, etc.) and mounted (mount) in the appropriate places under /mnt/. It may be handy to boot the target system using a “live” Linux cd, usb key, etc.

If you are logged in, as root, on the target system, the one with the new drive, you use:

ssh ‘tar -C / –numeric-owner -czf – .’ | tar -C /mnt -xzpf -

If you are logged in, as root, on the source system, the one being copied, you use:

tar -C / –numeric-owner -czf – . | ssh ‘tar -C /mnt -xzpf -’

Note that the above command, or any other sort of whole system backup and restore, should _not_ be used to “clone” a system because of the many little bits of supposed-to-be-unique data that a modern system contains. Use your package management system to “clone”.

7 mario April 27, 2010 at 10:27 am

Tar is common. But tar doesn’t generate fully POSIX-compliant archives. A better tool is “pax”. It’s also more reliable in reconstructing file access rights. (And it handles both, tar and cpio formats.)

8 loeppel April 27, 2010 at 10:44 am

@tej parkash: The compressed size depends on the content of your Folder.

9 Amit Agarwal April 27, 2010 at 7:54 pm

with latest version of tar, the options z or j does not need to be specified for listing or extracting.

10 Roger Roelofs April 27, 2010 at 11:17 pm

Thanks for the useful article. The one suggestion I’d make is that the code to estimate final archive file size isn’t very useful in real life. The example for estimating the final size of the archive actually creates the archive, (in memory at least) so the estimate will take almost as long as actually doing the command in the first place. For small file sets you really don’t care about the size of the final output. For larger file sets where you are likely to care, the time to get the estimate is prohibitive. However that example is a good demonstration of the power of combining command line utilities by piping data between them. Great job!

11 matus May 7, 2010 at 7:26 am

Nice explanation.

12 jose May 7, 2010 at 12:44 pm

tar does not use regular expressions (or regexp). it uses globbing – please do not mix these terms

13 David May 15, 2010 at 3:28 pm

Thanks – I’m always forgetting how to run tar to create a .tar.gz

14 djraj May 28, 2010 at 1:25 am

Really cool Tutorial.

15 rene September 3, 2010 at 1:02 am

I was wondering if anybody here could help with a little tar issue. I work for a post production company and we regularly back up full reels onto LTO4s. We are talking about 25-30k sequential .dpx frames. Recently we have been asked to start writing them in 500 frame blocks. This at the moment is an extremely time consuming effort…

We move 500 frames into a temp folder and create an order file that we tar. Then we move those 500 back into the main folder and grab the next 500 and so on…

So my question is… if anybody knows how to make a tar script that would write up to 34 thousand frames sequentially in 500 frame blocks.

Thank you

16 Vinay September 7, 2010 at 3:35 am

Excellent notes on Tar with appropriate examples and apt explanations. Easy to refere and remember.Very Very Handy.

17 Gabriel SOE October 11, 2010 at 7:39 am

Thank you very much for a great tutorial.

18 Michael Vincent February 8, 2011 at 11:16 pm

describe in detail what the following command does..tar czvf tarfile.tgz /etc/sysconfig.
Can some one tell me what this new to linux..stil trying to learn it…really confused..what sites do you reccomend…

19 Roger February 9, 2011 at 12:40 pm


tar czvf tarfile.tgz /etc/sysconfig
c = create
z = compress
v = verify
f = filename to create
/etc/sysconfig everything in that directory will go into the tar file.

There are a ton of resources. I tend to just google what I need when the time comes. Also, typing ‘man tar’ on the commandline would give you lots more info.

20 sridhar February 14, 2011 at 4:24 am

very good super … wondeful thanks a lot

21 golf May 11, 2011 at 5:30 pm

Hello thank you for examples –

Can you specify proper example/syntax to create a tar of a folder&subfolders on one server and send to a different server….
Source folder/s to TAR /home/222/TRLIST2
destination server=
destination folder home/new/

Also do i need to uzip on other server…..
Can this all be done in one command line or does it require a
batch or script file

thank you for any help on this

22 lxtips August 26, 2011 at 2:08 pm

Hello, thank-you very much, and the peeps who posted useful comments

23 adbs98 September 21, 2011 at 11:41 am

This is a great guide and really helped me.

I would only add one command which is extremely helpfull and that is the -C option to push all files or just one file extracted to a certain directory of your choice

tar xvgj test.tar.bz2 -C /opt/test Being an example

24 Navin Pathak September 26, 2011 at 3:36 am

Very Nice Ramesh Ji very good article I understand this complete to perform any compression task in any organization……………….. cool article

25 Fredrick October 6, 2011 at 1:21 am

nice article, Is it possible to enter a command relating to a tar.gz file will tell you relative information about the file such as when it was created, its compression ratio and how many files it contains? cheers

26 gcino October 11, 2011 at 3:25 pm

I have an old round reel, need to extract data. When I type in command tar xvf /dev/rmt/0m one tape gives me data, another tape gives me error msg: tar: blocksize=4

How do I extract the second tape?

27 Jeff October 31, 2011 at 1:33 pm

Thanks mate, just what l needed!

28 Raghavendra March 12, 2012 at 4:18 am

Hi , any one can you Please give me the commanlines to create tar file in Windows …?

how to create a tar in windows as well as tar in to GZip ?


29 Dhirendra Rai March 27, 2012 at 2:38 am

As per example no [10] Estimate the tar archive size :
estimates the tar file size ( in KB ) before you create the tar file.

So, I ran this command to estimates the compressed tar file size :
[root@server1 ~]# tar -czf – My-selected/ | wc -c
I had doubt on size. Whether it is in Bytes or in KB? So I ran below command to know the exact size :
[root@server1 ~]# tar -zcf selected.tar.gz My-selected/
[root@server1 ~]# du selected.tar.gz
388440 selected.tar.gz
[root@server1 ~]# du -h selected.tar.gz
380M selected.tar.gz

Now what you say on size?

30 Gil May 22, 2012 at 4:20 am

Who can help with TAR????????????
I got a TAR archive from Unix user and i need to get 1 small file out of it for modification.
Ok i managed to take the file of tar and by ZIP-7 get the directory out of it
I managed to modify the file and now i want to pack back this folder into a TAR file (NOT TAR ARCHIVE i mean the file inside of it withour extantion)
I dont know how to do it. Every time i try to use TAR commands i get TAR Archive with folder inside not with sigle FILE
Does any one knows how or what format are this files inside archive? i use Windows and know how to add a FILE to TAR archive but not how to convert a FOLDER to a file that later ill Archive!!!!

Helpppppppppp 2 bloody weeks already and my boss on my case all day!!!!


31 Paulo Bettega June 1, 2012 at 7:56 pm

There is a way to do this tar -zxv -f /some/dir/* -C /another/dir
lots of tgzs in /some/dir and I want extract all to /another/dir

Using vars and subshells is the only way ?

ps. sorry for my english

32 ramesh oruganti June 4, 2012 at 7:43 am

it is very good examples and more descriptive ..

33 travis July 24, 2012 at 7:13 pm

@Paulo: the command that will get you what you are looking for is this:

ls -1 /some/dir/*.tar | xargs -L1 -I{} tar zvf {} /another/dir/

assuming you have xargs installed on your machine; and you should, it is common. To see what the commands will look like before they execute, run the command like this:

ls -1 /some/dir/*.tar | xargs -L1 -I{} echo tar xvf {} /another/dir/

and if they are what you are looking for, remove the echo command, and it will run. This will extract all the files that have .tar extensions in the source directory to the target directory. that is an ‘EL’ and an ‘EYE’ btw, in the xargs command, those switches inform xargs to execute each line of the ls output separately (-L1), and to replace the symbols {} with the output file (-I{}). Assume proper extensions, etc, etc… mileage may vary.

@Ramesh: tar does not use regular expressions, your example is for globbing, not regex, they are two different things.

34 travis July 24, 2012 at 7:33 pm

@Gil: it sounds like you are just decompressing the file with 7z. if you take that file, the one without the extension, and run THAT through tar, you will get your files. tar’s purpose is to concatenate single files (and folders) recursively into a single file, like a portable file system, that can then be easily moved between machines, but it does not compress anything itself, it relies on outside libraries to do that, like gzip or bzip2. So, if your file has an extension like tgz, ot tb2, etc., that is a file that is first tarballed, then it is compressed, so, you need to reverse the process, first decompress, then un-tar. if the extensions give you hints to the compression type, and they are gzip or bzip2, you can use the switches and tar will handle the decompression for you, else, you need to first decompress, then un-tar. You can use anything to compress a tar, once it is tarred, but only the ‘b’ and ‘j’ switches are available for in-process decompression (as far as I know). So, the short of it, if the compression type is hinted in the file extension, use the switches to decompress, and the command will look like this:

tar xv[zj]f file.[tgz|tb2] /output/folder
(the [] groups are your options; j with tb2 or z with tgz)

or, if the compression is known to be outside the scope of tar, such as rar, then the command would look like this (if using, say rar):

unrar file.trar | tar xvf – /output/folder


PS – since you are using 7z:

perhaps your command would look like

7z e file.t7z | tar xvf – /output/folder

Hopefully, that gives you the idea.

35 Paulo Bettega July 24, 2012 at 11:39 pm

@travis (33)
Thanks a lot, travis. I’m just starting to learn about shell, my next readings
will be man xargs. I was just curious if there is a way to do that with tar
directly. In my shell script a simple loop did the job :)
Thanks again, is in my shell script bookmark.

36 rajesh October 12, 2012 at 10:57 am

very nice examples

37 Sunil Kumar December 2, 2012 at 9:50 pm

It is very helpful .Thanks lot.

38 Indhumathi December 19, 2012 at 12:47 am

Compress the file using ‘tar (file should be replaced by tar file)

Could you please help me to find out this answer?

39 Roger December 19, 2012 at 9:08 am


Tar is a multi-file archiver, so a 1 to 1 correspondence between original and compressed file is not assumed. gzip/gunzip will do this. if you want to use tar, you can as a second step use the -t option to read the files in the tar archive and delete the originals.

tar -tzf compressed.tgz | while read line; do rm `echo $line | awk ‘{print $6}’` ; done

40 Indhumathi December 24, 2012 at 1:21 am

Thank you Roger

41 ali.abry February 13, 2013 at 6:35 am

in wc command the -c option shows amount of data in byte not KB according to its man page:
-c, –bytes print the byte counts

42 Bijesh Lal Nyachhyon February 28, 2013 at 1:08 am

I am user the following command to take my data backup in AIX:
gtar cvf – folder | compress -> filename.tar.Z

I need the command output redirected to a text file.
How do I do that?

43 krefik March 1, 2013 at 2:45 am

@Bijesh Lal Nyachhyon:

Try gtar cvf – folder | compress – > filename.tar.Z 2>logfile.txt

Second stream is STDERR

44 Arun March 28, 2013 at 8:59 am

Thanks a lot.

45 Pablo Ardila October 30, 2013 at 1:27 am

No matter now many times I’ve used tar I’ve always forgotten the correct switches to use. Thanks for this – it suddenly all seems so simple.

46 RK November 20, 2013 at 1:18 am

Thanks a lot.. It is very useful and handy

47 Farfellow November 24, 2013 at 1:35 pm

Thank you so much, it is very helpful for a beginner like me.

48 amar suryawanshi December 6, 2013 at 1:50 am

You r doing just nice job…
just im learning linux…you helped me a lot…about all commamds…because they r confusing to me
thank you…

49 javier gonzález December 10, 2013 at 2:58 pm

this is about the 10th time I google and open this article for a hint on tar … hope one day I finally learn the commands by heart :-)

50 Prashant March 4, 2014 at 1:59 pm

Hi Ramesh,

I want to learn Linux but i am not from technical background, could you please help me how to start this.

I will be very thankful to you if you can help me in understanding the basics of Linux.

51 David Swails May 19, 2014 at 6:37 pm

Just for information the option -f needs to be at the ends of the options otherwise the tar wont create the proper file you get an error

52 siva shankar July 3, 2014 at 12:25 am

Nice tutorial … thanks very much.

Hope to see many stuffs from geekstuff moving forward

Leave a Comment

Previous post:

Next post: