How to gzip a folder in linux

Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.

Last updated: April 4, 2026

Quick Answer: To gzip a folder in Linux, you first need to archive it into a single file using the `tar` command, and then compress that archive file using the `gzip` command. This is typically done in a single step using the `tar` command with the `-z` option.

Key Facts

Overview

Compressing folders in Linux is a common task for saving disk space, facilitating file transfers, and creating backups. While individual files can be directly compressed with utilities like `gzip`, directories (folders) require a two-step process: first, archiving the folder into a single file, and then compressing that archive. Fortunately, the Linux command line provides a streamlined way to achieve this using the `tar` command, which can handle both archiving and compression in one go.

Understanding the Tools: `tar` and `gzip`

Before diving into the command, it's helpful to understand the primary tools involved:

Combining `tar` and `gzip`

The most efficient way to gzip a folder in Linux is to use the `tar` command with its built-in support for `gzip` compression. This is achieved by using the `-z` option along with other standard `tar` flags.

The Standard Command

The most common command to create a gzipped tar archive of a folder is:

tar -czvf archive_name.tar.gz /path/to/your/folder

Let's break down the options:

Step-by-Step Example

Suppose you have a folder named `my_project` located in your home directory (`~`), and you want to create a compressed archive named `my_project_backup.tar.gz` in your current directory.

  1. Navigate to the parent directory (or wherever you want the archive to be created). For instance, if `my_project` is in `/home/user/documents`, and you want the backup in `/home/user/backups`:
    cd /home/user/backups
  2. Execute the `tar` command:
    tar -czvf my_project_backup.tar.gz /home/user/documents/my_project

After the command completes, you will find the `my_project_backup.tar.gz` file in your current directory. This single file contains all the contents of the `my_project` folder, compressed using gzip.

Extracting a Gzipped Tar Archive

To extract the contents of a `.tar.gz` file, you use the `tar` command again, but this time with different options:

tar -xzvf archive_name.tar.gz

If you want to extract the archive to a specific directory, you can use the `-C` option:

tar -xzvf archive_name.tar.gz -C /path/to/destination/directory

Alternative Compression Methods

While `gzip` is very common, Linux also supports other compression algorithms that might offer better compression ratios, albeit often at the cost of slower compression/decompression speeds:

Best Practices

By mastering the `tar -czvf` command, you can efficiently manage and compress your folders in Linux environments.

Sources

  1. GNU Tar ManualGPL-3.0-or-later
  2. gzip(1) - Linux man pageCC0-1.0
  3. Gzip - WikipediaCC-BY-SA-4.0

Missing an answer?

Suggest a question and we'll generate an answer for it.