Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Gzip Command in Linux - DesignLinux

Oct 15 2020
designlinux 0 Comments
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.

Related

Tags: gzip, terminal

How to Share a Local Folder with a Remote Host Running on VMWare

Prev Post

How to Backup and Restore a PostgreSQL Database

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 1
  • 491
  • 612,670

DesignLinux.com © All rights reserved

Go to mobile version