CLI AI

Extract tar.gz, zip, or 7z on Linux with one command

2026-06-15

tar -xf archive.tar.gz extracts tar, tar.gz, tar.bz2, tar.xz, and tar.zst — modern tar detects the compression from the file itself, no flag needed. The other habit that saves you a mess is checking what's inside an unfamiliar archive before you extract it, since one without a shared top-level folder dumps everything into your current directory.

1. See what's inside first

clai
$ clai show me what's inside this archive without extracting it→ tar -tf arch.tar.gz | head -4d/d/b/d/b/two-copy.txtd/a/

-t lists the contents instead of extracting them. If every path shares one top-level folder, the archive is well-behaved. If the listing shows paths like ./file1, ./file2 with no shared folder, extracting it will scatter everything across your current directory.

2. Extract without thinking about compression

clai
$ clai extract this archive→ tar -xf arch.tar.gz

Modern GNU tar detects gzip, bzip2, xz, and zstd from the file's content. The z, j, and J flags only matter for older tar versions and for creating an archive. One command covers every tar-based format.

3. Extract into a separate directory

clai
$ clai extract the archive into a folder called out→ mkdir -p out && tar -xf arch.tar.gz -C out

-C changes directory before extracting. It's also the fix for the archive bomb from section 1: even without a shared top-level folder, the mess lands in out, not underfoot.

4. Pull out a single file

clai
$ clai extract just the file d/a/three.txt from the archive→ tar -xf arch.tar.gz d/a/three.txt

Give the path exactly as it appears in tar -tf's output. Don't guess it — copy it from the listing.

5. Other formats

clai
$ clai extract this zip into a folder called out→ unzip -d out archive.zip

unzip takes its output directory with -d, not -C. For 7z it's 7z x archive.7z -oout — no space after -o, or it reads as a filename. Every tool invented its own flag for the same idea.

6. Grep inside without extracting

clai
$ clai search for the word error in the archive without extracting it→ tar -xzOf logs.tar.gz | grep -n 'error' | head

-O streams file contents to stdout instead of writing them to disk. For a multi-gigabyte log archive, that's the difference between doing it right now and first finding 10 GB of free space.

Gotchas

  • The f flag has to come last in a bundle. tar -xfz tries to use z as the filename. Write tar -xzf or just tar -xf.
  • .tar.gz and .tgz are the same thing. So are .tar.bz2 and .tbz2. The extension decides nothing — tar looks at the content.
  • An archive with absolute paths is dangerous. If tar -tf shows paths starting with /, extracting it can overwrite system files. Only extract that kind of archive with -C into its own directory, after checking the listing.

Related questions

How do I see file sizes inside an archive? tar -tvf archive.tar.gz — v adds permissions, owner, size, and date.

Why does unzip mangle non-ASCII filenames? Zip filename encoding was never standardized. unzip -O CP866 fixes archives that came from Windows with Cyrillic names.

How do I extract many archives at once? for f in *.tar.gz; do tar -xf "$f" -C "${f%.tar.gz}"; done, after creating the directories.

See also

CliAI writes the right extraction command for whatever archive you throw at it, flags and all. Install it in one line.