How to create and extract zip, tar, tar.gz and tar.bz2 files in Linux

Data compression has been extremely useful to us over the years. Whether its a zip file containing images to be sent in a mail or a compressed data backup stored on a server, we use data compression to save valuable hard drive space or to make the downloading of files easier. There are compression formats out there which allow us to sometimes compress our data by 60% or more. I’ll run you through using some of these formats to compress and decompress files and directories on a Linux machine. We’ll cover the basic usage of zip, tar, tar.gz and the tar.bz2 formats. These are some of the most popular formats for compression used on Linux machines.

Quick Note: If you’re looking for the Windows version of this tutorial, you can find it at How to Open tar.gz Files in Windows.

Before we delve into the usage of the formats I’d like to share some of my experience using the various formats of archiving. I’m talking about only a few data compression formats here, and there are many more out there. I’ve realized that I need two or three formats of compression that I’m comfortable using, and stick to them. The zip format is definitely one of them. This is because zip has become the de-facto standard choice for data compression, and it works on Windows as well. I use the zip format for files that I might need to share with Windows users. I like to use the tar.gz format for files that I would only use on my Mac and Linux machines.


Zip is probably the most commonly used archiving format out there today. Its biggest advantage is the fact that it is available on all operating system platforms such as Linux, Windows, and Mac OS, and generally supported out of the box. The downside of the zip format is that it does not offer the best level of compression. Tar.gz and tar.bz2 are far superior in that respect. Let’s move on to usage now.

To compress a directory with zip do the following:

# zip -r directory_to_compress

Here’s how you extract a zip archive:

# unzip


Tar is a very commonly used archiving format on Linux systems. The advantage with tar is that it consumes very little time and CPU to compress files, but the compression isn’t very much either. Tar is probably the Linux/UNIX version of zip – quick and dirty. Here’s how you compress a directory:

# tar -cvf archive_name.tar directory_to_compress

And to extract the archive:

# tar -xvf archive_name.tar.gz

This will extract the files in the archive_name.tar archive in the current directory. Like with the tar format you can optionally extract the files to a different directory:

# tar -xvf archive_name.tar -C /tmp/extract_here/


This format is my weapon of choice for most compression. It gives very good compression while not utilizing too much of the CPU while it is compressing the data. To compress a directory use the following syntax:

# tar -zcvf archive_name.tar.gz directory_to_compress

To decompress an archive use the following syntax:

# tar -zxvf archive_name.tar.gz

This will extract the files in the archive_name.tar.gz archive in the current directory. Like with the tar format you can optionally extract the files to a different directory:

# tar -zxvf archive_name.tar.gz -C /tmp/extract_here/


This format has the best level of compression among all of the formats I’ve mentioned here. But this comes at a cost – in time and in CPU. Here’s how you compress a directory using tar.bz2:

# tar -jcvf archive_name.tar.bz2 directory_to_compress

This will extract the files in the archive_name.tar.bz2 archive in the current directory. To extract the files to a different directory use:

# tar -jxvf archive_name.tar.bz2 -C /tmp/extract_here/

Data compression is very handy particularly for backups. So if you have a shell script that takes a backup of your files on a regular basis you should think about using one of the compression formats you learned about here to shrink your backup size.

Over time you will realize that there is a trade-off between the level of compression and the the time and CPU taken to compress. You will learn to judge where you need a quick but less effective compression, and when you need the compression to be of a high level and you can afford to wait a little while longer.

