How to unpack tar gz files, your Ultimate Guide

Bits Lovers
Written by Bits Lovers on
How to unpack tar gz files, your Ultimate Guide

If you work with Linux, you’ll run into tar.gz files constantly. Open-source projects love them. They’re like the zip format of the Unix world—everywhere you look.

Let me walk you through how to work with these files.

What are tar and gz?

Tar stands for “Tape Archive.” Back in the day, it literally wrote files to magnetic tape. The name stuck around.

Gzip compresses data. It’s been around forever (Jean-loup Gailly wrote it for the GNU project), and every web browser handles gzip compression automatically.

Put them together and you get tar.gz: files packaged with tar, then compressed with gzip. Unix systems use this format everywhere. You’ll often see .tgz as shorthand—older systems like MS-DOS couldn’t handle file extensions with multiple dots.

Tar supports other compression tools too: bzip2, xz, lzip, lzma. These days you’ll also see zstd (Zstandard) gaining traction because it’s faster than gzip with similar compression ratios. More on that later.

Extracting tar.gz files

The tar command handles both creating and extracting archives. It’s built into every Unix-like system, so you don’t need to install anything.

Basic extraction

tar -xvzf bitslovers-app.tar.gz

Here’s what those flags do:

  • x - extract files from the archive
  • v - verbose mode (shows you what’s happening)
  • z - handle gzip compression
  • f - specifies the filename

Simple, right?

Extract to a specific directory

Don’t want files dumped in your current folder? Point them elsewhere:

tar -xvzf bitslovers-app.tar.gz -C /home/bitslovers/my-app

The -C flag tells tar where to put the files.

Peek inside first

Sometimes you want to see what’s in there before extracting:

tar -tzf bitslovers-app.tar.gz

The -t flag lists the archive contents instead of extracting them.

Creating tar.gz archives

Need to compress your own files? Just flip the script:

tar -cvzf bitslovers-app.tar.gz /home/bitslovers/my-app

The -c flag creates a new archive. Note the order: flags first, then the archive name, then the files to compress.

Want to pack multiple directories?

tar -cvzf backup.tar.gz /home/bitslovers/my-app /etc/httpd/conf

Just keep adding paths.

Working with plain tar files

Not all tar archives are compressed. If you see a .tar file (no .gz), skip the -z flag:

tar -xvf archive.tar

The same pattern works for creating uncompressed archives:

tar -cvf archive.tar /etc/httpd/conf

Extracting specific files

Got a massive archive but only need one file? Don’t extract everything—grab just what you need:

tar -xf archive.tar.gz path/to/specific/file

You can list multiple files:

tar -xf archive.tar.gz file1 file2 file3

Using wildcards

Wildcards work too, but you need quotes so the shell doesn’t interpret them:

tar -xf archive.tar.gz --wildcards '*.conf'

This extracts every .conf file in the archive.

Finding the exact path

If you’re not sure of the exact path, list the archive contents first:

tar -tzf archive.tar.gz | grep filename

The new kid: Zstandard compression

Since 2021 or so, zstd (Zstandard) has become the default choice for many projects. Why? It’s faster than gzip and compresses better. You’ll see .tar.zst or .tzst files more often now.

Creating zstd-compressed archives:

tar -I zstd -cf archive.tar.zst /path/to/directory

Extracting them:

tar -I zstd -xf archive.tar.zst

Some newer tar versions even support --zstd as a flag.

Control compression level:

tar -I 'zstd -3' -cf archive.tar.zst directory

Zstd levels range from 1-19, with 3 being the default. Higher means better compression but slower.

Why compression matters

Before you copy files to another server, compress them. You’ll save bandwidth and time. A lot of bandwidth. Especially when you’re moving large application bundles or backups.

I’ve seen transfers take 80% less time after compression. Your mileage will vary, but it’s almost always worth it.

Wrapping up

That’s the basics of working with tar archives. Extract what you need, create backups, and don’t forget to check out man tar—there’s a ton more options I didn’t cover here.

The tar command’s been around forever because it works. Learn these basics and you’ll handle 99% of what you run into.

Bits Lovers

Bits Lovers

Professional writer and blogger. Focus on Cloud Computing.

Comments

comments powered by Disqus