How many times have you come across a .tar, .tgz, or .bz2 file in Linux and had no idea what to do with it? Or what it was? Most users of computers know what the .zip file is. Simple, it's a compressed file. And to uncompress you generally double click it or right click and select Extract. Most people recognize that from the world of Windows. But did you know that file compression/decompression is just as easy in the world of Linux? It is. The only problem is that most don't recognize what files are, in fact, compressed files, archives, or compressed archives.
In this article I am going to de-mystify Linux file compression and archiving for you. By the end of the article you'll be compressing and decompressing files in your sleep. And, as a bonus, you'll know how to do this both via command line and GUI.
Compression versus archiving
The difference between compression and archiving is simple: Compression makes a file (or directory) smaller and Archiving takes many seperate files (such as a directory containing sub-directories and/or files) and turns it into a single file. You can have a compressed archive (such as .tar.gz or .tgz) and you can even have an archive of compressed files (such as a directory that contains a bunch of compressed files). But how do you know what are compressed files verses archived files? Here's a list of file extensions and what they are.
.tar - This is an uncompressed Tar archive file. This type of archive is unpacked with the command tar xvf file.tar.
.tgz - This is a compressed Tar archive file. This type of archive is decompressed and unpacked with the command tar xvzf file.tgz.
.gz - This is a file compressed with the gzip program. This type of file is decompressed with the command gunzip file.gz.
.zip - This is a compressed file. This type of file is decompressed with the command unzip file.zip
.bz2 - This is a compressed file. This type of file is decompressed with the command bunzip2 file.bz2.
Which compression to use?
Of the three compression utilities (bzip2, zip, and gzip) the tool that will result in the most compression (the smallest file) is bzip2. The only disadvantage to bzip2 is that the algorithm uses more processor to complete than does the other utilities.
If you are sharing your compressed files with Windows users your best bet is to use zip for compression. Although there are tools (such as Stuffit Deluxe for Windows) that can handle bz2 files, the default supported is .zip.
Using the GUI
If you are a fan of the GUI (and who isn't these days?) you will be glad to know that most all Linux file managers can handle compressed and archived files. In both Nautilus (GNOME) and Dolphin (KDE) you can simply right click a compressed, archived, or compressed archive file and select Extract. Both file managers also offer compression and archiving options when you right click a file or directory.
Working with compression and archiving is very simple with the Linux operating system. You have plenty of options in both the command line and the GUI worlds.
Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.
We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats (video ads) or subscription fees.
If you like our content, and would like to help, please consider making a contribution:
Ghacks is a technology news blog that was founded in 2005 by Martin Brinkmann. It has since then become one of the most popular tech news sites on the Internet with five authors and regular contributions from freelance writers.