Software & Apps Linux 55 55 people found this article helpful Example Uses of the Linux gzip Command There are plenty of things you can do with the Linux gzip command by Gary Newell Writer Gary Newell was a freelance contributor, application developer, and software tester with 20+ years in IT, working on Linux, UNIX, and Windows. our editorial process Gary Newell Updated on April 14, 2020 reviewed by Chris Selph Lifewire Tech Review Board Member Chris Selph is a CompTIA-certified technology and vocational IT teacher. He also serves as network & server administrator and performs computer maintenance and repair for numerous clients. our review board Article reviewed on Aug 10, 2020 Chris Selph Linux Switching from Windows Tweet Share Email The gzip command is a common way of compressing files within Linux. By default when you compress a file or folder using the gzip command it will have the same file name as it did before but with the extension .gz. The screenshots below apply to Ubuntu specifically, but the gzip command works on other Unix-like OSs, too. How to Compress a File Using gzip The simplest way to compress a single file using gzip is to run the following command: gzip filename To compress a file called mydocument.odt run the following command: gzip mydocument.odt If the file name contains spaces, surround it by quotes: gzip "this is my file.png" Some files compress better than others. For example documents, text files, bitmap images, and certain audio and video formats such as WAV and MPEG compress very well. Other file types such as JPEG images and MP3 audio files do not compress at all well and the file may actually increase in size after running the gzip command against it. JPEG images and MP3 audio files are already compressed and therefore the gzip command simply adds to it rather than compressing it. The gzip command only attempts to compress regular files and folders. How to Decompress a File Using the gzip Command Use the following command to decompress a gzip archive: gzip -d filename.gz To decompress the mydocument.odt.gz file use the following command: gzip -d mydocument.odt.gz Force a File to Be Compressed Sometimes a file cannot be compressed. To force the gzip command to do its stuff simply run the following command: gzip -f filename How to Keep the Uncompressed File By default when you compress a file using the gzip command you end up with a new file with the extension .gz. To compress the file and keep the original file must specify the -k flag: gzip -k filename I you run the following command you would end up with a file called mydocument.odt and mydocument.odt.gz. gzip -k mydocument.odt Get Some Stats About How Much Space You Saved The whole point of compressing files is about saving disk space or to reduce the size of a file prior to sending it over a network. The gzip command provides the kind of statistics you require when checking for compression performance. To get the list of statistics run the following command: gzip -l filename.gz In the above command, the option is a lowercase L, not a 1 or uppercase i. The information returned by the above command is as follows: Compressed sizeUncompressed sizeRatio as a percentageUncompressed filename Compress Every File in a Folder and Subfolders Compress every file in a folder and its subfolders by using the following command: gzip -r foldername This process doesn't create one file called foldername.gz. Instead, it traverses the directory structure and compresses each file in that folder structure. To compress the folder structure as one file you are better off creating a tar file and then gzipping the tar file. Use this command to decompress multiple files: gzip -d *.gz How to Test the Validity of a Compressed File To verify that a file is valid, run the following command: gzip -t filename If the file is valid there will be no output. How to Change the Compression Level You can compress a file in different ways. For example, you can go for a smaller compression which will work faster or you can go for maximum compression which has the tradeoff of taking longer to run. To get minimum compression at the fastest speed run the following command: gzip -1 filename To get maximum compression at the slowest speed run the following command: gzip -9 filename You can vary the speed and compression level by picking different numbers between 1 and 9. Standard Zip Files The gzip command should not be used when working with standard zip files. You can use the zip command and unzip command for handling those files.