How to Open and Extract .tar.bz2 Files by Example

Updated

Creating a compressed archive in a Linux terminal

To extract a .tar.bz2 file, run tar xf archive.tar.bz2 — on this machine’s tar (bsdtar 3.5.3), and on any modern GNU tar, you do not need -j, -z, or any other compression flag; the tool reads the archive’s header and picks bzip2, gzip, xz, or zstd decompression on its own. If you only need one file out of the archive, add its path: tar xf archive.tar.bz2 path/to/file. The flags that decide where it lands — -C for a target directory, --strip-components for the archive’s top-level folder — behave the same way whatever the compression is.

The benchmark this page used to publish was fabricated

An earlier version of this page said: “In benchmark tests on a 1GB dataset: tar.gz (gzip) → Compress: ~35 seconds | Extract: ~12 seconds. tar.bz2 (bzip2) → Compress: ~95 seconds | Extract: ~28 seconds.” That is wrong because no such test is described anywhere — no hardware, no file contents, no tool versions, no command line. Those four numbers were invented and presented as a measured result. They are deleted, and nothing here restates them as fact; what follows is a benchmark that was actually run, with the command output pasted below.

What was actually measured

Two ~50MiB inputs, chosen to show the honest range rather than one convenient number:

  • text.txt (54,865,470 bytes) — the macOS system word list (/usr/share/dict/words, 235,976 entries) shuffled and concatenated 22 times, so it is real English text without being one exact repeated block.
  • random.bin (52,428,800 bytes) — 50MiB from /dev/urandom, an incompressible control. No general-purpose compressor beats entropy it can’t find.

Each tool ran at its plain default invocation (gzip -c, bzip2 -c, xz -c -T1, zstd -c -q), single-threaded, compressing then decompressing, with a byte-for-byte cmp after each round trip:

RESULT label=gzip  (text.txt) orig_bytes=54865470 compressed_bytes=26190347 ratio=2.095 compress_s=2.033 decompress_s=0.100 roundtrip_ok=YES
RESULT label=bzip2 (text.txt) orig_bytes=54865470 compressed_bytes=21122984 ratio=2.597 compress_s=2.944 decompress_s=1.647 roundtrip_ok=YES
RESULT label=xz    (text.txt) orig_bytes=54865470 compressed_bytes=16170464 ratio=3.393 compress_s=47.782 decompress_s=0.604 roundtrip_ok=YES
RESULT label=zstd  (text.txt) orig_bytes=54865470 compressed_bytes=25547349 ratio=2.148 compress_s=0.115 decompress_s=0.072 roundtrip_ok=YES
RESULT label=gzip  (random.bin) orig_bytes=52428800 compressed_bytes=52444813 ratio=1.000 compress_s=0.816 decompress_s=0.034 roundtrip_ok=YES
RESULT label=bzip2 (random.bin) orig_bytes=52428800 compressed_bytes=52660870 ratio=0.996 compress_s=3.892 decompress_s=3.133 roundtrip_ok=YES
RESULT label=xz    (random.bin) orig_bytes=52428800 compressed_bytes=52431460 ratio=1.000 compress_s=26.621 decompress_s=0.109 roundtrip_ok=YES
RESULT label=zstd  (random.bin) orig_bytes=52428800 compressed_bytes=52430010 ratio=1.000 compress_s=0.019 decompress_s=0.013 roundtrip_ok=YES

On real text, bzip2 beat gzip’s ratio (2.60x vs 2.10x) and was slightly slower to compress (2.9s vs 2.0s) — nowhere near the fabricated 95s vs 35s gap, because that gap never existed at any size. xz compressed tightest (3.39x) but took 24x longer than gzip to get there. zstd at its default level compressed almost as fast as nothing at all (0.12s) for a ratio close to gzip’s. If gzip is the one you end up using, note that gzip file deletes the original with no prompt — -k is what keeps it. On random bytes, every tool landed at essentially 1.0x — gzip and bzip2 actually made the file slightly larger, because they still spend a few bytes on framing with nothing to compress. Every round trip verified byte-identical with cmp.

What this does and doesn’t generalize to

Compression ratio is extremely data-dependent — that’s the actual finding here, not any single number. A shuffled word list is highly redundant at the token level but has no long repeated byte runs; a real backup tarball of application logs, database dumps, or source code will compress differently again, sometimes much better (long repeated strings), sometimes worse (numbers and hashes resist all four tools similarly). The random-bytes row is the floor every format converges toward — already-compressed media (JPEG, MP4, most ZIP-nested content) behaves close to it. This ran single-threaded on default settings on one machine; it says nothing about xz -T0, zstd --ultra -22, pigz, or archives with thousands of small files, where per-file overhead dominates instead of the algorithm.

tar’s auto-detection, tested here

The old page’s flag table implied -j is required for bzip2. On this machine’s bsdtar, it is not: tar cjf a.tar.bz2 dir/, tar czf a.tar.gz dir/, tar cJf a.tar.xz dir/, and tar --zstd -cf a.tar.zst dir/ all built correctly, and then tar xf — no compression flag at all — extracted every one of the four archives with exit code 0. GNU tar’s own manual documents the same auto-compress detection on extraction; that part is cited, not tested here, since this Mac has bsdtar, not GNU tar. If you’re on a genuinely ancient tar that lacks auto-detection, the explicit flags are -j (bzip2), -z (gzip), -J (xz), and --zstd.

Windows and GUI options

Windows has no built-in .tar.bz2 support; 7-Zip extracts it in two passes (bz2 layer, then the resulting .tar) via right-click → Extract, or 7z x on the command line. On Linux and macOS desktops, File Roller (GNOME), Ark (KDE), and The Unarchiver (macOS) all read the format without a terminal.

If extraction fails with a corruption error, don’t re-extract blind — check the archive’s checksum against what the sender published, using the same approach as generating and verifying an MD5 checksum on Linux, then re-download if it doesn’t match.

/verify/open-and-extract-tar-bz2-files.sh regenerates both test inputs, re-runs all eight compress/decompress/round-trip checks, and re-runs the four-format tar auto-detection test, printing PASS/FAIL/SKIP for each.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.