How to compress recursively using gzip

→ Are you a new visitor? Please visit the page guidance for new visitors ←

Compress recursively using Gzip

What is Gzip? Gzip is a compression tool design to replace the ‘compress’ command from linux due to adding more compression and being even more faster. This has been accepted by a lot of different open source projects, web servers like nginx for example use Gzip for their compression method. A lot of other website page testing service are now checking if a website has been compressed using Gzip or not. Gzip has been getting more and more popular now, however, while Gzip stands its ground on the internet for serving fast compressed pages, it is also used locally on any computer for compressing data. In this article I’m going to show you how to compress recursively using Gzip, I’ve seen a few questions on the internet about this and thought I clear this misunderstanding.

Can Gzip be used recursively?

The simple and short answer is “No”. But it is possible with a little workaround.

Gzip has only been designed as a compression tool, its algorithm does contain anything related to compressing a sub folder or similar. However that’s not stopping anyone from using this tool, more like people thought of a different idea, which is:

“Why not grab the content in one single file, then compress it using Gzip?”

That’s the question, and the solution to this came to using the already existing tool name Tar, I believe this tools already existed since the early time of Unix, Gzip came afterwards and both Tar and Gzip complement each other really nicely.

So the solution first would be to tar the content of a folder and only afterwards apply compression. Of course the people who developed Tar have included the Gzip option directly in their command so that you can compress the tar file that is being generated at the end.

Command to compress a folder using Gzip

To compress a folder or file you would need to run the following command:

This will add the contents in a TarĀ format (tarball) and then compress it using Gzip at the end. This will of course include all the files and subfolders from within that directory.

I thought of adding these for those that want to know more about the Tar command. I highly recommend this when trying to preserve permission, this is done by using the -p flag. Bellow you have the exact flags I mostly use:

p == preserve permissions
c == create archive
v == verbose (print names while making tar)
z == Gzip
f == tar file name

Decompress a tar.gz file on linux

If you are on Windows to decompress the tar.gz generated file you would use a similar application like 7zip ( However on linux operating system, you can use the same tool to decompress the archive to how it was before.

The difference is by using the -x flag instead of -c for creating the archive.


Gzip is a really powerful tool and it is fairly fast, pigz may be even faster since its design to make use of all the server cores, while Gzip is being tied down to one core. In any case, Gzip still holds its ground.

That’s it for now, see you next time in our next article.

Request an article ←