How to gzip directory 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 directory in Linux, you first need to create a compressed archive of the directory's contents using the `tar` command, and then compress that archive using the `gzip` command. The most common way to do this is with a single command: `tar -czvf archive_name.tar.gz /path/to/directory`.

Key Facts

Overview

Compressing directories in Linux is a common task for saving disk space, facilitating faster file transfers, and creating backups. While `gzip` itself is designed to compress single files, you can effectively compress an entire directory by first archiving it into a single file using the `tar` (tape archive) utility, and then compressing that archive with `gzip`. This process ensures that all files and subdirectories within the original directory are preserved and compressed together.

What is `tar` and `gzip`?

`tar`: This is a powerful command-line utility that bundles multiple files and directories into a single archive file, often referred to as a "tarball". It's widely used in Unix-like systems for creating backups and distributing software. `tar` itself does not perform compression; it merely combines files. However, it can be combined with compression utilities like `gzip`, `bzip2`, or `xz`.

`gzip`: This is a popular compression utility that reduces the size of files. It typically achieves a compression ratio of 2:1 or better, meaning it can reduce a file's size by 50% or more. `gzip` works by replacing redundant data patterns with shorter codes. It's designed to compress single files, creating a new file with a `.gz` extension.

How to Gzip a Directory

The standard and most efficient method to create a gzipped archive of a directory is to use `tar` with the appropriate options. The command combines the archiving and compression steps into one.

Using `tar` with `gzip`

The common command structure is:

tar -czvf archive_name.tar.gz /path/to/directory

Let's break down the options:

Example:

To compress a directory named `my_project` located in your current directory into a file named `my_project_backup.tar.gz`, you would run:

tar -czvf my_project_backup.tar.gz my_project

If `my_project` is located elsewhere, you would provide its full path:

tar -czvf my_project_backup.tar.gz /home/user/documents/my_project

Extracting a Gzipped Tar Archive

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

tar -xzvf archive_name.tar.gz

Example:

To extract `my_project_backup.tar.gz`:

tar -xzvf my_project_backup.tar.gz

This will extract the contents into the current directory. If the original directory structure was preserved in the archive, it will recreate that structure.

Alternative Compression Methods

While `gzip` is very common, Linux offers other compression algorithms that might offer better compression ratios or faster speeds, depending on your needs. You can use `tar` with these as well:

Important Considerations

In summary, while `gzip` is a file compression tool, you leverage the `tar` command with the `-z` option to efficiently create a single, compressed archive of an entire directory in Linux.

Sources

  1. GNU Tar ManualGPL-3.0-or-later
  2. gzip(1) - Linux man pagefair-use
  3. How to Tar and Compress Files in Linuxfair-use

Missing an answer?

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