tar system

it's a file. in a file. this isn't hard.

The tar man page is 1,800 lines long. You need to know about four flags and you’re set for life.

You downloaded a .tar.gz file. You right-clicked it. You saw “Open with…” and nothing useful. So you Googled “how to open tar.gz on Windows.” You downloaded 7-Zip. Or WinRAR. You installed it. WinRAR told you it was a “40-day trial.” That was eleven years ago. It’s still running. It’s still reminding you. Every. Single. Time. You open it.

You’ve been using unlicensed software for over a decade to do something that takes one command.

Unless you’re running Windows — then what the hell are you doing here anyways?! If you want to join the party and understand what the hell we’re talking about, go install WSL2. We’ll wait. Impatiently.

Lazy sysadmins can skip straight to the cheat sheet.


Extract a .tar.gz (the thing you Googled)

This is why 90% of you are here. You downloaded something. It ends in .tar.gz or .tgz. You want the files inside.

tar -xzf archive.tar.gz

That’s it. The files are in your current directory. No wizard. No progress window with a green bar. No “Choose destination folder” dialog. No nag screen asking you to buy a license.

Extract to a specific folder

tar -xzf archive.tar.gz -C /path/to/destination/

-C means “put the stuff here instead.” The folder must already exist because tar isn’t going to hold your hand.

Extract a .tar.bz2

Same thing, different compression.

tar -xjf archive.tar.bz2

Notice we changed -z to -j. That’s it. That’s the only difference. One character. WinRAR didn’t even support bz2 until version 3-point-something and required a restart after installation. To extract a file.

Extract a .tar.xz

The new hotness. Better compression than gzip, slower but smaller.

tar -xJf archive.tar.xz

Capital -J this time. Lowercase z for gzip, lowercase j for bzip2, uppercase J for xz. Is it elegant? No. Will you remember it? Eventually. Is it better than installing a GUI application? Always.

Just look at what’s inside (don’t extract)

Not sure what’s in there? Peek first.

tar -tzf archive.tar.gz

Lists every file in the archive. Use this before extracting into your home directory and discovering someone archived 47,000 files without a top-level folder. That person is a monster, but at least now you know before it’s too late.

Peek at bz2 and xz too

tar -tjf archive.tar.bz2
tar -tJf archive.tar.xz

Same pattern. -t for list instead of -x for extract.


Create an archive

You need to give someone a folder. You could zip it. You could email 400 individual files. Or you could do what every Unix system has been doing since before you were born.

Create a .tar.gz

tar -czf archive.tar.gz /path/to/folder/

One command. The entire folder, compressed, in a single file. Name it whatever you want. Send it wherever you want.

Create a .tar.bz2 (smaller, slower)

tar -cjf archive.tar.bz2 /path/to/folder/

Better compression ratio if you’re shipping something big and don’t mind waiting an extra few seconds.

Create a .tar.xz (smallest, slowest)

tar -cJf archive.tar.xz /path/to/folder/

Maximum compression. Use this for stuff you’re archiving long-term. Your hard drive will thank you. Your CPU will not.

Archive multiple folders

tar -czf backup.tar.gz /home/user/projects/ /home/user/documents/ /etc/nginx/

Just keep listing paths. tar doesn’t care how many you throw at it.

Exclude stuff you don’t want

tar -czf project.tar.gz --exclude='node_modules' --exclude='.git' /path/to/project/

Because nobody needs a 2GB archive that’s 98% node_modules. Nobody. If you’ve ever sent someone a tarball with node_modules in it, you owe them an apology.

Add files to an existing archive

tar -rf archive.tar newfile.txt

Note: this only works with uncompressed .tar files. You can’t append to a .tar.gz because the compression doesn’t work that way. Decompress it, append, recompress. Or just create a new archive because your time is worth more than this.


The flags that actually matter

Here’s the decoder ring. Every tar command is just a combination of these:

Flag What it does
-c Create an archive.
-x Extract an archive.
-t List contents without extracting.
-z Use gzip compression (.tar.gz / .tgz).
-j Use bzip2 compression (.tar.bz2).
-J Use xz compression (.tar.xz).
-f The next argument is the filename. This must come last before the filename.
-v Verbose. Show every file as it’s processed. For when you need proof something is happening.
-C DIR Extract to a specific directory.
--exclude=PATTERN Skip files matching the pattern.

The pattern is always: action + compression + f + filename. That’s it:

tar -[c/x/t][z/j/J]f filename.tar.gz [path]
     ^^^^^^  ^^^^^^
     what    how
     to do   to compress

Once you see the pattern, you can’t unsee it. You’ll never Google “tar command” again.


“But WinRAR—”

Stop.

“WinRAR has a nice interface.” WinRAR has been in a “40-day trial” since approximately 2003. It has never expired. It has never stopped working. It just keeps asking. Every time you open it. Forever. That’s not software, that’s a guilt trip in an installer. Also, you installed a 4MB application to do what one command does.

“7-Zip is free and open source.” 7-Zip is fine. For Windows. Where you don’t have tar. But you’re reading this site, which means you either have tar or you should go install WSL2 and then you’ll have tar. Either way, you don’t need 7-Zip.

“I need to create .zip files.” Then use zip. It’s also a command. zip -r archive.zip folder/. Done. But between us, tar.gz is smaller, faster, and preserves Unix permissions. zip doesn’t know what file permissions are because it was made for Windows and Windows doesn’t know what file permissions are either.

“The Archive Manager on Ubuntu opens .tar.gz files with a double-click.” Great. And it also opens them slower, uses more memory, and can’t be scripted, piped, or automated. You double-clicked a file to extract it. Your grandma does that. You’re better than this.

“I can never remember the flags.” You literally just need -xzf to extract and -czf to create. Six characters total. You remember your Netflix password, your WiFi password, and the lyrics to at least three songs you haven’t heard in ten years. You can remember -xzf.


Cheat sheet

You made it. Or you skipped straight here. Either way, no judgment. Copy and paste these. Pin them. Tattoo them on your forearm. Whatever works.

What you’re doing Command
Extract .tar.gz tar -xzf archive.tar.gz
Extract .tar.bz2 tar -xjf archive.tar.bz2
Extract .tar.xz tar -xJf archive.tar.xz
Extract to a folder tar -xzf archive.tar.gz -C /dest/
List contents (don’t extract) tar -tzf archive.tar.gz
Create .tar.gz tar -czf archive.tar.gz /path/
Create .tar.bz2 tar -cjf archive.tar.bz2 /path/
Create .tar.xz tar -cJf archive.tar.xz /path/
Exclude files tar -czf a.tar.gz --exclude='node_modules' /path/
Create a .zip (if you must) zip -r archive.zip folder/

The pattern: action (create/extract/list) + compression (z/j/J) + f + filename. That’s it.

Back to the top, you overachiever.