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.

Steps to extract .tar.gz files in Linux:

  1. Change to the directory that contains the archive file and confirm the filename.
    $ cd /root/sg-work/archives
    $ ls
    source
    source.tar.gz
  2. Confirm that the file is a gzip-compressed tar archive.
    $ 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.

  3. List the archive members before extracting so the stored path names and top-level directory are visible.
    $ 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.

  4. Create a clean destination directory and extract the full archive into it.
    $ 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.

  5. Verify that the archive restored the expected directory tree under the target path.
    $ 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.

  6. Extract only one file from the archive when the full tree is not needed.
    $ 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.

  7. Confirm that only the requested file was restored.
    $ find single -maxdepth 4 \( -type d -o -type f \) | sort
    single
    single/source
    single/source/reports
    single/source/reports/report.txt