In server environments, we often need to replicate folders across locations. For example, one super common use case is backup, where you periodically backup many log files. Another example is where you need to move the source code of an entire project from one server to another. In such situations, you need to mimic the directory structure of the source to the destination exactly, and maintain other meta information like the file creation and modification dates, etc. For these purposes, tar and gzip are perfect.
The tools with these specific names have traditionally been Linux-based, because Windows has its own “zip” command, but built-in tools like Powershell now also recognize the tar and gzip commands, probably to ease the transition from Linux admins to Windows. Here’s all you need to know about these tools in both environments.
What Do Tar and Gzip Do?
“Tarring” and “Gzipping” files have a strange history of terminology. The “tar” tool stands for “Tape Archive”, and was first used as a means to transport multiple files via magnetic tape! The purpose was to reduce a whole folder structure into a single file so that it could be unpacked at a different location. So far, it doesn’t contain any compression methodology. The purpose is strictly archival.
“Zipping”, on the other hand, is based around compression. It can compress anything, not just tar files, and was initially created as an open-source alternative to the earlier “compress” tool. “Gzip” is a standalone tool and can be used to only compress individual files, not folders. Hence, the need for tarring first.
To “tar and gzip” taken together work naturally to both make it easier to transfer a single file instead of a bunch of them, and to compress that single file so that the transfer happens much sooner. In today’s terminology, the two tools work together via a single flag “-z”, and we’ll see below how it works.
How to Tar and Gzip a Folder in Linux
To start, I’m going to first create a test folder and put some files into it:
Now that I have a dummy folder with some files, I’m going to both reduce them to a single file and compress them with one command:
tar -czf archive-name.tar.gz ./test_folder
As you can see, after running the command, I now have a new “tar.gz” file. My bash editor via PuTTY recognizes this file as special and even shows it as a different color.
The flags I use for creating this file are as follows:
-c: This tells the tool to create a new archive. This is important because without it, tar might not know that you’re trying to create a new archive – there are other functions for which we use tar, and the tool won’t know what you’re trying to do.
-z: This tells the tool to also compress the file in addition to creating an archive
-f: This allows us to specify the name of the archive that will be created
And that’s it! We’ve created a tarred and zipped archive. Note that it has the extension “.tar.gz”. This means that it contains both an archive and its compressed. If it has only the “.tar” extension, it means that it’s not compressed.
Extracting a Compressed Tar Archive
Of course, creating an archive is only the first step. Now let’s move it to another location and extract it to see whether it’s able to accurately recreate the file structure from one folder to another. So first, I create a new empty folder called “new_location”. Then I move the archive file into that folder and run the following command:
tar -xzf archive-name.tar.gz
And here’s the output:
As you can see, at first, the folder “new_location” was empty. Now it’s a replica of the “test_folder” structure. The original “archive-name.tar.gz” file hasn’t vanished, however. You can either delete it, extract it somewhere else, or just keep it as a backup.
Using Tar and Gzip on Windows
While tar and gzip were originally Linux tools for a long time, Windows can now also use them with almost the same syntax, probably to smooth the way for Linux administrators who were trying to make the switch from Linux to Windows.
So just like in Linux, you can use the same commands to compress and archive files, as well as extract them:
tar -czf archive-name.tar.gz folder-name
and
tar -xzf archive-name.tar.gz
No Zip Command in Windows
The “tar -czf” command automatically handles file compression in Linux, so we don’t need to do it separately. But if we want, we could still use the “zip” command to compress files like this:
zip -r archive.zip my-folder/
In Windows, however, the zip command doesn’t exist. Instead, if you want to compress files, you must use the “Compress-Archive” command like this:
Compress-Archive -Path my-folder -DestinationPath archive.zip
Similarly, there’s no “unzip” command, either. You must use “Expand-Archive”, like this:
Expand-Archive -Path archive.zip -DestinationPath folder/
Apart from minor differences like this, the primary workflow of using tar to compress and extract files remains the same. Now, in Windows, even the regular “cmd” command-line tool supports it, and not just PowerShell.
Understanding the “tar.gz” Extension
It might seem odd to see a file have what appears to be two extensions. A file like “archive-name.tar.gz” seems to have both “tar” and “gz” as extensions. This is a useful shorthand to show that this is an archive that has also had compression added to it. Without these extensions, you wouldn’t know if a particular archive is compressed or not.
The commands for extracting an archive differ, depending on whether or not it’s already compressed. For example:
tar -xzf archive.tar.gz
Here, the “-z” parameter tells the tool that the archive is compressed, and it signifies that the output must be parsed through gzip. The tool itself doesn’t know or care about the extension – it’s only for our benefit that the filename is presented this way. If the archive name was simply “archive-name.tar”, it would indicate to us that the archive isn’t compressed, and the command for extracting it would be:
tar -xf archive.tar.gz
This would simply expand the archive file, without needing to decompress it.
Different Compression Algorithms
The “tar” command accepts arguments that can extract archives from different types of compression algorithms. Just like a file ending in “tar.gz” means that it’s been compressed using gzip, the following types of filenames also exist:
- tar.bz2 – bzip2 compression algorithm
- tar.xz – xz compression algorithm
Both bzip2 and xz are more efficient at compression than gzip, but are correspondingly slower. xz is even slower than bzip2, but has the best compression ratio. These algorithms are frequently used for distributing source code and software applications in Linux. To use the tar tool to extract files compressed using these formats, we use the following flags:
- tar -xjf archive.tar.bz2 – for extracting bzip2 files
- tar -xJf archive.tar.xz – for extracting xz files
You’ll know which tool to use based on the extension.
Conclusion
As you can see, tarring and gzipping a file is easy in Linux, and can be accomplished with just a single command. You can do the same thing in Windows, too, these days. If you want, you can also use alternative compression algorithms like bzip2 and xz, and compress and extract archives using the same “tar” tool with different flags.

I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!




Leave a Reply