🐧 How to Use the tar Command in Linux (Beginner to Advanced)

🐧 How to Use the tar Command in Linux (Beginner to Advanced)
Operating System(s)

The tar command is a core tool in Linux used to archive and extract files. It’s commonly used for backups, software packages, and compressing directories into a single file.

📦 What is tar?

tar stands for Tape Archive.
It bundles multiple files into one archive file (usually .tar, .tar.gz, or .tar.bz2).

👉 Important:

  • .tar = archive only (no compression)
  • .tar.gz = compressed with gzip
  • .tar.bz2 = compressed with bzip2

✅ Basic Syntax

tar [options] archive-name.tar files

 

📁 Create a .tar Archive

tar -cvf archive.tar folder/

  • -c → create archive
  • -v → verbose (show progress)
  • -f → filename

👉 Creates archive.tar from folder/

🗜 Create a Compressed Archive (gzip)

tar -czvf archive.tar.gz folder/

  • -z → use gzip compression

🧊 Create a Compressed Archive (bzip2)

 

tar -cjvf archive.tar.bz2 folder/

  • -j → use bzip2 compression

📤 Extract Files

Extract .tar

tar -xvf archive.tar

Extract .tar.gz

tar -xzvf archive.tar.gz

Extract .tar.bz2

tar -xjvf archive.tar.bz2

  • -x → extract

📂 Extract to a Specific Folder

tar -xvf archive.tar -C /path/to/folder

👉 Use -C to choose destination

🔍 List Contents Without Extracting

tar -tvf archive.tar

👉 Shows files inside the archive

➕ Add Files to an Existing Archive

tar -rvf archive.tar newfile.txt

👉 Only works with uncompressed .tar files

❌ Extract a Single File

tar -xvf archive.tar filename.txt

🚀 Common Real-World Examples

Backup a directory

tar -czvf backup.tar.gz /home/user/

Extract downloaded software

tar -xzvf program.tar.gz

View contents before extracting

tar -tvf file.tar.gz

⚠️ Common Mistakes

  • ❌ Using wrong flag (-z for gzip, -j for bzip2)
  • ❌ Forgetting -f (required for filename)
  • ❌ Trying to append to compressed archives

🧠 Pro Tips

  • Use -C to avoid messy extractions
  • Combine with grep to search inside lists:

    tar -tvf archive.tar | grep filename

  • Use --exclude to skip files:

    tar -czvf backup.tar.gz folder/ --exclude="*.log"

👍 Quick Summary

  • -c → create
  • -x → extract
  • -v → verbose
  • -f → file
  • -z → gzip
  • -j → bzip2

🎯 Final Thoughts

The tar command is essential for managing files in Linux. Once you understand the basic flags, you can easily create backups, compress files, and extract archives like a pro.

Force Cache (Faster Mirror)

Linux Commands

Files

tar · pv · cat · tac · chmod · grep ·  diff · sed · ar · man · pushd · popd · fsck · testdisk · seq · fd · pandoc · cd · $PATH · awk · join · jq · fold · uniq · journalctl · tail · stat · ls · fstab · echo · less · chgrp · chown · rev · look · strings · type · rename · zip · unzip · mount · umount · install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · vi · nano · mkdir · du · ln · patch · convert · rclone · shred · srm · scp · gzip · chattr · cut · find · umask · wc · tr · hash  · compgen

Processes

alias · screen · top · nice · renice · progress · strace · systemd · tmux · chsh · history · at · batch · free · which · dmesg · chfn · usermod · ps · chroot · xargs · tty · pinky · lsof · vmstat · timeout · wall · yes · kill · sleep · sudo · su · time · groupadd · usermod · groups · lshw · shutdown · reboot · halt · poweroff · passwd · lscpu · crontab · date · bg · fg · pidof · nohup · pmap

Networking

netstat · ping · traceroute · ip · ss · whois · fail2ban · bmon · dig · finger · nmap · ftp · curl · wget · who · whoami ·· iptables · ssh-keygen · ufw · arping · firewalld