In Linux, .tar.gz archives keep an entire directory tree in one compressed file, which makes them common for source releases, backups, and exported logs. Extracting them correctly restores the original files and subdirectories without manually unpacking each item.
A .tar.gz archive combines two layers: tar records the stored file paths, and gzip compresses that tar stream into a smaller file. The tar command can inspect the archive before extraction, unpack the whole tree into a chosen directory, or restore only one member when only part of the archive is needed.
Archive extraction writes files exactly as they are stored in the archive, so using a dedicated destination directory is safer than unpacking into a busy working tree. Archives can contain a top-level directory, nested paths, or names that would overwrite existing files, so check the member list first and use elevated privileges only when the destination path actually requires them.
Related: How to extract tar.bz2 files in Linux
Related: How to extract xz files in Linux
$ cd /root/sg-work/archives $ ls source source.tar.gz
$ file source.tar.gz source.tar.gz: gzip compressed data, from Unix, original size modulo 2^32 10240
Archives ending in .tgz use the same extraction commands as .tar.gz files.
$ tar -tzf source.tar.gz source/ source/reports/ source/reports/report.txt source/beta.log source/alpha.txt
The member names shown by tar -tzf are the exact paths required when extracting only one file or subdirectory later.
$ mkdir -p extract $ tar -xvzf source.tar.gz -C extract source/ source/reports/ source/reports/report.txt source/beta.log source/alpha.txt
-C extract changes to the destination directory before writing files. On current GNU tar builds, tar -xvf source.tar.gz -C extract also works because the compression type is detected automatically, but -z keeps the gzip layer explicit.
$ find extract -maxdepth 3 \( -type d -o -type f \) | sort extract extract/source extract/source/alpha.txt extract/source/beta.log extract/source/reports extract/source/reports/report.txt
Many archives already contain a top-level directory such as source. Extracting into a dedicated destination directory avoids mixing those restored paths with unrelated files in the current working tree.
$ mkdir -p single $ tar -xvzf source.tar.gz -C single source/reports/report.txt source/reports/report.txt
Replace source/reports/report.txt with the exact member name from the earlier tar -tzf listing.
$ find single -maxdepth 4 \( -type d -o -type f \) | sort single single/source single/source/reports single/source/reports/report.txt