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
Key Facts
- The `tar` command is used to create archives, bundling multiple files and directories into a single file.
- The `gzip` command is a compression utility that reduces the size of files.
- The `-z` option in `tar` tells it to compress the archive using gzip.
- The common command structure is `tar -czvf archive_name.tar.gz /path/to/folder`.
- The `c` flag means create, `z` means gzip, `v` means verbose (show files being processed), and `f` specifies the archive filename.
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:
- `tar` (Tape Archiver): This command is used to bundle multiple files and directories into a single archive file, often referred to as a "tarball." Historically, `tar` was used to write archives to magnetic tape, but it's now commonly used to create `.tar` files on disk. It doesn't inherently compress files; it just groups them.
- `gzip` (GNU Zip): This is a popular compression utility that reduces the size of files. When `gzip` compresses a file, it typically creates a new file with a `.gz` extension and removes the original. For example, `file.txt` compressed with `gzip` becomes `file.txt.gz`.
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/folderLet's break down the options:
- `-c` (create): This tells `tar` to create a new archive.
- `-z` (gzip): This is the crucial option that instructs `tar` to compress the archive using `gzip`. If you wanted to use a different compression method like `bzip2` or `xz`, you would use `-j` or `-J` respectively.
- `-v` (verbose): This option makes `tar` list the files and directories as it processes them. It's useful for seeing the progress, especially with large folders, but can be omitted if you prefer a quieter operation.
- `-f` (file): This option specifies the name of the archive file that will be created. It must be followed immediately by the desired filename (e.g., `archive_name.tar.gz`).
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.
- 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 - 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- `-x` (extract): Tells `tar` to extract files from an archive.
- `-z` (gzip): Tells `tar` to decompress the archive using `gzip` before extracting.
- `-v` (verbose): Lists the files as they are extracted.
- `-f` (file): Specifies the archive filename.
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/directoryAlternative 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:
- `bzip2` (`.tar.bz2`): Use the `-j` option with `tar` (`tar -cjvf archive.tar.bz2 folder/`). Extract with `tar -xjvf archive.tar.bz2`.
- `xz` (`.tar.xz`): Use the `-J` option with `tar` (`tar -cJvf archive.tar.xz folder/`). Extract with `tar -xJvf archive.tar.xz`.
Best Practices
- Naming Conventions: Always use the `.tar.gz` (or `.tgz`) extension for archives compressed with `gzip` to easily identify the format.
- Path Specificity: Be mindful of the path you provide for the folder you are archiving and the location where you save the archive file. Using absolute paths can prevent confusion.
- Verbose vs. Quiet: For scripting or automated tasks, omit the `-v` flag for quieter output. For interactive use, `-v` can be helpful.
- Disk Space: Ensure you have enough free disk space for the compressed archive, which will be smaller than the original folder but still requires space.
By mastering the `tar -czvf` command, you can efficiently manage and compress your folders in Linux environments.
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
- Gzip - WikipediaCC-BY-SA-4.0
Missing an answer?
Suggest a question and we'll generate an answer for it.