• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
NameHero® Blog

NameHero® Blog

Web Hosting Tips & Resources From NameHero

  • AI Agents
    • OpenClaw
    • Claude Code
    • n8n
    • Hermes Agent
    • Open WebUI
    • Docker
  • Hosting
    • Web Hosting
    • WordPress Hosting
    • WooCommerce Hosting
    • Enterprise Hosting
    • Email Hosting
    • HeroicGuard
    • GPU Hosting
    • Website Builder
  • VPS
    • Managed VPS
    • Unmanaged VPS
    • Flex VPS
  • Reseller
  • Gaming
  • Domains
  • Account
  • Blog Home
  • Categories
  • Authors

How to Tar and Gzip a Folder

Bhagwad Park

Published on: June 4, 2025

Categories: Linux Command Line, VPS Hosting 0

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:

Creating a Test Folder

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
New Tar and Gzipped Archive Created
New Tar and Gzipped Archive Created

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:

Extracting the New Archive
Extracting the New Archive

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:

  1. tar.bz2 – bzip2 compression algorithm
  2. 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:

  1. tar -xjf archive.tar.bz2 – for extracting bzip2 files
  2. 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.

Bhagwad Park Profile Picture
Bhagwad Park

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!

Related Posts

Using Systemctl to Enable Services on Boot on Linux

You can use the systemctl tool to start services automatically on boot. We do this using the "enable" keyword.

How to Use Tmux: The Complete Guide

Tmux is a "terminal multiplexer", that allows you to run several processes from one command line window. Here's how it works.

Bash Signal Handling with Trap: EXIT, ERR, INT

Bash in Linux allows us to set functions that run when certain signals are generated - either by the script, or the shell. Here's how.

How to Prune Unused Images in Docker

In Docker, containers and images can build up over time, using up space and resources. Here's how to get rid of them.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow & Subscribe

Exclusive promos, content and more!


Most Popular Posts

NameHero’s Recommended WordPress Plugin and Theme Setup

WordPress Hosting vs. Web Hosting – What’s The Difference?

How To Increase The InnoDB Buffer Pool Size

How To Fix A Stuck All-in-One WP Migration Import

How To Add A Subdomain In Cloudflare

Top Categories

  • WordPress
  • WordPress Tutorials
  • OpenClaw Hosting
  • Enterprise Hosting
  • WooCommerce
  • Web Hosting
  • Resellers
  • Website Security
  • Website Development
  • Website Performance
  • VPS Hosting
  • SEO Tips
  • Announcements
  • Domain Registration
NameHero

NameHero® proudly provides web hosting to over 40,000 customers with 99.9% uptime to over 750,000 websites.

  • Master Card
  • Visa
  • American Express
  • Discover
  • Paypal
Products
  • Web Hosting
  • Managed VPS Hosting
  • Unmanaged VPS Hosting
  • Flex VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • GPU Hosting
  • Email Hosting
  • HeroicGuard
  • Domains
  • Website Builder
  • AI Agent Hosting
Help & Support
  • NameHero Blog
  • NameHero Gaming Blog
  • Support
  • Help Center
  • Migrations
  • Affiliates
  • Gaming Affiliates
  • Call 1-855-984-6263
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2026 Name Hero, LLC. All rights reserved.
NameHero® is a registered trademark.

  • Privacy Policy
  • Terms of Use
  • Acceptable Use Policy
  • Payment Policy
  • DMCA