• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

Gzip Command in Linux

Written by Admin, Updated On October 11, 2020
gzip, terminal
Gzip Command in Linux

The Gzip is the popular compression algorithm to reduce the size of a file with keeping file in original mode, ownership and timestamp. It also known as the .gz file format. The gzip utility is used to compress and decompress the files. In this guide, we will show you how to use the gzip command.

gzip Command Syntax#

The basic syntax for the gzip command is as follows:

gzip [OPTION]... [FILE]...

Using gzip, you can compress single file and generate compressed file for each file. The compressed file will be end with the either .gz or .z extension.

If your requirement is to compress multiple files or folders in to one file, you should first create a Tar archive file and need you can compress the .tar file with Gzip.

Usually, the Gzip is used to compress the text files, Tar archives, and web pages. It’s useless to use gzip for compress images, audio, PDF documents, and other binary files as they are already compressed.

The regular files can be compressed by gzip and the symbolic links are ignored.

Compress Files with gzip#

You can compress a single file using the gzip command followed by the filename:

gzip filename

It will delete the original file and creates a filename.gz file. Gzip keeps the original file timestamp, mode, ownership, and name in the compressed file.

Keep Original File#

If you would like to keep the original file instead of delete after compression, use -k option with gzip command:

gzip -k filename

Verbose output#

To see the percentage of reduction and the names of the files that are being processed, use the -v option:

gzip -v filename
filename: 9.5% -- replaced with filename.gz

Compress multiple files#

To compress the multiple files, pass multiple file names as arguments. For example, to compress the files named filename1, filename2, filename3, you would run the following command:

gzip filename1 filename2 filename3

As an output the above command will create the three different files named with filename1.gz, filename2.gz, filename3.gz .

Compress all files in a directory#

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

gzip -r directory

gzip will recursively traverse through the whole directory structure and compress all the files in the directory and it’s subdirectories.

Change compression level#

You can specify the range if compression levels from 1 to 9. For fastest compression with minimal compression ratio, you can pass -1 or --fast option. For the maximum compression ratio and slowest compression you should use the -9 or --best option with the gzip command. By default, the compression level is -6.

For example, for maximum compression, type:

gzip -9 filename

Compression is a CPU-intensive task, the higher the compression level, the longer the process takes.

Using standard input#

You can create a .gz file from the standard input, just pipe the output if the command to gzip. For example, to generate a Gzip MySQL database backup file, you would run:

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

The gzip will consider input which is the output of the mysqldump command.

Decompressing Files with gzip#

Use the -d option, to decompress a .gz file:

gzip -d filename.gz

You can use the gunzip command to decompress a Gzip file. This command is basically an alias of gzip -d:

Keep the compressed file#

Same as 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

Decompress multiple files#

You can decompress multiple files in single command by passing the filenames as arguments:

gzip -d filename1.gz filename2.gz filename3.gz

Decompress all files in a directory#

Use -d and -r options to decompress all files in a given directory recursively:

gzip -dr directory

Conclusion#

Using Gzip you can reduce the size of a given file. The gzip command allows you to compress and decompress files. To learn more about the gzip command, visit Gnu gzip documentation page.

If you have any questions or feedback, leave a comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Create and Select MySQL Database
Next Article   Bash For Loop

Related Posts

  • How to Use sed to Find and Replace String in Files

    How to Use sed to Find and Replace String in Files

    December 18, 2020
  • Linux Head Command

    Linux Head Command

    December 16, 2020
  • Rename Files and Directories in Linux

    How to Rename Files and Directories in Linux

    December 15, 2020

Leave a Reply Cancel reply

Popular Posts

  • How to Install Python 3.9 on Debian 10
    How to Install Python 3.9 on Debian 10 December 25, 2020
  • How to Install GIMP on Ubuntu 20.04
    How to Install GIMP 2.10 on Ubuntu 20.04 December 22, 2020
  • How to Install Notepad++ on Debian 10
    How to Install Notepad++ on Debian 10 December 23, 2020
  • How to Install Php 8 on Debian 10
    How to Install PHP 8 on Debian 10 January 2, 2021
  • How to Install Php 8 on Ubuntu 20.04
    How to Install PHP 8 on Ubuntu 20.04 December 28, 2020
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook