Top 10 gzip examples in Linux or unix

Multiple examples of gzip commands in Linux and Unix.

  • gzip and zip command tutorial examples
  • How to Compression the file by deleting the original file
  • Compress the files with keeping the original file
  • Uncompress/decompress the gz file
  • multiple files and directories compress

What is Gzip command in linux

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

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

This will save bandwidth if the file is transferred between different systems. Moreover, the reduced size depends on the content of the file, if the content is text, it will reduce by 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 in Linux and Unix

Let’s see the different examples of gzip command

How to Compression the file by deleting the original file

The gzip command compresses 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 compress the file and the original file is not deleted.

Gzip –c linuxFileName

This command will behave the same as the above command except 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 command compress and decompress the gz files

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

Both above options compress filename.txt very fast and create a filename.txt.gz folder

Compression files fastly

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

Both above options compress filename.txt files slowly and create a 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