There’s nothing worse than a friend or colleague sending you an archive file, and you realize you’ve never even heard of the format before. Even worse, you have to root around the Internet to try to find an application that can support it. Fortunately, there are command-line utilities for handling not just the common compression files, like
*.tgz, but also for the less mainstream files like
This article is going to make use of the very common commands
tar, which come standard with most modern Unix-based operating systems like macOS and Linux.
In addition to the usual suspects, we’re also going to discuss decompressing with
These commands aren’t usually included by default, so you may need to confer with your favorite package manager to get them installed.
Hopefully you have a compressed file handy that you’d like to decompress. If not, you should check out our article on Compressing Files and Directories.
There are quite a few different archive compression algorithms out there. “Zipping” files has become synonymous with compression as a whole, but it’s good to know about the other formats that are out there.
Here’s a list of the file extensions associated with the commands we’re going go be using in this article:
*.tar.gz, but also
tar is a special case on this list because it actually only handles the grouping of files, not compressing them.
tar relies on other utilities, like
gunzip, and even
7z, which results in the concatenation of file extensions.
You may be wondering why
rar files have been omitted. This is because the RAR format is proprietary, and while it’s a good format, I’d prefer to talk about command-line utilities that are open source and don’t carry a price tag to use.
Decompressing archive files
Now that we know which files are associated with which commands, let’s run through how to decompress these different archive files.
First up, and the arguably the most common compression format, ZIP files, using the
$ unzip filename.zip
Not much to it! Files created by
gzip are just as easy to decompress:
$ gunzip filename.gz
Keep in mind that
gunzip removes the original file, so if you want to keep the archive when you extract it’s contents, you will need to pass in the
$ gunzip -k filename.gz
gzip can only compress single files, so often times you’ll run into
*.tar.gz or “gzipped tarball” files, which have also run through
tar to create an archive and then
gzip to compress it.
For these types of files, you can use
tar with an argument to tell it run the file through
gunzip, another argument to tell it to extract the contents of the tape archive and yet another argument to tell it to create the files on the file system.
All together it looks like this:
$ tar -zxf filename.tar.gz
If you are trying to extract a tape archive file that hasn’t been compressed, you can omit the
-z argument and extract the contents of the uncompressed archive:
$ tar -xf filename.tar
bunzip2 command is purposefully similar to that of the
gzip command. It accepts similar arguments, and has similar behavior in regard to removing the original archive file.
It’s also similar in the sense that
bzip2 works on individual files, so for multiple files, you’ll need our good friend
tar to manage the collection of files:
$ bunzip2 -k filename.bz2 $ tar -jxf filename.tar.bz2 # Or more verbosely $ tar --bzip2 -xf filename.tar.bz2
Similar to ZIP archives, 7-ZIP archive files don’t need to be run through
tar directly, for support of multiple files.
Where the commands, and specifically the compression algorithms differ, is that 7-ZIP files are 30% to 50% smaller than the same files archived with ZIP.
To decompress a 7-ZIP archive, simply run:
$ 7z e filename.7z
With the power of the command-line, you never have to be stuck trying to find an application that can extract even the most esoteric compression formats.
The most common commands tend to come standard and most of the more modern and robust package managers allow you to quickly and easily install the just about any command you may need.