How to Extract Compressed Files using the tar Command in Linux
Hey there, and welcome to this hands-on Linux tutorial from ProgramGuru.org! 👋
Today, we're going to walk through one of the most common tasks every Linux user faces — extracting compressed files using the tar command.
We’ll look at how to handle different archive formats like:
.tar– just an archive, no compression.tar.gzor.tgz– archive + gzip compression.tar.bz2– archive + bzip2 compression
Let’s get started!
🛠️ 1. Extracting a .tar file
Imagine you’ve downloaded a file called project.tar. This file is just a collection of files bundled together without compression.
To extract it, run:
tar -xvf project.tar
Let’s break it down:
-xmeans extract-vstands for verbose (shows progress)-fspecifies the file name to work with
Here's what the output might look like:
file1.txt
file2.txt
images/
images/logo.png
🛠️ 2. Extracting a .tar.gz or .tgz file
These files are archived and compressed using gzip. The command is just as simple:
tar -xvzf project.tar.gz
Or if the file is named project.tgz:
tar -xvzf project.tgz
Here’s the output:
project/
project/main.cpp
project/README.md
And again, here’s what the options mean:
-ztellstarto use gzip-xvzfis the usual combo to extract and see progress
🛠️ 3. Extracting a .tar.bz2 file
If your file uses bzip2 compression, like project.tar.bz2, you’ll need to use the -j flag:
tar -xvjf project.tar.bz2
That tells tar to use bzip2 while extracting.
project/
project/index.html
project/style.css
📁 Optional: Extract to a Specific Directory
Want to extract files to a custom folder? Use the -C option:
tar -xvzf project.tar.gz -C /home/yourname/projects/
✅ Summary
Here’s a quick cheat sheet:
tar -xvf file.tar– extract .tartar -xvzf file.tar.gz– extract .tar.gz or .tgztar -xvjf file.tar.bz2– extract .tar.bz2
And that’s it! You've now mastered the basics of extracting compressed files using tar in Linux.
Thanks for learning with us at ProgramGuru.org. If this was helpful, be sure to check out our other beginner-friendly Linux guides. 🚀
