SysadminGuide

How to extract and create tar.gz archives (tar)

On this page
  1. Extract a .tar.gz
  2. Create a .tar.gz
  3. Look before you leap
  4. Other compression: bz2 and xz
  5. On Windows

Someone hands us a release.tar.gz and the clock's already ticking. Two commands cover almost everything: tar -xzf archive.tar.gz unpacks one, tar -czf archive.tar.gz folder builds one. The flags look cryptic but they read as words once you see it: x for eXtract or c for Create, z for gunZip, f for the File whose name comes next. Add -C to drop the files into a specific folder instead of wherever you happen to be standing. Works on Linux and macOS out of the box, and on Windows 10 and 11 too, where tar now ships built in. Below, each command with a real example, plus the one-letter swaps for .tar.bz2 and .tar.xz. And how to list what's inside before you unpack. Do that one.

The short answer

tar -xzf archive.tar.gz extracts and tar -czf archive.tar.gz folder creates. Check the contents first with tar -tzf archive.tar.gz. The flags read as eXtract or Create, gunZip, File. Add -C /target to pick where the files land.

tar -xzfextract an archive
tar -czfcreate one
tar -tzflist what is inside
Answer card showing tar -xzf to extract a tar.gz and tar -czf to create one.
Two commands cover most of it. The flags spell out what they do once you know the trick. PNG

Extract a .tar.gz

Linux
tar -xzf archive.tar.gz

It unpacks into whatever folder we’re standing in, which isn’t always where we wanted it. To send it somewhere specific (the folder must already exist):

Linux
tar -xzf archive.tar.gz -C /opt/app

Create a .tar.gz

Point it at a folder, or name the files one by one:

Linux
tar -czf backup.tar.gz myfolder/

Look before you leap

Some archives unpack a hundred loose files straight into your current directory. A tarbomb. List the contents first and it never catches you:

Linux
tar -tzf archive.tar.gz
Terminal showing tar -czf creating an archive, tar -tzf listing its contents, then tar -xzf extracting into a target folder.
Create, list, extract. The list step is the one people skip and regret. PNG

Other compression: bz2 and xz

Same verbs, one different letter. Use -j for .tar.bz2 and -J (capital) for .tar.xz. xz squeezes smaller and runs slower, which is why distros love it for downloads. Honestly, on a slow laptop we’ve cursed it more than once.

On Windows

No third-party tool needed anymore: tar ships in Windows 10 and 11, same flags, in both Command Prompt and PowerShell. Plain .zip files are the exception there; Expand-Archive is the native route for those.

Frequently asked questions

What do the letters in tar -xzf mean?

Each one is a switch. x extracts (swap in c to create), z handles the gzip layer for .gz files. f says the next word is the filename, so tar -xzf reads as extract, gunzip, from this file. Plenty of us still type tar xzf without the dash out of old habit, and that works too.

How do I extract a tar.gz into a specific folder?

Add -C and the target: "tar -xzf archive.tar.gz -C /opt/app". One catch: the folder has to exist first, so run "mkdir -p /opt/app" before it if needed. Leave -C off and tar dumps everything into the current directory.

How do I see what is inside without extracting?

Swap the x for t (list): "tar -tzf archive.tar.gz". It prints every path inside, so you know whether it's about to spray files all over your current folder. That's the classic tarbomb, and we've all met one.

What about .tar.bz2 or .tar.xz files?

Same commands, one different compression letter: -j for .tar.bz2, -J (capital) for .tar.xz. Modern tar can also auto-detect from the name, so "tar -xf archive.tar.xz" usually just works whatever the compression.