Skip to content
shellmap

Extract a tar archive

Unpack a `.tar`, `.tar.gz`, or `.tar.bz2` file into the current directory.

How to extract a tar archive in each shell

Bashunix
tar -xzf archive.tar.gz

GNU `tar`. Use `-xjf` for `.tar.bz2`, `-xJf` for `.tar.xz`. GNU `tar` auto-detects compression with `--auto-compress`/`-a`; BSD `tar` does not.

Zshunix
tar -xzf archive.tar.gz
Fishunix
tar -xzf archive.tar.gz
PowerShellwindows
tar -xzf archive.tar.gz

Windows 10 build 17063+ ships `bsdtar.exe` in `C:\Windows\System32`. For older Windows use 7-Zip CLI: `7z x archive.tar.gz` then `7z x archive.tar`.

cmd.exewindows
tar -xzf archive.tar.gz

Same bundled `bsdtar` binary as PowerShell on Windows 10 1803+. No native cmd alternative on older Windows — install 7-Zip.

Equivalents listed for Bash, Zsh, Fish, PowerShell, cmd.exe.

Gotchas & notes

  • GNU `tar` auto-detects compression from the filename when given `-a`/`--auto-compress`; BSD `tar` (macOS, Windows 10+) treats `-z`/`-j`/`-J` as required hints.
  • List archive contents without extracting: `tar -tzf archive.tar.gz`. Useful when you suspect a tarbomb (no top-level folder).
  • Extract into a specific directory: `tar -xzf archive.tar.gz -C /target/dir` — the directory must already exist on BSD `tar`; GNU creates it on demand.
  • PowerShell has no `Expand-Tar` cmdlet. `Expand-Archive` handles only `.zip`; for tar you must call the bundled `tar`/`bsdtar` binary.

Related commands

Related tasks