Tar files are common in Linux for packaging, and untarring them is a fundamental skill for managing compressed data. If you are new to Linux or just need a refresher on how to untar in linux, this guide covers everything from basic commands to advanced options. You will learn to extract tar archives quickly, handle different compression formats, and avoid common mistakes.
Tar stands for “tape archive,” a format that bundles multiple files into one archive. Untarring means extracting those files. Linux offers several tools for this, but the tar command is the most common. Let’s start with the basics.
How To Untar In Linux
Untarring a tar file is straightforward. The basic syntax is tar -xvf file.tar. The -x flag extracts, -v shows verbose output, and -f specifies the file name. For example, to extract archive.tar, run:
tar -xvf archive.tar
This command extracts all files into the current directory. If you want to extract to a specific folder, use the -C option:
tar -xvf archive.tar -C /path/to/directory
Now, let’s break down common tar file types and how to handle them.
Common Tar File Extensions
- .tar – Uncompressed tar archive
- .tar.gz or .tgz – Gzip compressed
- .tar.bz2 or .tbz2 – Bzip2 compressed
- .tar.xz – XZ compressed
- .tar.zst – Zstandard compressed
For compressed archives, you often need to add a decompression flag. But modern tar can auto-detect the compression type. So tar -xvf file.tar.gz works without extra flags.
Step-By-Step: Untarring Different Formats
1. Untar a .tar.gz File
Gzip is the most common compression. Use:
tar -xzvf file.tar.gz
The -z flag tells tar to use gzip decompression. But as mentioned, you can omit it if your tar version supports auto-detection.
2. Untar a .tar.bz2 File
Bzip2 offers better compression but slower speed. Use:
tar -xjvf file.tar.bz2
Or simply:
tar -xvf file.tar.bz2
3. Untar a .tar.xz File
XZ provides high compression ratios. Use:
tar -xJvf file.tar.xz
Note the capital -J flag for XZ.
4. Untar a .tar.zst File
Zstandard is newer and fast. Use:
tar --zstd -xvf file.tar.zst
You may need to install zstd first: sudo apt install zstd.
Useful Tar Options
-t– List contents without extracting-v– Verbose, shows files as they are extracted-C– Extract to a specific directory--exclude– Skip certain files or patterns--strip-components=N– Remove leading path components
How To List Contents Of A Tar File
Before extracting, you might want to see what’s inside. Use:
tar -tvf file.tar
This lists all files and directories. For compressed archives, add the appropriate flag or let tar auto-detect.
Extracting Specific Files From A Tar Archive
You can extract only certain files. For example:
tar -xvf archive.tar file1.txt file2.txt
To extract a directory:
tar -xvf archive.tar myfolder/
Use wildcards with --wildcards:
tar -xvf archive.tar --wildcards '*.txt'
Handling Large Tar Files
For large archives, use -v sparingly to avoid cluttering the terminal. Also, consider using pv (pipe viewer) to monitor progress:
pv file.tar.gz | tar -xz
Install pv with sudo apt install pv.
Common Errors And Fixes
- “Cannot open: No such file” – Check the file path or name.
- “Not a gzip file” – The file may be corrupted or not compressed as expected. Use
filecommand to check type. - “Permission denied” – You may need
sudoor change directory permissions. - “Unexpected EOF” – The archive is incomplete. Re-download it.
Using Tar With Pipes
Tar can work with pipes for streaming. For example, to extract a remote archive:
wget -qO- http://example.com/file.tar.gz | tar -xz
Or to compress and send over SSH:
tar -czf - /local/dir | ssh user@remote "tar -xzf - -C /remote/dir"
Graphical Alternatives
If you prefer GUI, file managers like Nautilus or Dolphin can extract tar files. Right-click and select “Extract Here.” But for scripting and efficiency, the command line is best.
Advanced: Creating Tar Archives
While this guide focuses on untarring, knowing how to create archives helps. Use:
tar -cvf archive.tar /path/to/dir
Add compression flags as needed.
Automating Untarring With Scripts
You can write a simple bash script to extract multiple archives:
#!/bin/bash
for file in *.tar.gz; do
tar -xzf "$file" -C /target/dir
done
Make it executable with chmod +x script.sh.
Security Considerations
Be cautious when extracting archives from untrusted sources. Tar files can contain absolute paths or symlinks that overwrite system files. Use --no-overwrite-dir or extract to a temporary directory first.
Performance Tips
- Use
pigz(parallel gzip) for faster decompression:tar -xzf file.tar.gz --use-compress-program=pigz - For large archives, consider
lbzip2for bzip2. - Use
--checkpointto show progress:tar -xvf file.tar --checkpoint=1000
Frequently Asked Questions
What is the difference between tar and zip?
Tar is common on Linux for archiving without compression by default, while zip compresses each file individually. Tar with compression (e.g., tar.gz) is more efficient for many small files.
How do I untar a file in Linux without the tar command?
You can use gunzip for .gz files, bunzip2 for .bz2, or unxz for .xz, but they only decompress, not extract the tar archive. You still need tar or tools like 7z.
Can I untar a file in Linux using a single command?
Yes, tar -xvf file.tar works for most formats. For compressed ones, add the appropriate flag or let auto-detection handle it.
Why do I get “tar: Error is not recoverable: exiting now”?
This usually means the archive is corrupted or the wrong decompression method was used. Check the file type with file and re-download if needed.
How to untar a file in Linux to a different directory?
Use the -C option: tar -xvf file.tar -C /target/path. Ensure the target directory exists.
Conclusion
Mastering how to untar in linux is essential for anyone working with Linux systems. Whether you are extracting software packages, backups, or data archives, the tar command is your go-to tool. Remember the basic flags: -x to extract, -v for verbosity, -f for file, and -C for target directory. Practice with different compression types, and you will handle any tar file with ease. For further reading, check the man page (man tar) or explore online tutorials. Now go ahead and untar those archives!