How to extract a gzip file or ungzip in Mac OS via terminal

Hello, everyone. Today, I was trying to extract or ungzip a file compressed using gzip compression on Mac OS. Unfortunately, I couldn’t do it by extracting, as there was no predefined UI option that appeared after right-clicking on the file.

Gzip compression comes into play to compress HTTP content before delivering it to the client. Hence, gzip has become popular nowadays. If you are using nginx for your servers, you might have seen “gzip on” in your nginx.conf file.

Let’s address our problem of extracting a .gz file. I have to do it the old way, the “Terminal Way (which I always prefer),” to extract the gzipped file.

To Extract a gz file:

gzip -d path-to-your-file.gz

Here, -d stands for uncompress/decompress.

To compress a file using gzip compression, use the following command:

gzip -k <path-to-your-file>

Here, -k stands for compress and keep the original file. If you don’t want the original file, then use:

gzip <path-to-your-file>

Please note that the above command will modify your original file, so be aware of it while using it without any options.

To test a compressed file, use the following command:

We can verify our compressed file by using the following command

gzip -t <path-to-your-file> -v

-t stands for test, and -v stands for verbose. This will return output like:

<your-file-name>:   OK

So far, we have seen how to compress and decompress gzip files. Can we gzip a directory using gzip alone? Unfortunately, you can’t compress a directory using gzip. Don’t worry; there is a hack for that too.

To compress a directory using gzip, when you try to compress a directory using -r with gzip, it compresses all files inside a directory with gzip compression.

For example, if you have a directory structure like this:

home
   |
   test/
        -- fileone.csv
        -- filetwo.csv
        nestedtestdir/
            -- nsfilefileone.xlsx
            -- hello.txt

What happens when you fire this command:

After this, the directory structure becomes:

gzip -r test
test/
   -- fileone.csv.gz
   -- filetwo.csv.gz
   nestedtestdir/
      -- nsfilefileone.xlsx.gz
      -- hello.txt.gz

As you see, it recursively goes to each file inside the folder and compresses that file, saving it with a .gz extension.

Note: It modifies all the original files inside that folder and also other folders inside the given folder. In case you don’t want to modify the original files, add -k to keep the original files inside a directory like this:

gzip -r <your-directory> -k

To compress a directory using gzip without compressing files inside it, the hack would be to use tar and pass the tar file to be gzipped.

tar zcvf <your-gzip-file-name>.tar.gz <dir-name-to-be-gzip>/

Here, zcvf stands for z -> compress archive with, c -> create, v -> verbose, f -> filename.

You can achieve the same by:

tar -cvf - your-dir-name | gzip > your-dir-name.tar.gz

Here, -cvf stands for compress, verbose, filename after hyphen (-) your directory name. The | (pipe) symbol is used to pass on the result from one command to another command, then gzip is used to compress, and > (greater than) symbol denotes writing into your-dir-name.tar.gz, the target/output file name with .gz extension.

That’s it. Thanks for reading! Feedback is always welcome. Happy times !!!