Bootstrap FreeKB - Linux Commands - gzip (compress file)
Linux Commands - gzip (compress file)

Updated:   |  Linux Commands articles

The gzip command can be used to compress files. The tar command can be used to compress a directory.

In this example, the /usr/local/logs/my.log file is not compressed and is 2170505207 bytes.

~]# ll /usr/local/logs
-rw-r--r-- admin admins 2170505207 Jan 01 17:16 my.log

 

The du (disk usage) command can be used to return a more human readable output, showing my.log is 2.1 GB.

~]# du --human-readable --summarize /usr/local/logs/my.log
2.1G   /usr/local/logs/my.log

 

The gzip command can be used to compress the file.

gzip /usr/local/logs/my.log

 

This will create a new file named my.log.gz and remove the original my.log file. In this example, the compressed file is now 128699609 bytes.

~]# ll /usr/local/logs
-rw-r--r-- admin admins 128699609 Jan 01 17:16 my.log.gz

 

And the du (disk usage) command shows that my.log.gz is now 123 MB.

~]# du --human-readable --summarize /usr/local/logs/my.log.gz
123M   /usr/local/logs/my.log.gz

 


Compress every file in a directory

The -r or --recursive option can be used to compress every file at or below a directory. This does not create one .gz file that contains every file in the directory. Instead, this creates individual .gz files for each file in the directory. If you want to create one file that contains every file in the directory, you would create a tar archive.

In this example, every file at and below /usr/local/logs will be compressed.

gzip -r /usr/local/logs

 


Prompt

When manually issuing the gzip command, you may be prompted "do you want to overwrite". If you are using gzip in a script, you may want to set a default action, such as "y" to overwrite or "n" to not overwrite. In this example, the default action will be "n" to not overwrite.

echo n | gzip /usr/local/logs/my.log

 


Compression level

The -1 through -9 options can be used to select the compression level. -1 will have the least compression, but will be the fastest to compress. -9 will have the most compression, but will be the slowest to compress. In this example, the file is compressed with the most compression. If the compression level is not specified, the default compression level used is -6.

gzip -9 /usr/local/logs/my.log

 


Force overwrite

By default, if a gzip compress file already exists, the gzip command will not overwrite the existing file. The -f or --force flag can be used to overwrite the existing gzip compressed file. For example, if example.tar.gz already exists, the -f or --force flag can be used to overwrite example.tar.gz.

gzip --force example.tar

 


Statistics

The -l or --list option can be used to view statistics of a compressed gzip file. The -l option will display the size of the uncompressed file, the size of the compressed file, the compression percentage, and the name of the uncompressed file.

gzip -l /usr/local/logs/my.log
         compressed        uncompressed  ratio uncompressed_name
          128699609          2170505207  94.1% /usr/local/logs/my.log

 

Or, to list the compression level of every file in a directory.

gzip -l /usr/local/logs/*

 


Decompress

The -d, --decompress, or --uncompress option can be used to uncompress a gzip compressed file. In this example, my.log.gz is uncompressed. Similarly, the gunzip command can also be used to decompress a gzip compressed file.

gzip -d my.log.gz

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter fddb67 in the box below so that we can be sure you are a human.