gzip's Examples

To compress a single file invoke the gzip command followed by the filename

gzip filename

If you want to keep the input (original) file, use the -k option

gzip -k filename

Another option to keep the original file is to use the -c option which tells gzip to write on standard output and redirect the output to a file.

gzip -c filename > filename.gz

Use the -v option if you want to see the percentage reduction and the names of the files that are being processed

gzip -v filename

 

You can also pass multiple files as arguments to the command. For example, to compress the files named file1file2file3, you would run the following command

gzip file1 file2 file3

To compress all files in a given directory, use the -r option

gzip -r directory

gzip allows you to specify a range of compression levels, from 1 to 9. -1 or --fast means fastest compression speed with minimal compression ratio, -9 or --best indicates the slowest compression speed with maximum compression ratio. The default compression level is -6.

gzip -9 filename

To create a .gz file from the stdin, pipe the output of the command to gzip. For example, to create a Gzipped MySQL database backup you would run

mysqldump database_name | gzip -c > database_name.sql.gz

Another command that you can use to decompress a Gzip file is gunzip . This command is basically an alias to gzip -d.

gzip -d filename.gz

when compressing a file, the -k option tells gzip to keep the input file, in this case, that is the compressed file

gzip -dk filename.gz

To decompress multiple files at once pass the filenames to gzip as arguments

gzip -d file1.gz file2.gz file3.gz

 

When used with -d and -r options, gzip decompresses all files in a given directory recursively

gzip -dr directory

When used with the -l option, gzip shows statistics about the given compressed files

gzip -l filename

To get more information, add the -v option

gzip -lv filename

When a compressed file contains several individual files, the uncompressed size and CRC reported by the --list option are for the last member only. To get the uncompressed size for all members, useWhen a compressed file contains several individual files, the uncompressed size and CRC reported by the --list option are for the last member only. To get the uncompressed size for all members, use

gzip -cd file.gz

-f option : Sometimes a file cannot be compressed. Perhaps you are trying to compress a file called “myfile1” but there is already a file called “myfile1.gz”.

$ gzip -f myfile1.txt

compress the following file

gzip numbers.txt

to decompress the file using gzip command by giving -d option

gzip -d nnumbers.txt.gz

force a file to be compressed

gzip -f numbers.txt

to keep the uncompressed file

gzip -k numbers.txt

compress every file in a folder and subfolders

gzip -r/tmp numbers.txt

to test the validity of a compressed file

gzip -t/rmp colors.txt

to change the compression level. to get the minimum compression at the fastest speed

gzip -1 numbers.txt

to set the compresion level.to get the maximum compression at slowest speed

gzip -9 numbers.txt

lists all the compressed file contents

gzip -l

compress all files recursively

gzip ls -r *

Explanation:
Compresses file.txt to file.txt.gz and removes the original file.

gzip file.txt

Explanation:
Each file is compressed separately into file1.txt.gz, file2.txt.gz, and file3.txt.gz.

gzip file1.txt file2.txt file3.txt

Explanation:
Compresses all .txt files in the current directory.

gzip *.txt

Explanation:
Compresses file.txt to file.txt.gz but keeps the original file.

gzip -k file.txt

Explanation:
Uses the highest level (-9) to maximize compression.

gzip -9 file.txt

Explanation:
Uses the lowest level (-1) for fast compression (less compression).

gzip -1 file.txt

Output Example:

  compressed   uncompressed  ratio uncompressed_name
       12345         67890   81%   file.txt
Explanation:
Shows compression ratio and file details.

gzip -l file.txt.gz

Explanation:
Outputs compressed data to the terminal without saving it.

gzip -c file.txt
or
cat file.txt | gzip

Explanation:
Finds all files in /path/to/directory and compresses them.

find /path/to/directory -type f -exec gzip {} \;

Explanation:

  • tar archives foldername/ into archive.tar.gz in one step.
  • -z uses gzip for compression.

tar -cvzf archive.tar.gz foldername/

Compresses files to save space using GNU zip compression.

gzip file.txt

Decompresses the file.

gzip -d file.txt.gz

DevOpsSchool
Typically replies within an hour

DevOpsSchool
Hi there 👋

How can I help you?
×
Chat with Us