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 user
- Change to the directory that contains the .tar.gz archive.
$ cd ~/Downloads $ ls archive.tar.gz
- Confirm that the file is a gzip-compressed archive (optional).
$ file archive.tar.gz archive.tar.gz: gzip compressed data, last modified: Wed Aug 21 08:36:18 2019, from Unix, original size 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=archive.tar.gz folder/ folder/sub-01/ folder/sub-01/file-01 folder/sub-01/file-02 folder/sub-02/ folder/sub-02/file-01 folder/sub-02/file-02
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=archive.tar.gz --directory=target folder/ folder/sub-01/ folder/sub-01/file-01 folder/sub-01/file-02 folder/sub-02/ folder/sub-02/file-01 folder/sub-02/file-02
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. ##### snipped #####
- Verify that the expected files were unpacked into the target directory.
$ ls -R target target: folder target/folder: sub-01 sub-02 target/folder/sub-01: file-01 file-02 target/folder/sub-02: file-01 file-02
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.
Comment anonymously. Login not required.
