How to extract a .tar.gz file on UNIX
Categories:
How to Extract .tar.gz Files on UNIX Systems

Learn the essential commands and techniques for efficiently extracting compressed .tar.gz archives on various UNIX-like operating systems.
Compressed archives are a common way to package multiple files and directories into a single, smaller file. On UNIX-like systems (Linux, macOS, BSD), the .tar.gz
format is ubiquitous. This format combines two utilities: tar
(tape archive) for bundling files and gzip
for compression. Understanding how to extract these files is a fundamental skill for any UNIX user or system administrator.
Understanding .tar.gz Files
A .tar.gz
file is essentially a two-step archive. First, tar
is used to create an archive (a single file containing multiple files and directories, preserving their structure and permissions). This archive typically has a .tar
extension. Second, gzip
is used to compress this .tar
file, resulting in a .tar.gz
(or sometimes .tgz
) file. To extract it, you effectively reverse this process: decompress with gzip
and then unarchive with tar
.
flowchart TD A[Original Files/Directories] --> B["tar (Archive)"] B --> C["gzip (Compress)"] C --> D[".tar.gz File"] D --> E["gunzip (Decompress)"] E --> F[".tar File"] F --> G["tar (Unarchive)"] G --> H[Extracted Files/Directories]
The archiving and compression process for .tar.gz files.
Basic Extraction Command
The tar
command is versatile and can handle both archiving and compression/decompression. For .tar.gz
files, the most common command combines several options to decompress and extract in one go. The primary options you'll use are:
-x
: Extract files from an archive.-z
: Filter the archive throughgzip
(for.gz
compression).-v
: Verbose output, showing the files being extracted.-f
: Specify the archive file name.
tar -xzvf archive.tar.gz
The standard command to extract a .tar.gz file.
Let's break down the command: tar
is the utility, -x
tells it to extract, -z
tells it the file is gzipped, -v
makes it verbose (showing you what's happening), and -f
specifies the input file, archive.tar.gz
in this case. The order of xzvf
doesn't strictly matter, but f
must always be followed immediately by the filename.
-z
option for a .tar.gz
file, tar
might still try to decompress it automatically on modern systems, but it's best practice to include it for clarity and compatibility.Extracting to a Specific Directory
Often, you'll want to extract the contents of an archive into a directory other than your current working directory. You can achieve this using the -C
(capital C) option, followed by the target directory path.
mkdir -p /path/to/destination
tar -xzvf archive.tar.gz -C /path/to/destination
Extracting a .tar.gz file to a specified directory.
In this example, mkdir -p
ensures the destination directory exists (and creates it if it doesn't, without error if it already does). Then, tar
extracts the archive's contents directly into /path/to/destination
.
Listing Archive Contents Without Extracting
Before extracting, you might want to see what's inside the .tar.gz
file. This is useful for checking the archive's structure or verifying its contents without cluttering your filesystem. Use the -t
option instead of -x
.
tar -tzvf archive.tar.gz
Listing the contents of a .tar.gz file.
This command will display a list of all files and directories contained within archive.tar.gz
, along with their permissions, ownership, and sizes, without actually extracting them.
-t
first if unsure.Extracting Specific Files or Directories
Sometimes you don't need the entire archive, but only a few specific files or a particular subdirectory. You can specify these paths after the archive filename.
tar -xzvf archive.tar.gz path/to/specific/file.txt
tar -xzvf archive.tar.gz path/to/specific/directory/
Extracting only specific files or directories from an archive.
When specifying paths, ensure they match the paths inside the archive, which you can determine by listing the contents using tar -tzvf
.
1. Verify Archive Integrity
Before extracting, it's good practice to check if the archive is intact. While tar
will often report errors during extraction, a quick gzip -t archive.tar.gz
can sometimes catch issues with the gzip layer early.
2. List Contents
Use tar -tzvf archive.tar.gz
to preview the contents and structure of the archive. This helps you understand where files will be placed and if there's a top-level directory.
3. Choose Destination
Decide where you want the extracted files to go. If it's not the current directory, create the target directory using mkdir -p /path/to/destination
.
4. Extract
Execute the extraction command: tar -xzvf archive.tar.gz -C /path/to/destination
(or without -C
if extracting to the current directory). Monitor the verbose output for any errors.
5. Verify Extraction
After extraction, navigate to the destination directory and confirm that all expected files and directories are present and accessible.