Using compressed archives in .tar.gz format keeps software releases, configuration backups, and log bundles together while reducing disk usage and transfer time on Linux systems. Extracting these archives reliably restores the original directory tree, which is essential for reproducible builds and disaster recovery.
A .tar.gz file combines the tar archiver with gzip compression: tar groups many files and directories into a single stream, then gzip compresses that stream into a smaller file. On most Linux distributions, the tar command understands this combination natively, so a single invocation can both decompress and unpack the archive using options such as –extract, –gzip, and –file.
Extraction usually runs as an unprivileged user, but unpacking into system paths or overwriting existing data can still cause problems if the wrong directory is chosen. Checking the archive type, previewing its contents, and extracting into a dedicated target folder reduce the risk of accidental overwrites and make it easier to verify that the expected files are present.
Steps to extract .tar.gz files in Linux:
- Open a terminal on the Linux system.
$ whoami root
- Change to the directory that contains the .tar.gz archive.
$ cd /root/sg-work/archives $ ls destination single.txt.xz source source.tar.bz2 source.tar.gz source.tar.xz target target-xz
- Confirm that the file is a gzip-compressed archive (optional).
$ file source.tar.gz source.tar.gz: gzip compressed data, from Unix, original size modulo 2^32 10240
Output mentioning gzip compressed data indicates a standard .tar.gz or .tgz archive.
- Preview the contents of the archive without extracting them (optional).
$ tar --list --gzip --file=source.tar.gz source/ source/reports/ source/reports/report.txt source/beta.log source/alpha.txt
Short option form for listing is tar -tzf archive.tar.gz, where t lists, z enables gzip, and f specifies the archive file.
- Create a dedicated directory where the files will be unpacked (optional).
$ mkdir -p target
Using a separate directory keeps extracted files grouped and avoids mixing them with unrelated data.
- Extract the .tar.gz archive into the target directory.
$ tar --extract --gzip --verbose --file=source.tar.gz --directory=target source/ source/reports/ source/reports/report.txt source/beta.log source/alpha.txt
Options for tar.
$ tar --help Usage: tar [OPTION...] [FILE]... GNU 'tar' saves many files together into a single tape or disk archive, and can restore individual files from the archive. Examples: tar -cf archive.tar foo bar # Create archive.tar from files foo and bar. tar -tvf archive.tar # List all files in archive.tar verbosely. tar -xf archive.tar # Extract all files from archive.tar. Main operation mode: -A, --catenate, --concatenate append tar files to an archive -c, --create create a new archive
- Verify that the expected files were unpacked into the target directory.
$ ls -R target target: source target/source: alpha.txt beta.log reports target/source/reports: report.txt
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
