How to untar a file
Categories:
How to Untar a File: A Comprehensive Guide to Extracting Tar Archives

Learn the essential commands and techniques for extracting files from .tar, .tar.gz, .tgz, .tar.bz2, and .tar.xz archives on Linux and macOS systems.
The tar
command (short for tape archive) is a fundamental utility in Unix-like operating systems for creating and extracting archive files. While often associated with backup tapes, tar
is widely used today to bundle multiple files and directories into a single archive file, making them easier to transfer or store. These archives are commonly compressed using tools like gzip
, bzip2
, or xz
to save space. This article will guide you through the process of untarring (extracting) various types of tar
archives.
Understanding Tar Archives and Compression
Before diving into extraction, it's helpful to understand the different types of tar
archives you might encounter. A plain .tar
file is simply an archive of files and directories, without any compression. However, to save disk space and bandwidth, tar
archives are frequently compressed using external utilities. The most common compression formats are:
- gzip (.tar.gz or .tgz): The most common compression format, offering a good balance of compression ratio and speed.
- bzip2 (.tar.bz2 or .tbz): Generally provides better compression than gzip, but is slower.
- xz (.tar.xz or .txz): Offers the best compression ratio among the three, but is the slowest to compress and decompress.
flowchart TD A[Tar Archive] --> B{Compressed?} B -->|No| C[Plain .tar] B -->|Yes| D{Compression Type?} D -->|gzip| E[tar.gz / tgz] D -->|bzip2| F[tar.bz2 / tbz] D -->|xz| G[tar.xz / txz] C --> H[Extract with `tar -xf`] E --> I[Extract with `tar -xzf`] F --> J[Extract with `tar -xjf`] G --> K[Extract with `tar -xJf`] H & I & J & K --> L[Extracted Files]
Flowchart illustrating different tar archive types and their extraction commands.
Basic Tar Extraction Commands
The tar
command uses various options to control its behavior. For extraction, the primary options are:
-x
: Extract files from an archive.-f
: Specify the archive file name. This option must be followed by the archive's filename.-v
: Verbose output, showing the files being extracted (optional, but often useful).-C
: Change to a specified directory before performing the operation (e.g.,-C /path/to/destination
).
When dealing with compressed archives, you'll add another option corresponding to the compression type:
# Extract a plain .tar file
tar -xf archive.tar
# Extract a .tar.gz or .tgz file
tar -xzf archive.tar.gz
# Extract a .tar.bz2 or .tbz file
tar -xjf archive.tar.bz2
# Extract a .tar.xz or .txz file
tar -xJf archive.tar.xz
Common tar
extraction commands for different archive types.
tar
are often smart enough to detect the compression type automatically. You can frequently use tar -xf
for all compressed types, and it will figure out the correct decompression utility. However, explicitly specifying the compression flag (-z
, -j
, -J
) is good practice for clarity and compatibility with older systems.Advanced Extraction Options
Beyond basic extraction, tar
offers several powerful options for more granular control over the process.
# Extract to a specific directory
tar -xzf archive.tar.gz -C /path/to/destination/
# List contents of an archive without extracting
tar -tf archive.tar.gz
# Extract only specific files or directories
tar -xzf archive.tar.gz file_to_extract.txt directory_to_extract/
# Exclude specific files/directories during extraction (less common for extraction, more for creation)
tar -xzf archive.tar.gz --exclude='*.log' --exclude='temp_dir/'
Advanced tar
commands for specific extraction scenarios.
1. Step 1: Navigate to the Archive's Location
Open your terminal and use the cd
command to navigate to the directory where your .tar
archive is located. This makes it easier to reference the file.
2. Step 2: Identify the Archive Type
Examine the file extension of your archive. This will tell you if it's a plain .tar
, .tar.gz
, .tar.bz2
, or .tar.xz
file. For example, ls -l myarchive.*
will show the full filename.
3. Step 3: Choose the Correct Extraction Command
Based on the archive type, select the appropriate tar
command. For .tar.gz
, use tar -xzf
. For .tar.bz2
, use tar -xjf
. For .tar.xz
, use tar -xJf
. For plain .tar
, use tar -xf
.
4. Step 4: Execute the Extraction
Run the chosen command, replacing archive.tar.gz
with your actual filename. For example: tar -xzf myarchive.tar.gz
. If you want to see the files being extracted, add the -v
option: tar -xvzf myarchive.tar.gz
.
5. Step 5: Verify Extracted Files (Optional)
After extraction, use ls
or ls -l
to list the contents of the current directory or the specified destination directory (-C
) to confirm that your files have been successfully extracted.