How to gzip a file 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 `gzip` command reduces file size by about 40-70%.
- The original file is deleted by default after compression.
- The `gunzip` command is used to decompress `.gz` files.
- You can compress multiple files at once by listing them or using wildcards.
- The `gzip -r` option recursively compresses files in a directory.
Overview
Compressing files is a common practice in computing for several reasons, including saving storage space and reducing the time it takes to transfer files over a network. In the Linux operating system, the gzip utility is a widely used and efficient tool for this purpose. It employs the DEFLATE algorithm, which is a combination of the LZ77 algorithm and Huffman coding, to achieve significant file size reduction.
What is Gzip?
gzip stands for GNU zip. It is a free software utility that compresses and decompresses files. It's a command-line tool, meaning you interact with it by typing commands into the terminal. gzip is a staple in Unix-like operating systems, including Linux and macOS, and is often used in conjunction with the tar utility for archiving multiple files before compression (creating .tar.gz or .tgz files).
How to Gzip a File
The most basic way to compress a file using gzip is to simply type the command followed by the name of the file you want to compress.
Basic Compression
To compress a file named mydocument.txt, you would open your terminal and type:
gzip mydocument.txtAfter running this command, the original file mydocument.txt will be removed, and a new file named mydocument.txt.gz will be created. This new file is the compressed version.
Keeping the Original File
Sometimes, you might want to keep the original file after compression. You can achieve this using the -k or --keep option:
gzip -k mydocument.txtThis command will create mydocument.txt.gz but leave mydocument.txt intact.
Compressing Multiple Files
You can compress multiple files at once by listing them after the gzip command:
gzip file1.txt file2.log file3.csvAlternatively, you can use wildcards to compress files that match a pattern. For instance, to compress all files ending with .txt in the current directory:
gzip *.txtIn both cases, each specified file will be compressed individually, and the original files will be deleted by default.
Compressing Recursively (Directories)
If you need to compress all files within a directory and its subdirectories, you can use the -r or --recursive option:
gzip -r my_directoryThis command will go through my_directory and all its subdirectories, compressing every file it finds. Note that this will create compressed versions of files inside the directory structure, not compress the directory itself into a single archive file.
Controlling Compression Level
gzip allows you to specify the compression level, ranging from 1 (fastest compression, least compression) to 9 (slowest compression, best compression). The default level is 6.
- To use the fastest compression:
gzip -1 mydocument.txt - To use the best compression:
gzip -9 mydocument.txt
Higher compression levels take more CPU time and memory but result in smaller files.
How to Decompress Gzip Files
To decompress a file compressed with gzip (a file ending in .gz), you use the gunzip command or the gzip -d option.
Using gunzip
To decompress mydocument.txt.gz:
gunzip mydocument.txt.gzThis will remove the .gz file and restore the original mydocument.txt.
Using gzip -d
The -d or --decompress option with gzip performs the same function:
gzip -d mydocument.txt.gzLike gunzip, this command also deletes the .gz file and recreates the original.
Keeping the Compressed File After Decompression
Similar to compression, you can keep the compressed file after decompressing using the -k option with gzip -d:
gzip -dk mydocument.txt.gzCommon Use Cases
- Saving Disk Space: Compress large log files, archives, or old data that you need to keep but don't access frequently.
- Faster Transfers: Compress files before uploading or downloading them to save bandwidth and time.
- Log Rotation: System administrators often configure log rotation tools to compress old log files automatically using
gzip. - Software Distribution: While less common now for large distributions (which often use
tar.gzor other formats), individual configuration files or small data sets might be distributed compressed.
Difference Between gzip and tar
It's important to distinguish gzip from tar (Tape Archiver). tar is used to bundle multiple files and directories into a single archive file (often called a tarball), but it does not compress them. gzip, on the other hand, compresses a single file. They are often used together: you first use tar to create a single archive, and then you use gzip to compress that archive. This results in a .tar.gz or .tgz file, which is a common format for distributing software and data on Linux.
Example of creating and compressing a tarball:
# Create a tarball named archive.tar from all .txt filestar -cvf archive.tar *.txt# Compress archive.tar using gzipgzip archive.tar# This creates archive.tar.gzAlternatively, you can use the -z option with tar to perform both archiving and gzip compression in one step:
tar -czvf archive.tar.gz *.txtConclusion
The gzip command is a fundamental tool for file compression in Linux, offering a simple yet effective way to reduce file sizes. Understanding its basic usage, options like keeping original files, recursive compression, and its relationship with tar will significantly enhance your command-line efficiency.
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
- gzip(1) - Linux man pageCC0-1.0
- Gzip - GNU Project - Free Software FoundationGPL-3.0-or-later
- Gzip - WikipediaCC-BY-SA-4.0
Missing an answer?
Suggest a question and we'll generate an answer for it.