Extract a .tar.gz archive
Unpacks a .tar.gz archive into the current directory, decompressing the gzip layer and extracting every file in a single command.
Last updated
tar -xzvf archive.tar.gz
How it works
-x extracts (as opposed to -c, which creates); -z decompresses the gzip layer on the fly so you don't need a separate gunzip step first; -v lists each file as it's extracted, which is genuinely useful for confirming what actually shipped inside the archive, not just watching progress; -f names the archive to read from, and it must come LAST in the combined flag cluster since tar treats the very next command-line argument as that filename.
This is the one command most people actually need to remember for tar — extract, decompress and show progress, all from a single flag cluster with the filename right after it.
Watch out for
- →This extracts into the CURRENT directory by default; add -C /target/dir to extract somewhere else without cd-ing there first — note the target directory must already exist, since tar won't create it for you.
- →-f has to be the last letter in the combined cluster (-xzvf, not -fxzv) because whatever follows -f on the command line is read as the archive path — reordering the flags breaks that parse.
- →Modern GNU and BSD tar both auto-detect gzip/bzip2/xz compression from the archive's contents even without -z, but including it explicitly documents intent clearly and keeps the command working on older tar versions that need the hint.
Part by part
- -xzvf
- Four flags bundled into one argument, which is how tar is almost always written and why the individual letters below are not separately prefixed.
- x
- Extract. The counterpart to c for create, and the two are one letter apart, which is worth noticing before running either.
- z
- Filter through gzip. Required for .tar.gz on older tar implementations; GNU tar now detects compression automatically.
- v
- Verbose — list each file as it is handled. Optional, and genuinely useful for confirming an archive contains what you expect.
- f archive.tar.gz
- The archive filename. The f must come last in the bundle, because it consumes whatever argument follows it.
Common mistakes
tar -xzfv archive.tar.gzThe f is followed by v rather than the filename, so tar treats v as the archive name and fails with a confusing error about a file that does not exist. Keep f last in the bundle.
tar -xzvf archive.tar.gztar -xjvf archive.tar.gzj selects bzip2, not gzip. On modern GNU tar the automatic detection saves you, but on BSD and older systems it fails with an unhelpful message about an unrecognised format.
tar -xzvf archive.tar.gz in the current directory without checkingAn archive without a single top-level folder scatters its contents across your working directory. Run tar -tzf first to list the paths, which takes a second and avoids a tedious cleanup.
Questions
How do I look inside without extracting?
tar -tzf archive.tar.gz lists the contents. Doing this first is a good habit, because it shows whether everything sits under one directory or is about to spill into the one you are standing in.
How do I extract somewhere other than here?
-C takes a directory, as in tar -xzvf archive.tar.gz -C /tmp/target. The directory must already exist; tar will not create it.
Do I still need the -z flag?
On GNU tar, no — it detects gzip, bzip2 and xz automatically. Including it is still worth doing for portability, since BSD tar on macOS and older Unixes is less forgiving.
Can I extract a single file?
Yes, name it after the archive using its exact path as listed by -t. tar has to scan the whole archive to find it, so it is not fast, but it beats extracting everything.