Learn how to untar, extract, decompress, or open tar files and tar.gz archives in Linux or Unix. Whether you're managing software packages, backups, or downloaded content, mastering Linux tar commands is essential.
Quick Tar Commands at a Glance
Use these essential commands for extracting and managing tar archives in Linux or Unix:
Archive Type | Tar Command | Notes |
---|---|---|
.tar | tar -xvf file_name.tar |
Extracts files from an uncompressed tar archive |
.tar.gz .tgz |
tar -zxvf file_name.tar.gz tar -zxvf file_name.tgz |
Extracts files from a gzip-compressed tar archive |
Any | tar -C /destination/path -xvf file_name.tar |
Extract to a specific directory |
What is a Tar File?
tar
(short for tape archive) is a standard Linux and Unix command utility for bundling multiple files or directories into a single archive.
- .tar, uncompressed archive
- .tar.gz or .tgz, compressed with gzip for smaller file size
Tar files are commonly used for software distribution, backups, and file transfers.
How to Create a Tar.gz Archive
To package a folder into a compressed archive:
tar -czvf archive_name.tar.gz folder_name
Options explained:
Option | Description |
---|---|
-c | Create a new archive |
-z | Compress with gzip |
-v | Verbose output, shows files being added |
-f | Specify archive filename |
--exclude="path" | Exclude files or directories |
Example:
tar -czvf archive_name.tar.gz folder_name --exclude="folder_name/exclude_this"
How to Untar Files in Linux
"Untar" means extracting files from a tar archive using the tar
command.
Extract a .tar File
tar -xvf file_name.tar
-x
, extract files-v
, verbose-f
, specify archive file
Extract a .tar.gz or .tgz File
tar -zxvf file_name.tar.gz
tar -zxvf file_name.tgz
-z
, decompress gzip- Use
-C /path/to/destination
to extract elsewhere
Preview Contents Before Extracting
tar -tvf file_name.tar.gz
-t
, list files-v
, verbose-f
, specify archive
Why preview first? Prevents clutter, saves time, and keeps workflows organized.
Adding Files to an Existing Tar Archive
tar -rvf archive_name.tar newfile.txt
⚠️ Tip: Avoid appending directly to compressed .tar.gz
; decompress first if needed.
.tar vs .tar.gz: When to Use Each
Archive Type | Pros | Cons |
---|---|---|
.tar | Faster extraction, uncompressed, good for local backups | Large file size |
.tar.gz .tgz |
Compressed, smaller file size, good for downloads/transfers | Slower extraction due to decompression |
Common Tar Synonyms
- Untar = Extract, decompress, open, unpack
- Tarball = Archive file (.tar, .tar.gz)
- Compress/Decompress = gzip, zip, unpack
FAQ: Tar & Untar in Linux
How do I extract a tar.gz file?
Open terminal and run: tar -zxvf filename.tar.gz
How can I view contents without extracting?
Use: tar -tvf filename.tar
or tar -tzvf filename.tar.gz
What does -z do?
Enables gzip compression/decompression
Can I compress multiple directories into one tar.gz?
Yes: tar -czvf archive.tar.gz folder1 folder2 folder3
How to extract to another directory?
Use -C
: tar -C /target/path -zxvf file.tar.gz
Final Thoughts on Linux Tar
With just a few tar
commands, you can easily create, untar, decompress, and manage tar.gz archives in Linux or Unix. Using this guide, you can handle archives efficiently, avoid clutter, and keep your workflows organized.