Use Tar, Gzip & Zip in Linux
Introduction
Use Tar, Gzip or ZIP to bundle multiple files together, compressing them to save space. Tar allows you to combine files into a single archive, while Gzip compresses files to reduce their size. Together, Tar and Gzip create the commonly used .tar.gz (or .tgz) archive file format, whereas ZIP creates .zip files.
This guide walks you step-by-step through creating, extracting, and working with Tar, Gzip and Zip files in Linux.
Prerequisites
- A Linux system with tar and gzip installed (most distributions include them by default).
- Basic familiarity with the terminal.
- A test folder or file to practice with.
Compress Files with Tar and Gzip
- Locate the target file or folder you want to compress. In our example, we are compressing ‘/root/my_folder’ into a file named archive.tar.gz located in the /root/ folder.
- Run the tar command making sure to include the destination followed by the target:
tar -czvf /root/archive.tar.gz /root/my_folderc – Creates a new archive
z – Compresses using Gzip
v – Verbose (shows progress)
f – Allows you to specify a filename
View a TAR.GZ file’s contents without extracting
- Locate the Archived file you would like to view. In this example, we’ll view the contents of archive.tar.gz stored in the /root folder without extracting it.
- Run the following command, making sure to specify the correct path to your archive:
tar -tvf /root/archive.tar.gzt – Lists the contents of the archive
v – Verbose (shows details such as file size, permissions, and timestamps)
f – Allows you to specify a filename
Extract a TAR.GZ Archive
To extract the contents of a .tar.gz archive, you have two options depending on where you want the files to go.
Extract into the current folder
- Locate the .tar.gz file you would like to extract. In this example, we’ll extract /root/archive.tar.gz into the current working folder.
- Execute the following command, replacing the path with the location of your archive:
tar -xzvf /root/archive.tar.gzExtract into a specific folder
- In this example, we’ll extract /root/archive.tar.gz into a folder of your choice.
- Run the following command, replacing /path/to/destination with the path to your desired location:
tar -xzvf /root/archive.tar.gz -C /path/to/destinationx – Extracts files from the archive
z – Decompresses using Gzip
v – Verbose (shows progress)
f – Specifies the archive filename
C – Changes to the specified folder before extracting
Compress Files with Gzip
- Locate the file you want to compress. In this example, we are compressing /root/file.txt into a file named file.txt.gz
- Execute the command below, replacing file.txt with the name of your file.
gzip /root/file.txt-k – Keep the original file
(no flag) – By default, gzip replaces the original file with the compressed version
View a Gzip file’s contents without extracting
- Locate the compressed file you would like to view. In the example below we will be viewing a file called “file.txt.gz in the /root folder.
- Run the following command making sure you replace file.txt.gz with your file
zcat /root/file.txt.gzExtract a Gzip File
- Locate your compressed file. Make sure you know the full path to the Gzip file you want to decompress (for example, /root/file.txt.gz).
- Run one of the following commands to decompress it:
Option A – Using gunzip:
gunzip /root/file.txt.gzOption B – Using gzip with the -d flag:
gzip -d /root/file.txt.gz-d – Decompresses the file
gunzip – Equivalent to gzip -d
Compress Files with ZIP
- Locate the files or folders you want to compress, Make sure you know the path(s) to the file(s) or folder(s) you want to include in the archive. For example, /root/my_folder or /root/file.txt.
- In this example we will create a zip in /root containing the contents of “/root/my_folder and /root/file.txt”
zip -r /root/archive.zip /root/my_folder /root/file.txt-r – Recursively adds folders and their contents
archive.zip – The name of the resulting ZIP file
files/folders – Specify which files or folders to include
View a Zip file’s contents without extracting
- Locate the ZIP file, Make sure you know the path to the archive. In our example we will be using /root/archive.zip.
- Running the command below will display all files and folders contained in the archive.
unzip -l /root/archive.zip-l – Lists archive contents without extracting
Extract a ZIP Archive
To extract the contents of a .zip file, you have two options depending on where you want the files to go.
Current folder
- Ensure you know the path to the ZIP archive you want to extract. In the example below we will be extracting /root/archive.zip into the folder we are in.
- Run the command below to extract the zip file making sure to replace the file name.
unzip /root/archive.zipExtract into a specific folder
- Ensure you know the path to the ZIP archive you want to extract. In the example below we will be extracting /root/archive.zip to a specific location.
- Run the zip command making sure to include the destination followed by the target.
unzip /root/archive.zip -d /path/to/destination-d – Specifies the folder where files will be extracted
Troubleshooting & FAQs
Q: I ran gzip on a folder, but nothing happened. Why?
A: Gzip only works on individual files, not folders. Use tar first to bundle files, then compress with gzip.
Q: What’s the difference between .tar.gz and .tgz?
A: They’re the same format—.tgz is just a shorter file extension.
Q: I don’t want verbose output when creating archives. What should I do?
A: Omit the v flag:
tar -czf archive.tar.gz my_folderRelated Articles
- Learn more about File Compression Algorithms.
- Learn how to free up more space by following the guidance found: Managing disk space in Linux
- Unable to run these commands due to permissions issues? Learn more about Linux file permissions here: Understanding File Permissions in Linux