Extracting files from a tar archive in Linux requires a simple command that decompresses and unpacks the data. If you are searching for how to untar file in linux, you have come to the right place. This guide will walk you through every step, from basic commands to advanced options, ensuring you can handle any tar archive with confidence.
Tar files are everywhere in Linux. They bundle multiple files into one archive, often with compression. Understanding how to extract them is a fundamental skill for any Linux user. Let’s start with the basics and build up from there.
Understanding Tar Archives
Before we dive into commands, it helps to know what a tar file actually is. The name “tar” stands for Tape Archive, a format from the early days of Unix. It was designed to store files on magnetic tape, but now it’s used for distribution and backup.
A tar archive can be compressed or uncompressed. Common extensions include:
- .tar – Uncompressed archive
- .tar.gz or .tgz – Compressed with gzip
- .tar.bz2 or .tbz2 – Compressed with bzip2
- .tar.xz – Compressed with xz
Each compression method requires a slightly different flag in the extraction command. But don’t worry—the tar command is smart enough to detect the compression type automatically in most cases.
How To Untar File In Linux
Now let’s get to the main event. The most common command to extract a tar file is:
tar -xvf filename.tar
Here’s what each flag means:
- -x : Extract files from the archive
- -v : Verbose mode (shows files being extracted)
- -f : Specify the archive file name
For compressed archives, you add the appropriate decompression flag:
- -z for gzip (.tar.gz or .tgz)
- -j for bzip2 (.tar.bz2)
- -J for xz (.tar.xz)
So for a .tar.gz file, the command becomes:
tar -xzvf archive.tar.gz
And for .tar.bz2:
tar -xjvf archive.tar.bz2
For .tar.xz:
tar -xJvf archive.tar.xz
However, modern versions of tar can auto-detect the compression. You can simply use:
tar -xvf archive.tar.gz
And it will work. The auto-detection is based on the file extension. This makes the command much simpler to remember.
Step-By-Step Extraction Process
Let’s walk through a real example. Suppose you downloaded a file called project.tar.gz and want to extract it to your current directory.
- Open your terminal.
- Navigate to the directory containing the file:
cd /path/to/file - Run the extraction command:
tar -xzvf project.tar.gz - Watch the files appear as they are extracted.
- Verify the contents with
ls -l.
Thats it. You have successfully untarred the file. The files will be placed in the current working directory, unless the archive itself contains a folder structure.
Extracting To A Specific Directory
Sometimes you want to extract files to a specific location rather than the current directory. Use the -C flag followed by the target directory path.
tar -xzvf archive.tar.gz -C /path/to/destination
For example, to extract to your Desktop:
tar -xzvf archive.tar.gz -C ~/Desktop
This is very usefull when you want to keep your home directory organized. The destination folder must exist before running the command, or tar will throw an error.
Listing Archive Contents Without Extracting
Before extracting, you might want to see what’s inside the archive. Use the -t flag (list) instead of -x (extract).
tar -tvf archive.tar.gz
This shows all files and directories inside the archive, along with their sizes and permissions. It’s a great way to check if the archive contains a top-level folder or just a bunch of files.
Extracting Specific Files Or Folders
You don’t have to extract the entire archive. You can extract only specific files or directories by listing them at the end of the command.
tar -xzvf archive.tar.gz file1.txt folder2/
This extracts only file1.txt and the entire folder2 directory. The paths must match exactly what appears in the archive listing.
To extract multiple files, just add them separated by spaces:
tar -xzvf archive.tar.gz file1.txt file2.txt folder3/
This is handy when you only need a few items from a large archive.
Excluding Files During Extraction
What if you want to extract everything except certain files? Use the –exclude option.
tar -xzvf archive.tar.gz --exclude='*.log' --exclude='temp/'
This extracts all files except those ending in .log and the temp directory. You can use multiple –exclude patterns. Note that the patterns are matched against the full path inside the archive.
Handling Large Archives With Progress
When extracting very large files, you might want to see progress. The -v flag shows each file as it’s extracted, but that can be overwhelming. Use the –checkpoint option to show progress at intervals.
tar -xzvf huge_archive.tar.gz --checkpoint=1000
This prints a checkpoint message every 1000 records. For a more visual progress bar, you can pipe the output to pv (Pipe Viewer) if installed:
tar -xzvf huge_archive.tar.gz | pv -s $(du -sb huge_archive.tar.gz | awk '{print $1}')
This shows a real-time progress bar with speed and ETA.
Common Tar File Extensions And Their Commands
Here’s a quick reference table for different tar file types:
| Extension | Command |
|---|---|
| .tar | tar -xvf file.tar |
| .tar.gz or .tgz | tar -xzvf file.tar.gz |
| .tar.bz2 or .tbz2 | tar -xjvf file.tar.bz2 |
| .tar.xz | tar -xJvf file.tar.xz |
Remember, modern tar auto-detects compression, so tar -xvf file.tar.gz works fine. But knowing the flags helps when auto-detection fails or when writing scripts.
Using Wildcards With Tar
You can use wildcards to match patterns when extracting. For example, to extract all .txt files from an archive:
tar -xzvf archive.tar.gz --wildcards '*.txt'
The –wildcards flag enables pattern matching. Without it, tar treats the pattern as a literal filename. This is usefull for selective extraction based on file types.
Preserving Permissions And Ownership
By default, tar preserves file permissions. But if you need to preserve ownership (requires root), use the –same-owner flag.
sudo tar -xzvf archive.tar.gz --same-owner
This is important when restoring backups or system files. Without root, tar will set the owner to the current user.
Extracting To Stdout
Sometimes you want to see the content of a file without extracting it to disk. Use the -O flag (capital O) to extract to standard output.
tar -xzvf archive.tar.gz file.txt -O
This prints the contents of file.txt to the terminal. You can pipe it to other commands like grep or less.
Troubleshooting Common Issues
Even experienced users run into problems. Here are some common errors and how to fix them.
Error: “Cannot Open: No Such File Or Directory”
This usually means the archive file doesn’t exist in the current directory. Double-check the path and filename. Use ls to list files.
Error: “Gzip: Stdin: Not In Gzip Format”
This happens when you use the wrong decompression flag. The file might not be gzip compressed, or the extension is wrong. Try using file command to check the actual format:
file archive.tar.gz
Then use the correct flag based on the output.
Error: “Cannot Mkdir: Permission Denied”
You don’t have write permission to the destination directory. Use sudo or change to a directory where you have write access.
Error: “Unexpected EOF In Archive”
The archive is corrupt or incomplete. Re-download the file and try again. You can also try tar -xvf with –ignore-zeros to skip corrupted parts.
Advanced Tar Extraction Techniques
Once you master the basics, these advanced techniques will make you a tar pro.
Extracting Multiple Archives At Once
You can extract multiple tar files with a single command using a loop:
for f in *.tar.gz; do tar -xzvf "$f"; done
This extracts all .tar.gz files in the current directory. Be careful—this can flood your directory with files.
Using Tar With Find
Combine find with tar to extract archives in subdirectories:
find . -name "*.tar.gz" -exec tar -xzvf {} \;
This finds all .tar.gz files recursively and extracts them in place.
Extracting To A Different Filesystem
When extracting to a different filesystem, use –xattrs to preserve extended attributes:
tar -xzvf archive.tar.gz --xattrs -C /mnt/usb
This is usefull when backing up to external drives.
Checking Archive Integrity
Before extracting, you can check if the archive is valid:
tar -tzf archive.tar.gz > /dev/null
If the command exits without error, the archive is intact. Redirecting to /dev/null suppresses the file list.
Frequently Asked Questions
What Is The Difference Between Tar And Gzip?
Tar is an archiving tool that bundles files together. Gzip is a compression tool that reduces file size. They are often used together: tar creates the archive, then gzip compresses it. The result is a .tar.gz file.
How Do I Untar A File Without Verbose Output?
Simply omit the -v flag. Use tar -xf archive.tar.gz. The files will be extracted silently.
Can I Untar A File To A Different Directory Without Changing My Current Location?
Yes, use the -C option. For example: tar -xzf archive.tar.gz -C /target/directory. Your current working directory remains unchanged.
How Do I Untar A File That Has Spaces In Its Name?
Enclose the filename in quotes or escape the spaces with backslashes. For example: tar -xzf “my archive.tar.gz” or tar -xzf my\ archive.tar.gz.
What If I Forget The Exact Command To Untar A File?
Use the –help flag: tar –help. Or check the manual with man tar. Most systems also have info tar for detailed documentation.
Putting It All Together
By now, you should feel confident about how to untar file in linux. The process is straightforward once you understand the flags and options. Remember these key points:
- Use tar -xvf for basic extraction
- Add compression flags (-z, -j, -J) for compressed archives
- Use -C to extract to a specific directory
- Use -t to list contents without extracting
- Use –exclude to skip certain files
Practice with different archive types to build muscle memory. Soon, extracting tar files will become second nature. Whether you’re installing software, restoring backups, or just unpacking a download, the tar command is your reliable tool.
If you ever get stuck, remember that the man page is your friend. And don’t be afraid to experiment in a test directory. Linux is forgiving—you can always delete extracted files and try again.
Now go ahead and untar that file. You’ve got all the knowledge you need. Happy extracting!