Skip to content

What is gzip Linux command?

The gzip command in Linux is used to compress and decompress files efficiently. It is commonly used to reduce file size and save disk space.

gzip Syntax:

Terminal window
gzip [option] [file]

gzip Options:

OptionDescription
-cWrite output on standard output; keep original files unchanged.
-dDecompress; when this option is used, gzip will try to decompress the specified file(s).
-fForce compression or decompression even if the file has multiple links or the corresponding file already exists.
-h, —helpDisplay a help message and exit.
-kKeep the original file (don’t delete it).
-lList the compressed file metadata only.
-qQuiet mode; suppress non-essential output.
-tTest the integrity of the specified file(s).
-vVerbose mode; show the compression ratio for each file processed.
-1 to -9Set the compression level (1 being the fastest and 9 being the best compression ratio).

Parameters:

ParameterDescription
fileThe file(s) to be compressed or decompressed. Wildcards can also be used to specify multiple files to process at once.

gzip Command Usage Examples:

Compress a File

Terminal window
gzip file.txt

Compresses a file named “file.txt” using gzip.

Compress Multiple Files Simultaneously

Terminal window
gzip file1.txt file2.txt file3.txt

Compresses multiple files (“file1.txt”, “file2.txt”, “file3.txt”) simultaneously using gzip.

Compress and Keep Original File

Terminal window
gzip -c file.txt > file.txt.gz

Compresses a file named “file.txt” using gzip and keeps the original file while creating a compressed file “file.txt.gz”.

Decompress a File

Terminal window
gzip -d file.txt.gz

Decompresses a file “file.txt.gz” that was previously compressed using gzip.

Compress a Directory Recursively

Terminal window
tar -czf directory.tar.gz directory/

Compresses a directory “directory” recursively using “tar” and then compresses the tar file using gzip to create “directory.tar.gz”.

How do I use gzip in Linux?

To use the gzip command in bash, execute the following command:

Terminal window
gzip file.txt

How do I compress a file and keep the original using gzip?

To compress a file with gzip and keep the original, you can use the -k or —keep option. Here’s an example:

Terminal window
gzip -k file.txt

How do I decompress a file using gzip?

To decompress a file with gzip, you can use the -d or —decompress option. Here’s an example:

Terminal window
gzip -d file.txt.gz

How do I list the contents of a gzip file without decompressing it?

To list the contents of a gzip file without decompressing it, you can use the -l or —list option. Here’s an example:

Terminal window
gzip -l file.txt.gz

How do I force gzip to compress files, even if they are already compressed?

To force gzip to compress files, even if they are already compressed, you can use the -f or —force option. Here’s an example:

Terminal window
gzip -f file.txt

How do I set the compression level when using gzip?

To set the compression level when using gzip, you can use the -[1-9] option to specify the level (1 being the fastest and 9 being the best compression). Here’s an example:

Terminal window
gzip -9 file.txt

How do I compress multiple files using gzip?

To compress multiple files using gzip, you can list all the files as arguments. Here’s an example:

Terminal window
gzip file1.txt file2.txt file3.txt

How do I compress a directory and its contents using gzip?

To compress a directory and its contents using gzip, you can use the -r or —recursive option. Here’s an example:

Terminal window
gzip -r directory/

Applications of the gzip command

  • Compressing files to reduce storage space
  • Combining with tar to create compressed archive files
  • Fast file compression and decompression