How to create and open zip, tar, tar.gz files in Linux
This guide will explain how to create and/or open and extract the contents of .ZIP, .TAR and .TAR.GZ files in the Linux operating system.
Background
Data compression has been extremely useful to us over the years. Whether it’s 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 Files in Linxu
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 archive_name.zip directory_to_compress
Here’s how you extract a zip archive:
# unzip archive_name.zip
TAR Files in Linux
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/
TAR.GZ Files in Linux
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/
TAR.BZ2 Files in Linux
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.
How do I zip/unzip on the unix command line?
@joachim, you should ask the question in a question and provide an answer in an answer. There’s no problem with answering your own questions but not as part of the question itself, since that breaks down the whole community-votes-on-the-best-answer advantage of SO. In addition, this is probably better suited for superuser since it’s not programming related.
@rlb.usa: I work in a lot of different places on a lot of different servers. Using SA for a note like this helps other people too, as I see it.
9 Answers 9
Typically one uses tar to create an uncompressed archive and either gzip or bzip2 to compress that archive. The corresponding gunzip and bunzip2 commands can be used to uncompress said archive, or you can just use flags on the tar command to perform the uncompression.
If you are referring specifically to the Zip file format, you can simply use the zip and unzip commands.
zip squash.zip file1 file2 file3
this unzips it in your current working directory.
Just as a side note: zip can create many different flavors of .ZIP archives, as well as use many different compressors. On the other hand, there are a number of other compression tools named similarly: 7-Zip, gzip, bzip2, rzip, etc. which are not interchangeable.
I’m confused, I did this and get a long list of inflations and extractions but when I check the directory it is empty, save for the original zip file? Where is it all going?
For the record, use unzip -d myfolder squash.zip to extract into a separate new directory ( myfolder ) instead of putting all the files directly into the current directory, which can be annoying if the zip file contains many files and directories in its top level. This mimics how most UI unzip tools work.
There are a truly vast number of different ways to compress and uncompress under UNIX derivatives so I’m going to assume you meant «zip» in the generic sense rather than a specific file format.
You can zip files up (in compressed format) with the GNU tar program:
which will do the current directory. Replace . with other file names if you want something else.
That’s assuming of course that you have a tar capable of doing the compression as well as combining of files into one.
If not, you can just use tar cvf followed by gzip (again, if available) for compression and gunzip followed by tar xvf .
For specific handling of ZIP format files, I would recommend downloading 7zip and using that — it recognises a huge variety of file formats, including the ZIP one.
Or use tar jcvf file.tar.bz2. to compress in bzip2 format or tar Jcvf file.tar.xz . for xz compression.
@Starfish, that’s certainly within your rights, thanks for at least explaining why, so many people don’t take the time to do that 🙂 I took «zip» in the more generic sense (compression) rather than a specific format, so I’ll make that more clear.
Be aware that tar archives may not work correctly when moving between different platforms, mac to linux or vice versa. Zip is much safer, as it works correctly on all systems.
I suppose technically this is gzip, so that’s fine. I guess. Pretty obviously not what the question asked for though.
If you don’t have zip and unzip packages installed and you have java, you can use jar to unzip:
Well, when it comes to distributing files for a variety of operating systems, I’d recommend 7-zip.
Usually in the package p7zip , you’ll get the 7z and 7za command, with which you can create your own 7z archives.
7za can also decompress standard (pkzip) zip archives (and create them as well with the -tzip switch).
7za a archive.7z file1 file2 directory/
It can also create self-extracting archives with the -sfx switch:
7za a -sfx archive.exe files1 file2 dir
I recommend this method in case Windows users can’t open 7z archives (in case you want to advice a tool for that: PeaZip).
If you want to use the same compression algorithm with your tarballs, use the -J switch with tar :
tar cJf archive.tar.xz file1 file2 dir
xz is a UNIX tool, that uses LZMA2 for compression, but works the way gz , bz2 , etc works. It even works as a filter.
7z doesn’t create archives with full filesystem information on UNIX, so you’d need to use tar before using 7z (but since 7z stores other information about the tar file, I’d recommend using xz , as it is designed for it):
tar cf - file1 file2 dir | 7za z -si archive.tar.7z