Tar files in Linux compress multiple files into one, and untarring them extracts the contents for individual use. If you are wondering how to untar a file in linux, you have come to the right place. This guide walks you through every step with clear commands and examples.
Tar files are common in Linux. They bundle files together, often with compression. Untarring is the process of extracting them. It is a basic skill for any Linux user.
Understanding Tar Files
A tar file is an archive. It combines many files into a single file. The name comes from “tape archive.” Tar files often end with .tar. They can also have compression extensions like .gz, .bz2, or .xz.
When you see a file like archive.tar.gz, it means the tar archive is compressed with gzip. Untarring extracts the original files from this archive.
Common Tar File Extensions
.tar– Uncompressed tar archive.tar.gzor.tgz– Compressed with gzip.tar.bz2or.tbz2– Compressed with bzip2.tar.xz– Compressed with xz
How To Untar A File In Linux
The main command for untarring is tar. You use it with different options depending on the file type. The basic syntax is:
tar -xvf filename.tar
Let us break down the options:
-x– Extract files from the archive-v– Verbose mode, shows files being extracted-f– Specifies the archive file name
Untarring A .Tar File
For a simple .tar file, run:
tar -xvf myfiles.tar
This extracts all files into the current directory. If you want to extract to a specific location, use the -C option:
tar -xvf myfiles.tar -C /path/to/destination
Untarring A .Tar.gz Or .Tgz File
For gzip-compressed tar files, add the -z option:
tar -xzvf archive.tar.gz
Or for .tgz files:
tar -xzvf archive.tgz
Untarring A .Tar.bz2 Or .Tbz2 File
For bzip2 compression, use -j:
tar -xjvf archive.tar.bz2
For .tbz2:
tar -xjvf archive.tbz2
Untarring A .Tar.xz File
For xz compression, use -J:
tar -xJvf archive.tar.xz
Step-By-Step Guide To Untar A File
Follow these steps to untar any file in Linux. We will use a .tar.gz file as an example.
Step 1: Open The Terminal
Press Ctrl + Alt + T to open a terminal window. You can also search for “Terminal” in your applications menu.
Step 2: Navigate To The File Location
Use the cd command to go to the directory containing the tar file:
cd /path/to/your/file
For example, if the file is in your Downloads folder:
cd ~/Downloads
Step 3: Check The File Type
Use file command to confirm the compression:
file myarchive.tar.gz
This shows you the file format, so you know which options to use.
Step 4: Run The Untar Command
For a .tar.gz file, type:
tar -xzvf myarchive.tar.gz
Press Enter. The files will extract to the current directory.
Step 5: Verify The Extraction
List the files to confirm:
ls -l
You should see the extracted files or folders.
Advanced Untarring Options
The tar command has many useful options. Here are some advanced ones.
Extract Specific Files
To extract only certain files from the archive, list them after the command:
tar -xvf archive.tar file1.txt file2.txt
This extracts only file1.txt and file2.txt.
List Contents Without Extracting
Use -t to see what is inside the archive:
tar -tvf archive.tar
This shows all files without extracting them.
Extract To A Different Directory
Use -C to specify a destination:
tar -xvf archive.tar -C /target/directory
Preserve Permissions And Ownership
Add -p to keep original file permissions:
tar -xvpvf archive.tar
Extract With Progress
Use --checkpoint to see progress for large files:
tar -xvf archive.tar --checkpoint=100
This shows a dot every 100 blocks.
Common Errors And Solutions
Sometimes untarring fails. Here are common issues and fixes.
Error: “Cannot Open: No Such File Or Directory”
This means the file does not exist in the current location. Double-check the path. Use ls to list files.
Error: “Gzip: Stdin: Not In Gzip Format”
You used the wrong option. The file might not be gzip compressed. Check with file command. Use the correct option for the compression type.
Error: “Permission Denied”
You lack write permission in the current directory. Use sudo to extract to a system directory:
sudo tar -xvf archive.tar -C /system/path
Or extract to a directory where you have write access.
Error: “Tar: Exiting With Failure Status Due To Previous Errors”
This usually means a corrupt archive. Try downloading the file again. You can also try tar -xvf without compression options to see if it works.
Untarring With GUI Tools
If you prefer a graphical interface, Linux has tools for untarring.
Using File Roller (GNOME)
Right-click the tar file. Select “Extract Here” or “Extract to…” File Roller opens and extracts the files.
Using Ark (KDE)
Right-click and choose “Extract” from the menu. Ark handles most tar formats.
Using Xarchiver
Install Xarchiver if needed. Open it, navigate to the tar file, and click “Extract.”
Automating Untarring With Scripts
You can write a simple bash script to untar multiple files at once. Save this as untar_all.sh:
#!/bin/bash
for file in *.tar.gz; do
tar -xzvf "$file"
done
Make it executable:
chmod +x untar_all.sh
Run it:
./untar_all.sh
This extracts all .tar.gz files in the current directory.
Untarring On Different Linux Distributions
The tar command works the same on all distributions. However, some may have different default tools.
Ubuntu And Debian
Pre-installed with tar. Use the commands above. For GUI, File Roller is default.
Fedora And Red Hat
Same tar command. GUI tool is usually Ark or File Roller depending on desktop.
Arch Linux
Standard tar command. Arch users often prefer command line.
Security Considerations
Be careful when untarring files from unknown sources. Tar files can contain malicious scripts or files with unexpected names.
Check Before Extracting
List the contents first:
tar -tvf suspicious.tar.gz
Look for files with strange names or paths like ../ that might overwrite system files.
Extract In A Safe Directory
Always extract to a temporary directory first. Use -C to specify a safe location.
Use –No-same-permissions
To avoid preserving dangerous permissions, add --no-same-permissions:
tar -xvf archive.tar --no-same-permissions
Performance Tips For Large Tar Files
Untarring large archives can take time. Here are tips to speed it up.
Use Faster Compression
If you create tar files, use faster compression like gzip instead of xz. Extraction is quicker.
Extract To An SSD
Solid-state drives are faster than hard drives. Extract to an SSD if possible.
Use Parallel Extraction
Some tools like pigz can parallelize decompression. Install it and use:
tar -xvf archive.tar.gz --use-compress-program=pigz
Untarring On Embedded Systems
On small devices like Raspberry Pi, tar works the same. But resources are limited.
Check Free Space
Use df -h to ensure enough disk space before extracting large files.
Use Minimal Options
Avoid verbose mode to save CPU:
tar -xf archive.tar.gz
Recovery From Corrupt Tar Files
If a tar file is damaged, you can try to recover data.
Use Tar With –Ignore-failed-read
This skips corrupt parts:
tar -xvf corrupt.tar --ignore-failed-read
Use Ddrescue
For severely damaged files, use ddrescue to copy the file first:
ddrescue corrupt.tar recovered.tar
Then try untarring the recovered file.
Comparing Tar With Other Archive Tools
Linux has other archiving tools. Here is how tar compares.
Tar Vs Zip
Zip is more common on Windows. Tar preserves Unix permissions better. Zip compresses each file individually, tar compresses the whole archive.
Tar Vs 7Z
7z offers better compression but is less standard. Tar is built into Linux.
Tar Vs Rar
Rar is proprietary. Tar is open-source and universal on Linux.
Frequently Asked Questions
What Is The Command To Untar A File In Linux?
The basic command is tar -xvf filename.tar. For compressed files, add the appropriate option: -z for gzip, -j for bzip2, -J for xz.
How Do I Untar A Tar.gz File In Linux?
Use tar -xzvf filename.tar.gz. The -z option handles gzip decompression.
Can I Untar A File Without Changing Directory?
Yes, use the -C option to specify a target directory: tar -xvf archive.tar -C /target/path.
How Do I List Contents Of A Tar File Without Extracting?
Use tar -tvf filename.tar to see the file list.
What If I Get A “Not In Gzip Format” Error?
Check the file type with file command. Use the correct compression option. The file might be uncompressed or use a different compression.
Conclusion
Now you know how to untar a file in Linux. The tar command is powerful and flexible. Practice with different file types. Always check file contents before extracting. With these skills, you can handle any tar archive on your system.
Remmeber to use the correct options for each compression type. Start with -xvf for basic extraction. Add -z, -j, or -J as needed. Use -C to extract to a specific folder. The command line gives you full control.
If you prefer a GUI, your desktop environment likely has a tool. File Roller and Ark are common choices. They handle most tar formats automatically.
Untarring is a fundamental Linux task. Master it and you will save time. Your system will thank you.