{ 53 comments… add one }
  • myhnet January 2, 2009, 6:00 am

    tar just for packaging, not for compress

  • angelo January 15, 2009, 6:38 am

    Utile consiglio, grazie, ma vorrei un consiglio
    metti che ho un sul sito e vorrei estrarlo li o su un’altra cartella, come posso fare?

    Forse basta scrivere un file.php con questo dentro?

    # Decomprimere

  • Como?? April 14, 2009, 8:50 am

    ┬┐Donde tengo que poner lo de
    #Zip-r directory_to_compress??

  • JL May 7, 2009, 3:46 pm

    Tar doesn’t compress anything, it just archives. Gzip compresses and so .tar.gz is a compressed archive.

  • marco August 4, 2009, 11:02 am

    How can i vote for this article? :P

  • linux user October 20, 2009, 7:51 am

    thank you !!!

  • D3M-TEAM November 9, 2009, 12:30 pm

    Thanks Iam Need A Program tar.gz

  • pepijn January 10, 2010, 12:01 pm

    How do I exclude hidden files? I’m using bsdtar 2.6.2 – libarchive 2.6.2 and the usual solutions don’t seem to work.

  • Rago January 15, 2010, 11:33 am


  • hossam khaled al-dalely January 21, 2010, 7:14 am


  • hossam khaled al-dalely January 21, 2010, 7:25 am

    hossa 1002

  • DASDAS February 17, 2010, 5:52 pm

    Muchas Gracias tio.

  • bwalls2 September 7, 2010, 1:53 am

    Can you compress tar with zip, not bzip or gzip or compress but zip? And if so would it have a zip extension?

  • bwalls2 September 7, 2010, 1:55 am

    Can you use tar with the zip utility? And if so would it have a zip extension. As far as I can tell you can only compress files with tar using bzip, gzip, compress having a .bz2, tgz, or Z extension(I think, might be slightly off). As far as I can tell, zip will work with linux but it doesent seem able to use files compressed with zip.

  • Frank December 10, 2010, 7:06 am

    good stuff. archiving is good ;]

  • rahman December 27, 2010, 2:46 pm

    thanks, that’s really a common question

  • Martin January 5, 2011, 8:51 pm

    Thank you very much!
    I keep forget/mix the compress options, you save my day!

  • Yaro Kasear January 21, 2011, 12:53 pm

    It pains me to say this, but tar is not compression at all. That’s why there IS tar.gz and tar.bz2.

    There are other formats our there that have a much better compression ratio than these. 7Zip and LZMO come to mind.

  • php ide January 31, 2011, 2:19 pm

    just to let you know that under unix freebsd i dont have unzip installed but the following command unzipped my zip file quite nicely

    tar -xf

    thanks for the info!

  • ss June 4, 2011, 5:44 pm

    .tar.gz and .tar.bz2 are common ways too compress the files

  • Nasiru Abdullahi August 15, 2011, 8:55 am

    From the Foregoing ; it means that we that used Windows operating system cannot access file with .tg, tar, etc, extensions. if it is possible how do i go about it pls.

  • Yaro Kasear August 15, 2011, 9:56 am

    @Nasiru Abdullahi – Well, out of the box you may not from Windows. My recommendation is to get 7-Zip. It opens just about any archive or compression format, including UNIX tarballs.

    Out of the box you don’t really get support for very many formats at all. Windows barely supports zip natively and that’s about all.

    Linux will usually start out with at least tar, gzip, and bzip2 support right out of the gate as usually its package manager and many other applications outright require it to even run. And things like basic rar and zip support are pretty easy to get running with a single request to the package manager.

  • newa December 6, 2011, 1:46 am


  • amber April 1, 2012, 12:28 am

    nice, tank you, everything at one breath!

  • Mikal April 6, 2012, 1:57 pm

    Great article
    you save my lot time

  • Erica October 10, 2012, 5:08 pm

    I’ve come back to this post time and time again to remind myself of the correct syntax (I don’t spend that much time in unix). Thanks for writing a great resource.

  • Francesco Casula March 6, 2013, 5:30 am

    You miss a way to specify the desired compression level. For example (with tar.gz):

    GZIP=-9 tar -cvzf archive.tar.gz file_to_compress.txt

    -9 is the max compression level, -6 is the default level

  • Akash Kumar February 5, 2014, 3:18 am

    thanks :)

  • Jacob February 13, 2014, 1:49 pm

    Thank you very much, noble man

  • djalmaaraujo March 25, 2014, 3:26 pm


  • Zaag May 8, 2014, 10:36 am

    thanks indeed!

  • Devendra Bhat July 19, 2014, 1:07 pm

    Thnx alot for clearing my doubt sir….

  • Scott Furry February 7, 2015, 11:45 pm

    Thank you!

  • Jesus Flores March 10, 2015, 11:02 pm

    It helped me, great work, thanks a lot!

  • Niculas Sergiu March 16, 2015, 2:26 pm

    You forgot a very important flag(parameter) the ‘p’ for keeping the same permissions of the files.

  • Abdul Hameed Rasheed August 3, 2015, 6:23 pm

    thank you for the info!

  • Jonathan V C February 26, 2016, 7:53 pm

    I knew about unzip, but I didn’t know zip was available for Linux. Thanks for the info!

  • Saad April 3, 2017, 10:38 pm

    Thank you!

Leave a Comment