gzip
: Compressing Files on Linux
Summary
gzip
is a command-line utility used for compressing files to reduce their storage size. It uses the Lempel-Ziv coding (LZ77) algorithm. It's a quick and easy way to create compressed archives, saving disk space and bandwidth when transferring files.
Introduction
The gzip
command is a standard tool in most Linux distributions for compressing files. When a file is compressed using gzip
, the original file is replaced by a compressed version with the .gz
extension. gzip
is particularly useful for archiving files that are not frequently accessed or for reducing the size of files for faster transfer over a network. To uncompress files compressed with gzip, you would use the gunzip
command, or the gzip -d
option.
Use Case and Examples
Compressing a Single File
This command compressesmyfile.txt
, replacing it with myfile.txt.gz
. Compressing Multiple Files
This compressesfile1.txt
, file2.log
, and file3.pdf
individually, creating file1.txt.gz
, file2.log.gz
, and file3.pdf.gz
. Keeping the Original File
This command compressesmyfile.txt
and redirects the output to myfile.txt.gz
, effectively creating a compressed copy without deleting the original until rm
is used. While -c
allows you to redirect the output, you need to manually remove the original file, if desired. Alternatively, the -k
flag achieves the same result on systems where it's supported (see flag details below). Compressing Recursively (Directories)
This command recursively compresses all files within themydirectory
. Each individual file in the directory will be compressed. The directory structure itself is not archived (use tar
in conjunction with gzip
for that, often achieved with the tar -czvf
option). Decompressing a File
This decompressesmyfile.txt.gz
, replacing it with the original myfile.txt
. You can also use gunzip myfile.txt.gz
for the same effect. Specifying Compression Level
This compressesmyfile.txt
using the highest compression level (9). Higher levels take longer but result in smaller file sizes. The default is level 6. Commonly used flags
Flag | Description | Example |
---|---|---|
-d or --decompress | Decompress the specified file(s). | gzip -d myfile.txt.gz |
-r or --recursive | Recursively compress or decompress files within directories. | gzip -r mydirectory |
-v or --verbose | Display compression ratio and other information. | gzip -v myfile.txt |
-l or --list | List compressed file contents. Provides information such as compressed size, uncompressed size, and ratio. | gzip -l myfile.txt.gz |
-c or --stdout or --to-stdout | Write output on standard output; keep original files. | gzip -c myfile.txt > myfile.txt.gz |
-k or --keep | Keep (don't delete) input files during compression or decompression. Note: Not supported on all systems; check man gzip . | gzip -k myfile.txt |
-[1-9] | Compression level, where 1 is fastest compression (less compression) and 9 is best compression (slowest). Default is 6. | gzip -1 myfile.txt , gzip -9 myfile.txt |
-f or --force | Force compression even if the file already has a .gz extension or if there are hard links. | gzip -f myfile.txt.gz |
-t or --test | Test compressed file. | gzip -t myfile.txt.gz |