How to gzip a tar file
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
- The `tar` command is used to combine multiple files into a single archive.
- The `gzip` command is a popular compression utility that reduces file size.
- The `-z` option in `tar` tells it to use gzip compression.
- A file ending in `.tar.gz` or `.tgz` indicates a gzipped tar archive.
- Uncompressing a `.tar.gz` file usually involves `tar -xzvf archive.tar.gz`.
Overview
Archiving and compressing files is a common task for managing data, whether for backups, transferring large amounts of information, or saving disk space. In Unix-like operating systems (Linux, macOS), the combination of the `tar` and `gzip` utilities is frequently used for this purpose. `tar` (Tape ARchiver) is a utility that bundles multiple files and directories into a single archive file, often referred to as a tarball. `gzip` is a widely used compression program that reduces the size of files. When used together, they create a `.tar.gz` (or `.tgz`) file, which is both an archive and a compressed file.
What is Tar?
The `tar` command's primary function is to create an archive from a set of files and directories. It doesn't compress the files by itself; it simply concatenates them into one file. This archive file can then be processed by other utilities, such as `gzip`, for compression. The original purpose of `tar` was to write archives to magnetic tape, hence the name. Modern usage involves writing to disk files.
What is Gzip?
`gzip` is a command-line utility for file compression. It works by replacing redundant portions of data with shorter representations. It is known for its efficiency and widespread availability on Unix-like systems. `gzip` typically achieves compression ratios of around 60-70% for text files, though this varies greatly depending on the file type and its content. `gzip` can compress single files, creating a new file with a `.gz` extension. It does not archive multiple files on its own.
How to Gzip a Tar File
There are two primary methods to create a gzipped tar archive:
Method 1: Using `tar` with the `-z` option
This is the most common and recommended method. The `tar` command has built-in support for invoking `gzip` during the archiving process. The syntax is as follows:
tar -czvf archive_name.tar.gz /path/to/directory_or_filesLet's break down the options:
-c: Create a new archive.-z: Filter the archive through gzip. This is the key option for gzipping.-v: Verbosely list the files processed. This shows you the progress as files are added to the archive.-f: Use archive file or device. This option must be followed by the name of the archive file you want to create (e.g., `archive_name.tar.gz`).
For example, to create a gzipped archive named `my_project_backup.tar.gz` containing the contents of the `my_project` directory, you would run:
tar -czvf my_project_backup.tar.gz my_project/If you want to archive multiple files or directories, you can list them after the source directory:
tar -czvf my_files.tar.gz file1.txt directory1/ file2.logMethod 2: Using `tar` and `gzip` separately
You can first create a standard tar archive and then compress it using `gzip`.
- Create the tar archive:
tar -cvf archive_name.tar /path/to/directory_or_filesThis creates an uncompressed archive named `archive_name.tar`.
- Compress the tar archive with gzip:
gzip archive_name.tarThis command will compress `archive_name.tar` and replace it with `archive_name.tar.gz`. The original `.tar` file is deleted by default.
While this method works, it is less efficient and requires two steps compared to the integrated `-z` option in `tar`.
How to Uncompress a Gzipped Tar File
To extract the files from a `.tar.gz` archive, you use the `tar` command again, but with different options:
tar -xzvf archive_name.tar.gzThe options here are:
-x: Extract files from an archive.-z: Filter the archive through gzip (necessary because it's a gzipped archive).-v: Verbosely list the files processed.-f: Specify the archive file name.
For example, to extract `my_project_backup.tar.gz` into the current directory:
tar -xzvf my_project_backup.tar.gzIf you want to extract the archive to a specific directory, you can use the -C option (note the uppercase C):
tar -xzvf archive_name.tar.gz -C /path/to/destination_directoryOther Compression Methods
It's worth noting that `gzip` is not the only compression method available. Other popular options include:
- `bzip2`: Often achieves better compression ratios than `gzip` but is slower. Archives are typically named `.tar.bz2` or `.tbz2`. The `tar` command uses the `-j` option for `bzip2` compression (e.g., `tar -cjvf archive.tar.bz2 directory/`).
- `xz`: Generally provides the best compression ratios among these options, but can be computationally intensive and slow. Archives are typically named `.tar.xz` or `.txz`. The `tar` command uses the `-J` option for `xz` compression (e.g., `tar -cJvf archive.tar.xz directory/`).
The choice of compression method often depends on the trade-off between compression ratio, speed, and compatibility with different systems.
Conclusion
Gzipping a tar file is a fundamental operation for managing data on Unix-like systems. Using the `tar -czvf` command provides a straightforward and efficient way to create `.tar.gz` archives, while `tar -xzvf` allows for easy extraction. Understanding these commands is essential for anyone working with backups, software distribution, or large datasets.
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 pagefair-use
- Tar (computing) - WikipediaCC-BY-SA-4.0
Missing an answer?
Suggest a question and we'll generate an answer for it.