Help Docs Server Administration Linux Server Administration File and Directory Management in Linux Use Tar, Gzip & Zip in Linux

Use Tar, Gzip & Zip in Linux

Learn how to decompress `.gz` and `.tar.gz` files in Linux. Our guide also covers the key differences between `TAR`, `GZ`, `TGZ`, and `ZIP` file formats.

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

  1. 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.
  2. Run the tar command making sure to include the destination followed by the target:
tar -czvf /root/archive.tar.gz  /root/my_folder
Tip – Tar flags

c – 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

  1. 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. 
  2. Run the following command, making sure to specify the correct path to your archive:
tar -tvf /root/archive.tar.gz
Tip – Tar flags

t – 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

  1. 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. 
  2. Execute the following command, replacing the path with the location of your archive:
tar -xzvf /root/archive.tar.gz

Extract into a specific folder

  1. In this example, we’ll extract /root/archive.tar.gz into a folder of your choice.
  2. Run the following command, replacing /path/to/destination with the path to your desired location:
tar -xzvf /root/archive.tar.gz -C /path/to/destination
Tip – Tar flags

x – 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

  1. Locate the file you want to compress. In this example, we are compressing /root/file.txt into a file named file.txt.gz
  2. Execute the command below, replacing file.txt with the name of your file.
gzip /root/file.txt
Tip – Tar flags

-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

  1. 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.
  2. Run the following command making sure you replace file.txt.gz with your file
zcat /root/file.txt.gz

Extract a Gzip File

  1. 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).
  2. Run one of the following commands to decompress it:

Option A – Using gunzip:

gunzip /root/file.txt.gz

Option B – Using gzip with the -d flag:

gzip -d /root/file.txt.gz
Tip – Tar flags

-d – Decompresses the file

gunzip – Equivalent to gzip -d

Compress Files with ZIP

  1. 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.
  2. 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
Tip – Tar flags

-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

  1. Locate the ZIP file, Make sure you know the path to the archive. In our example we will be using  /root/archive.zip.
  2.  Running the command below will display all files and folders contained in the archive.
unzip -l /root/archive.zip
Tip – Tar flags

-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

  1. 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.
  2. Run the command below to extract the zip file making sure to replace the file name.
unzip /root/archive.zip

Extract into a specific folder

  1. 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.
  2. Run the zip command making sure to include the destination followed by the target.
unzip /root/archive.zip -d /path/to/destination
Tip – Tar flags

-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_folder
Was this article helpful?