How to gzip a directory

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 directory, you first need to archive it into a single file using a tool like `tar`, and then compress that archive file using the `gzip` command. This is typically done on Linux and macOS systems via the terminal.

Key Facts

Overview

Gzipping, or more accurately, creating a gzipped archive of a directory, is a common task for reducing file size for storage or transfer. Since the `gzip` utility is designed to compress single files, you cannot directly apply it to a directory. Therefore, the process involves two main steps: first, archiving the directory into a single file, and second, compressing that archive file using `gzip`. This is most frequently performed on Unix-like operating systems (Linux, macOS) using command-line tools.

Archiving with `tar`

The `tar` (tape archive) utility is the standard tool for bundling multiple files and directories into a single archive file. It preserves file permissions, ownership, and directory structure. When you create a tar archive, it doesn't inherently compress the data; it simply combines everything into one file, often with a `.tar` extension.

To create a tar archive of a directory named my_directory and name the archive my_archive.tar, you would use the following command in your terminal:

tar -cvf my_archive.tar my_directory

Compressing with `gzip`

Once you have a `.tar` archive, you can compress it using the `gzip` command. This utility uses the DEFLATE compression algorithm to reduce the file size. The result is typically a file with a .gz extension appended to the original filename (e.g., my_archive.tar.gz).

To compress the my_archive.tar file, you would run:

gzip my_archive.tar

This command replaces the original my_archive.tar file with the compressed my_archive.tar.gz file.

Combining Archiving and Compression (`tar -czvf`)

Most modern versions of `tar` have built-in support for `gzip` compression. This allows you to perform both archiving and compression in a single step, which is the most common and efficient method. The command combines the options for creating an archive and specifying gzip compression.

To create a gzipped tar archive of my_directory named my_archive.tar.gz directly, use:

tar -czvf my_archive.tar.gz my_directory

This single command achieves the same result as creating a `.tar` file and then compressing it with `gzip`, but it's faster and requires fewer steps.

Decompressing and Extracting

To reverse the process and extract the contents of a gzipped tar archive (e.g., my_archive.tar.gz), you can use the `tar` command with different options:

tar -xzvf my_archive.tar.gz

This command will decompress the archive and extract its contents into the current directory.

Alternatives and Considerations

While `tar` and `gzip` are standard on Linux and macOS, Windows users typically rely on third-party archiving tools. Popular options include 7-Zip, WinRAR, and built-in Windows utilities that support various compression formats, including `.zip` (which can be created from directories natively) and `.tar.gz`.

For very large directories or when dealing with specific performance requirements, other compression tools like `bzip2` (often resulting in `.tar.bz2` files) or `xz` (resulting in `.tar.xz` files) might offer better compression ratios, though they might be slower. The basic principle of archiving first, then compressing, remains the same.

Sources

  1. GNU tar ManualGPL-3.0-or-later
  2. gzip(1) - Linux man pageCC0-1.0
  3. 7-ZipLGPL-3.0

Missing an answer?

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