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
Key Facts
- A directory cannot be directly gzipped; it must first be archived into a single file.
- The `tar` command is commonly used to create archives (e.g., `.tar` files).
- The `gzip` command is used to compress files, often a `.tar` archive, creating a `.tar.gz` or `.tgz` file.
- The combined command `tar -czvf archive_name.tar.gz directory_to_compress` is a common shortcut.
- On Windows, third-party tools like 7-Zip or WinRAR are generally used for this purpose.
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-c: Create a new archive.-v: Verbose mode, showing the files being added.-f: Specifies the archive filename.
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.tarThis 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-c: Create a new archive.-z: Filter the archive through gzip (compress).-v: Verbose mode.-f: Specifies the archive filename.
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-x: Extract files from an archive.-z: Filter the archive through gzip (decompress).-v: Verbose mode.-f: Specifies the archive filename.
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.
More How To in Daily Life
Also in Daily Life
More "How To" Questions
Trending on WhatAnswers
Browse by Topic
Browse by Question Type
Sources
- GNU tar ManualGPL-3.0-or-later
- gzip(1) - Linux man pageCC0-1.0
- 7-ZipLGPL-3.0
Missing an answer?
Suggest a question and we'll generate an answer for it.