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 file1
, file2
, file3
, 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 *