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

Need to extract or create a tar.gz archive? Two commands cover almost everything: tar -xzf archive.tar.gz unpacks one, and tar -czf archive.tar.gz folder builds one. The cryptic flags actually read as words once you see them: 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 the current one. It works on Linux and macOS out of the box, and on Windows 10 and 11 too, where tar now ships built in. Here's each command with a real example, how to list what is inside before you unpack, and the one-letter swaps for .tar.bz2 and .tar.xz.

The short answer

tar -xzf archive.tar.gz extracts, tar -czf archive.tar.gz folder creates, tar -tzf archive.tar.gz lists. Read the flags as eXtract or Create, gunZip, File. Add -C /target to choose 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

By default it unpacks into the folder you are standing in. 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 a list of files:

Linux
tar -czf backup.tar.gz myfolder/

Look before you leap

Some archives unpack a hundred loose files into your current directory. List the contents first and you will never get caught by that:

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 and hate it on a slow laptop.

On Windows

You do not need a third-party tool anymore: tar ships in Windows 10 and 11, with the same flags, in both Command Prompt and PowerShell. Plain .zip files are the exception there, where Expand-Archive is the native route.

Frequently asked questions

What do the letters in tar -xzf mean?

Each one is a switch. x extracts (use c to create instead), z handles gzip compression for .gz files, and f says the next word is the filename. So tar -xzf reads as extract, gunzip, from this file. An old habit writes them as tar xzf without the dash, and that still works.

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

Add -C and the target: "tar -xzf archive.tar.gz -C /opt/app". The folder has to exist first, so run "mkdir -p /opt/app" before it if needed. Without -C, tar unpacks 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 in the archive, so you can check it will not spray files all over your current folder, which is the classic tarbomb.

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

Same commands, different compression letter: -j for .tar.bz2 and -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.