Top 10 gzip examples in Linux or unix

gzip linux command usagex

Gzip is abbreviated as GUN zip and it is one of the frequent commands used in Linux.

gzip command is used to compress a file for reducing the size of the file.

This will save bandwidth if the file is transferring between different systems. Moreover, the reduced size depends on the content of the file, if the content is text, it will reduce 60% and for the image, it should be 80%.

if we want to copy multiple files, the files should be compressed, so that the bandwidth of the file is reduced.

Gzip command examples

Let’s see the different examples of gzip command

How to Compression the file by deleting the original file

Gzip linuxFileName

This will replace a linuxFileName with linuxFileName.gz which has a reduced size to 80% in the current directory. The filename size is reduced by this command. Once the gz file is created, linuxFileName should be deleted.

Compression of the file with keeping the original file

Gzip –c  linuxFileName

This command will behave the same as the above command expect the original file is not deleted. So original file should be kept as it is. You will have two files in the current directory linuxFileName and linuxFileName.gz

Uncompress/decompress the gz file

Gunzip fileName.gz

This will unzip the filename.gz and get the original file before using gzip command

Compression of multiple files in a directory

Gzip -r directoryname

using -r option, recursively traverse all the files, meaning all the files in the current directory including all the files subdirectory, and create a directoryname.gz which contains all the files in the current directory and subdirectory After compression, the total size of the files is approximately 20% less gz file.

Uncompress/decompress the gz file into multiple files

Gunzip -r fileName.gz

```This will unzip the filename.gz into the multiple original files before using gzip -r command

## Compression files fastly

Gzip -1 filename.txt Gzip –fast filename.txt

Compression files fastly

Gzip -9 filename.txt Gzip –best filename.txt

Both above options compress filename.txt files slowly and create filename.txt.gz folder

**Advanced gzip examples**

## zip each file in the current directory and create a separate gz.

for filename in *.txt; do gzip -c “$filename” > “$filename.gz”;

let us say we have file1.txt,file2.txt,file3.txt in the current directory /tmp/directory. To do this, we have to iterate each file and do the gzip command redirect(>) the output as a gz file
The above command creates file1.txt.gz,file2.txt.gz,file3.txt.gz
\-c option keeps all the original files (file1.txt,file2.txt,file3.txt) and gives the file to the stdout console.
if we don't specify any option, it will remove all the files, and create a gz file

Hope you got a basic start for gzip with examples.

Please feel free to comment and if you have any questions, leave a comment, I would get back to you.
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.