Hello fellow Linux enthusiasts! Today, let’s dive into one of our most reliable and often underappreciated tools in the Linux toolkit: the tar
command. Whether you’re a seasoned sysadmin or a Linux hobbyist, understanding how to efficiently use tar
for handling folders can be a real game-changer. So, grab your favorite beverage, and let’s get started on this journey together!
What’s tar
and Why Should You Care?
tar
, short for Tape Archive, is more than just a command; it’s a staple in the Linux world. It allows us to bundle up a bunch of files and directories into one neat package, known as a tarball. Think of it like a digital Swiss Army knife for your files and directories!
The Basics of tar
The general syntax of tar
is pretty straightforward:
tar [options] [archive-file] [what to tar]
Here:
[options]
telltar
what you want it to do.[archive-file]
is the resulting tarball.[what to tar]
are the files or directories you’re wrapping up.
Creating Your First Tarball
Packing Up a Single Folder
Let’s say you have a folder named Photos
that you want to archive. Here’s how you do it:
tar -cvf photos.tar ~/Photos
This command breaks down as:
-c
for create,-v
for verbose (so you see what’s happening),-f
for file, followed by the name of your tarball.
Wrapping Multiple Folders Together
What if you want to archive both Photos
and Documents
? Just list them:
tar -cvf my_files.tar ~/Photos ~/Documents
Adding Some Squeeze with Compression
To save space, let’s add compression. For gzip compression, just add a z
:
tar -czvf photos.tar.gz ~/Photos
And for bzip2 compression, switch that to a j
:
tar -cjvf photos.tar.bz2 ~/Photos
Unboxing: Extracting Tarballs
To open up a tarball and get your files back, use:
tar -xvf photos.tar
tar
is smart enough to figure out if it’s gzipped or bzip2-compressed.
Some Cool tar
Tricks
Peek Inside a Tarball
Curious about what’s inside a tarball without opening it? Use:
tar -tf photos.tar
Keep Out the Unwanted
To exclude files when creating a tarball, like those pesky temp files, use --exclude
:
tar -cvf archive.tar ~/Documents --exclude='*.tmp'
Incremental Backups for the Win
tar
is also great for backups. To make an incremental backup:
tar -cvf backup.tar --listed-incremental=snapshot.file ~/Documents
This creates a record of what’s backed up, handy for the next backup.
Wrapping Up
And there you have it! tar
isn’t just about squashing files into a smaller space. It’s about organizing, securing, and managing our digital lives with ease. Remember, the best way to learn is by doing. So, open up your terminal and start playing around with tar
. Who knows what you’ll discover!
Until next time, happy tarring! 🐧💻
Hi, I am Himadri Das, I am a blogger, and an open source contributor. I have about 11 years of experience in the Information Technology domain. Currently I am working in a Startup Company as Quality Assurance Manager. I have hands-on experience on Appium, Selenium, QTP, Locust, Automation framework, Performance Testing, Functional Testing, Java, python, Shell scripting, MySql, Redis, Kafka etc. Apart from my work and writing blogs, I love to play guitar, love to travel and love to watch cricket and football. If you want to know more about me, please visit my linkedin profile .