gzip

gzip

The gzip command in Unix and Linux is used for file compression and decompression using the gzip compression algorithm. It is widely used for compressing files to reduce their size for storage, transmission, and backup purposes.

Basic Usage

The basic syntax for the gzip command is:

gzip [options] [file(s)]
  • options: Optional command-line options to control the behavior of gzip.

  • file(s): The name(s) of the file(s) to compress.

Examples

Compressing a File

To compress a file using gzip:

gzip filename.txt

This command compresses filename.txt and creates a compressed file filename.txt.gz. After compression, the original file filename.txt is typically removed unless the -k option is used to keep it.

Decompressing a File

To decompress a .gz file using gzip:

gzip -d filename.txt.gz

This command decompresses filename.txt.gz and restores it to filename.txt.

Decompressing with gunzip

Alternatively, you can use gunzip for decompression, which is equivalent to gzip -d:

gunzip filename.txt.gz

Options

Keep Original File

  • -k: Keep the original file after compression or decompression.

Verbose Output

  • -v: Verbose mode, display compression or decompression statistics.

Force Compression

  • -f: Force compression even if the resulting file is larger than the original.

Specify Compression Level

  • -1 to -9: Specify the compression level (1 for fastest compression, 9 for best compression ratio). The default is -6.

Practical Use Cases

Compressing Multiple Files

To compress multiple files at once:

gzip file1.txt file2.txt

This command compresses file1.txt and file2.txt, resulting in file1.txt.gz and file2.txt.gz.

Keeping Original Files

To keep the original files after compression:

gzip -k file1.txt

This command compresses file1.txt to file1.txt.gz while keeping the original file1.txt.

Summary

The gzip command is a versatile tool for compressing and decompressing files using the gzip compression algorithm in Unix and Linux systems. It offers various options for controlling compression levels, maintaining original files, and providing verbose output. Understanding its usage and options can help you efficiently manage file compression and storage on your system.

help

gzip [options] files

Compress or decompress files (by default, compresses).

Options:

-f, --force    Force compression even if file is already compressed.
-v, --verbose  Print verbose information.
-c, --stdout   Write output on stdout.
-d, --decompress Decompress.
-h, --help     Show this help message.

For more information, see the gzip man page.

Last updated