Chapter 44: Bash TAR Archive

Bash TAR Archive

tar is one of the oldest and most important commands in the entire Unix/Linux/Bash world. It’s been around since the 1970s (yes, really!) and is still used every single day in 2026 by developers, sysadmins, cloud engineers, and everyone who touches servers.

tar stands for Tape ARchiver (Back in the old days, people saved backups on magnetic tapes — tar was made to write/read those tapes.)

In modern life, tar does two main jobs:

  1. Bundles many files and folders into one single file (called an archive) → Like putting 50 books into one big cardboard box so you can carry them easily.
  2. Preserves all Unix/Linux file information perfectly: permissions, timestamps, ownership, symbolic links, special files, etc.

Important point: tar alone does NOT compress anything — it just bundles. The file usually gets bigger (because of extra metadata headers). To make it smaller, we almost always combine tar with a compressor: gzip, xz, zstd, bzip2 → creating the famous formats:

  • .tar.gz or .tgz (tar + gzip)
  • .tar.xz (tar + xz)
  • .tar.zst (tar + zstd)
  • .tar.bz2 (tar + bzip2)
  • plain .tar (no compression)

Why is tar so special in 2026?

  • It preserves Unix file attributes better than zip
  • Almost every Linux software package, source code download, backup script, Docker image layer, backup tool uses tar
  • It’s extremely reliable — you can extract a 20-year-old .tar.gz today and it still works perfectly
  • It’s the standard for server backups, website deploys, config bundles, etc.

Basic tar Commands – The Ones You’ll Use 95% of the Time

1. Create an Archive (No Compression)

Bash
  • -c = create new archive
  • -v = verbose (show every file being added — very useful)
  • -f = file name follows (the archive name)

2. Create + Compress in One Step (Most Common!)

Bash

Common shortcuts people remember:

Format Command Shortcut Full Command Example When to use
.tar.gz -z tar -czvf archive.tar.gz folder/ Everyday, fast, everywhere
.tar.xz -J tar -cJvf archive.tar.xz folder/ Maximum size saving
.tar.zst -a or –zstd tar -caf archive.tar.zst folder/ Best speed + good ratio (2026)
.tar.bz2 -j tar -cjvf archive.tar.bz2 folder/ Older systems
plain .tar (no letter) tar -cvf archive.tar folder/ When you don’t want compression

Pro tip: Always add trailing / after folder name when creating → means “contents of folder”, not the folder wrapper itself.

3. List Contents Without Extracting (-t)

Very useful — like peeking inside without unpacking.

Bash

Shows file names, permissions, sizes, dates — same as ls -l

4. Extract an Archive (-x)

Bash

Common extract shortcuts:

Format Extract Command Notes
.tar.gz tar -xzvf file.tar.gz Most common
.tar.xz tar -xJvf file.tar.xz Capital J
.tar.zst tar -xaf file.tar.zst -a = auto detect
.tar tar -xvf file.tar No compression letter

-C = change to directory before extracting (very clean!)

5. Quick Practice Session Right Now (Do This!)

Bash

Summary Table – tar Commands Cheat Sheet

Goal Command Example Shortcut Letter
Create plain tar tar -cvf archive.tar folder/ none
Create .tar.gz tar -czvf archive.tar.gz folder/ -z (gzip)
Create .tar.xz tar -cJvf archive.tar.xz folder/ -J (xz)
Create .tar.zst tar -caf archive.tar.zst folder/ -a (auto)
List contents tar -tvf archive.tar.gz -t
Extract everything tar -xzvf archive.tar.gz -x
Extract to folder tar -xzvf archive.tar.gz -C ~/dest/ -C
Extract one file tar -xzvf archive.tar.gz path/inside/file.txt

Teacher’s Golden Rules

  • Always use -v when learning — you see exactly what’s happening
  • Always use -C when extracting — keeps your current folder clean
  • Use .tar.zst or .tar.xz for best size savings in 2026
  • Use .tar.gz when you want maximum compatibility
  • Never compress already-compressed files (jpg, mp4, zip inside) — waste of time
  • For backups: add date → backup_$(date +%Y-%m-%d).tar.gz

Got it, boss? tar is the heart of Linux archiving — once you master create/list/extract, you can handle 99% of downloaded archives, backups, and source code packages.

Any part confusing? Want next: “Teacher, explain how to add files to existing tar” or “tar vs zip deep comparison” or “tar in backup scripts with cron”?

Just say — teacher is ready in Hyderabad! Keep archiving like a pro! 🐧📦📼 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *