Extract a compressed archive

Low risk
What do risk levels mean?
Read-only
Inspects state without changing anything.
Low risk
Reversible with a routine follow-up command.
Medium risk
Changes state; undo path documented.
Destructive
Deletes or overwrites; confirmation required.

Unpacks a tar.gz or zip archive into a target directory, first listing its contents so you know what will be written where.

filesarchiveextractunzip

Parameters

Path of the archive to extract (.tar.gz, .tgz, or .zip).

Directory to extract into.

Commands

Preview the archive's contents before extracting

tar -tzf "<archive>" | head -n 20

Extract the archive into the destination directory

mkdir -p "." && tar -xzf "<archive>" -C "."

Verification

ls "."

The archive's top-level files and directories are present in the destination.

Undo

Remove the extracted entries, leaving the archive itself intact

tar -tzf "<archive>" | xargs -I {} rm -rf "./{}"

Pitfalls

  • Archives without a single top-level directory scatter files into the destination ("tar bomb") — the preview step shows this before it happens.
  • Extraction overwrites existing files with the same names without asking.
  • For .zip on macOS/Linux use unzip; tar -xzf only handles tar-based formats.

Related