How to extract a GZ file in Linux
Download and install software on a Linux PC? You will need to know how to open GZ files in Linux at some point. This guide will walk you through.
If you are using a Linux PC, you will occasionally need to open a file or folder that has been compressed to take up less disk space. This is done to save space or to allow you to upload or download a file faster.
In Windows, these files are usually compressed as zip files. Linux uses a similar algorithm, called gzip. If you want to interact with these files, you need to know how to extract a GZ file in Linux. Here’s how.
Gzip vs. GZ: what’s the difference?
GzipComment is the compression algorithm, reducing the file size while retaining the original file mode, ownership, and timestamps. A file compressed using the gzip algorithm will usually end with the file extension .gz or sometimes just .z.
Jean-loup Gailly and Mark Adler developed gzip for the GNU Project as a replacement for compress. The Unisys and IBM patents covered the LZW algorithm compression used. This made it impossible to include compress in all GNU offshoots.
Luckily, gzip uses a superior compression algorithm than compress. Gzip is widely used not only in Linux, but also in many open source software projects. You will even find the software in macOS,
Extracting a file compressed using gzip is easy. Just issue this command from the terminal:
gzip -d file.gz
This unpacks the file to its original state and removes the .gz file. If you want to keep the file compressed, just add the option -k to order:
gzip -dk file.gz
The command gunzip is also available on most systems, as an alias of gzip-d. This means that decompressing a file does not require the -D option if you use gunzip:
Again, this will unpack the archive and delete the compressed file. If you want to keep the compressed version of the file, you add again -k to order:
gunzip -k file.gz
It is important to note that the gzip algorithm is designed to compress a single file. If you need to bundle multiple files or an entire directory structure, you’ll use both tar and gz. Let’s look at extracting a tar.gz file.
In this case, we will not use gz. Instead, we use tar, a holdover from the days of tape archives (hence the name) that is still very useful today. To extract a tar.gz file, we use this command:
tar -xf archive.tar.gz
The command automatically determines the type of compression used and extracts the archive to the current working directory.
Creation of your own archives and GZ files
Know how to extract .gz and tar.gz files may be the most important task you need. However, it is also important to know how to create these archives and/or compressed files in the first place.
If you need to compress a file, use the command again gzipbut with no options other than the file you want to compress.
This command will compress annual-profit.xlsx. The original file is deleted, leaving only the compressed version annual-profit.xlsx.gz. If you want to keep the original in place, add the -k option.
gzip -k annual-profit.xlsx
Now suppose we want to create a compressed archive of our documents directory, which contains a number of subdirectories. We want to preserve the directory structure, put everything into a single archive, and then compress it. Just issue this command from the terminal:
tar -czvf documents.tar.gz /home/jeff/documents
There are a lot of options out there, so let’s look at what each does.
- -vs: To create an archive
- -z: Compress the archive using gzIP.
- -v: Shows the progress of the archive creation. This is known as verbose fashion.
- -F: Specifies the file name archives.
One of the major aspects of tar is that you can include multiple directories in the archive. If we want to save our documents, Ddownloadsand ppictures directories in a single archive, we just run this command:
tar -czvf archive.tar.gz /home/jeff/documents /home/jeff/Downloads /home/jeff/Pictures
Compressing and decompressing files using the GUI
If you’d rather not work in the terminal, graphical desktop environments have what you need. At Gnome Files application, right-click the file you want to compress and click Compress.
If you want to archive and compress multiple directories and/or files, control-click the ones you need to back up, then right-click and choose Compress.
The file manager asks you which format you want to use. If you choose .Zip *: French, the compressed file will be easy to open on any operating system. You can also choose .tar.xz Where .7zbut these will require the correct software on Windows or Mac computers.
If there is a zipped file you need to open, right-click that archive again in Files. You will see a number of options including Open with Archive Manager, Extract hereand Extract to.
The archive manager gives you more options, but most of the time you’ll want to use Extract here Where Extract to.
Working with Files and Other Administrative Tasks in Linux
If you want to work with gzip files, the steps above should help you zip and unzip them on a Linux PC.
However, there are other important tasks that you will need to learn if you want to become a Linux pro. Be sure to check out our guide on finding files in Linux, as well as how to change your password.