gzip is basically a compression program which reduces size of file to create a new file with gz extension. Gzip use Lempel-Ziv (LZ77) algorithm to compress file. When a file is compressed using gzip a new file is created with same name with .gz file extension.
It is worth noting that compression program will not give same result in all applications. Some file are already compressed by default such as jpeg, mp3. These files are better uncompressed you may archive these using tar command but slight or no improvement may be observed.
Example use gzip command
Compress a file using gzip command
To compress a file using gzip command type gzip followed by file name
This will create a file with same file name but with gz extension. Also original file will be removed.
Below command compresses test.txt file and create test.txt.gz
Compress mulitple file using gzip command
To compress multiple files type gz command followed by file names with single space separating file names. Below command compresses multiple files
gzip test1.txt test2.txt test3.txt test4.txt
This command will remove original files and create test1.txt.gz, test2.txt.gz, test3.txt.gz and test4.txt.gz
Get details of how much space saved by compressed file
Using switch -l or –list information can be derived about how much is file size compressed and uncompressed file along with compression ratio.
gzip -l filename.gz
Compress a Directory
Using –r compresses through directory structure recursively. gzip will move into directory and compress all the files present in directory.
gzip -r directoryname
It does not create single file, better results can be achieved using tar command on directory. See it here How to use of tar command in Linux – compressing and archiving files
Change compression level
gzip uses compression level 1 to 9, where 1 is least compressed and 9 uses best compression ratio. It should be kept in mind low compression takes less time to complete than higher compression.
gzip -1 test.txt
The above command will compress file using least compression and command will be executed faster. While command below will compress file using higher compression rate and command will take more time for executing.
gzip -9 test.txt
Decompress a file using gzip command
To compress a file using gzip command type gzip followed by -d and file name
gzip -d test.txt.gz
This command will create test.txt and remove compressed file test.txt.gz