7 Ways to Zip and Unzip Files in Linux
Which one is your favorite and why?
More and faster are always two things we want, especially when it comes to data. The problem is that “more” and “faster” are usually at odds, so we havefile compression. There are several ways to zip and unzip files in Linux, and we’re going to show you the methods behind them.
Zip and Unzip Using Zip in Linux
Linux has several compression tools built into almost every distribution, commonly known asLinux distros. Zip is likely the most popular. The Zip utility can be used in the graphical user interface (GUI) or the terminal.
Zip in the Linux GUI
Unzip in the Linux GUI
The files are extracted. Notice that they’re back to their full size of 100 MB each.
Archive Manager to Unzip in the Linux GUI
Some Linux distros have other ways to unzip built into them. In this example, you can use Archive Manager.
The file is back at its full size. A copy is left inside the archive.
Zip Files in Linux Terminal
Open the terminal and navigate to the directory where the files to be zipped are located. Enter the commandzip ziptest.zip *.
Ziptells Linux to use the zip utility,ziptest.ziptells it the desired name for the archive, theasterisk (*)is a wildcard meaning zip all the files in this directory.
It zips the files, lists them, and says how much it has deflated or compressed them.
Many actions can be used with the zip command. To view them, enterzip –help,and you’ll see something like the following image.
Unzip Files in Linux Terminal
In the Terminal, use the commandunzip ziptest.zip, whereunzipis the command andziptest.zipis the name of the archive to unzip.
It will show the files being unzipped, so you know when it’s done.
Like the zip command, many actions can be used with the unzip command. To view them, enterunzip –help,and you’ll see something like the following image.
Bzip2 for Zipping and Unzipping Files in Linux
Bzip2 is another compression utility built into most Linux distros. A major difference is that bzip2 cannot zip multiple files into one archive. Each file gets its own zipped archive.
Zip Files in Linux Terminal With Bzip2
Enter the commandbzip2 -kv9 testfile1.txt testfile2.txtwhere bzip2 is the command.
-kv9breaks down tokmeanskeep the originals,vmeansverbose so we can see what’s happening, and9for the highest level of compression. You can choose between 1 and 9. The higher the level of compression, the longer zipping the files takes.
The output tells us more than zip does, but the end result is almost the same.
Unzip Files In Linux Terminal With Bzip2
Enter the commandbzip2 -kvd testfile.1.txt.bz2 testfile2.txt.bz2. The-kvdoptions break down askforkeep the archives,vforverbose output, anddfordecompress.
You see the files being decompressed and known when it’s done.
To view the bzip2 options, enterbzip2 –help,and you’ll see the following. Play around with the options on non-critical files just to see what they can do.
Gzip for Zipping and Unzipping Files in Linux
This is the last of the popular compression utilities that are included with most distros. It’s lighter thanbzip2andzipfor options. However, the quality of compression is still about the same.
Zip Files in Linux Terminal With Gzip
Enter the commandgzip2 -kv9 testfile.1.txt testfile2.txt. The-kv9options break down askforkeep the files,vforverbose output, and9for the highest compression level between 1 and 9.
As the verbose output shows, gzip works about as well as the other zipping methods.
Unzip Files in Linux Terminal With Gzip
There are two ways to unzip gzip archives. One is to use the gzip, and the other is gunzip.
For thegzipcommand, usegzip -kvd testfile1.txt.gz testfile2.txt.gz. Notice thedoption. That means todecompress.
For thegunzipcommand, usegunzip testfile1.txt.gz testfile.2.txt.gz. The only difference from gzip is that gunzip doesn’t require options for a basic decompression.
What About Tar to Zip and Unzip Files in Linux?
Why hasn’ttarbeen mentioned yet? It’s an archiving tool, taking a bunch of files and putting them into one archive for easy transport. Whatever the file sizes are, the size of the tar file will be about the same.
But if you combine a zip method with tar, then you get something really cool. You get a nicely compressed single package of files.
Using the other zip methods on a directory of files, you’d get a compressed archive for each file in the directory. Using tar with the gzip option on the directory compresses everything and makes one archive.
Zip Files in Linux Terminal With Tar and Gzip
Enter the commandtar -czvf Documents.tgz Documents.
The-czvfoptions break down ascforcreate a new archive,zfor compress with gzip,vforverbose output, andfforfile equals archive, which means the archive maintains the file structure of the original directory.
The new archive must be named, which isDocuments.tgzin this example. By using the.tgzfile extension, others will know that this is a tar archive that has been gzipped. FinallyDocumentsis the directory to archive and compress.
The output looks like the following.
In File Manager, you can see the tar archive and that it’s compressed.
Unzip Files in Linux Terminal With Tar and Gzip
To unzip a gzipped tar archive, it’s the same tar command with slightly different options.
Enter the commandtar -xzvf Documents.tgz Documents.
The-xzvfoptions break down as x for extract,zfor unzip with gzip, vfor verbose so we can watch it happen, andffor file=archive means keep the file structure.Documents.tgzis the archive to be unpacked and unzipped, andDocumentsis the directory into which you want the contents to go.
The results are shown in the image below. The two files are back to full size, and they are in the Documents directory.
To view the tar options, entertar –help,and you’ll get several pages of options. Notice that there are different zip methods available other than gzip, so you can choose the one you like.
Less Popular Zip Tools in Linux
There are two more compression utilities found in most Linux distros. However, they’re not that popular. Nevertheless, they’re listed here, so you’re aware of them.
LZMA is another command-line compression utility often found in Linux distros. It’s the compression algorithm used by 7-Zip.
The XZ utility is a command-line compression tool often included in Linux distros. Its options are similar to those of bzip2. It’s based on the LZMA2 algorithm, which is based on LZMA.
More information about these utilities can be found by using the commandslzma –helpandxz –help.
Aren’t There Other Ways To Zip and Unzip in Linux?
We’ve only gone into the compression tools built into most Linux distros. Still, Linux has several other ways to zip and unzip files. Some have GUIs, some are command-line utilities, and some can be used both with their GUI and through the command line. Do you use one of these zip tools in Linux? Which one is your favorite and why? Let us know in the comments.
Guy has been published online and in print newspapers, nominated for writing awards, and cited in scholarly papers due to his ability to speak tech to anyone, but still prefers analog watches.Read Guy’s Full Bio
Welcome to Help Desk Geek- a blog full of tech tips from trusted tech experts. We have thousands of articles and guides to help you troubleshoot any issue. Our articles have been read over 150 million times since we launched in 2008.
HomeAbout UsEditorial StandardsContact UsTerms of Use
Copyright © 2008-2024 Help Desk Geek.com, LLC All Rights Reserved