Site icon Ubuntu Free

Tar Command in Linux with Examples + Cheat Sheet

Tar command with examples cover

On Linux systems, managing multiple files and directories often requires bundling them into a single archive. This is where the tar command becomes important.

Originally designed for tape archives (hence “tar”), it is now a universal tool for creating backups, compressing files for uploads, and extracting downloaded archives.

Whether you’re using Ubuntu, Linux Mint, Pop!_OS, Fedora, or Debian, the tar command is a must-know skill.

In this tutorial, I’ll show you how to use tar for real-life tasks, like compressing a folder for email, extracting game mods, or backing up projects.

I’m using Ubuntu here, but every example works on most popular Linux distros.

Plus, you can download a free Tar command cheat sheet to keep handy, while you do that, don’t hesitate to contact us or leave a comment below.

Download the Tar Command Cheat Sheet

Get a free reference guide for mastering the tar command in Linux. Download it in multiple formats:

How to Use Tar Command in Linux

Basic Syntax

The general syntax for tar is:

tar [options] [archive-file] [file or directory]

For example, to create an archive of a folder named Documents:

tar -cvf documents.tar Documents/

Common Tar Command Examples

Create an Archive

Command:

tar -cvf project_backup.tar ~/Projects/MyWebsite/

Extract an Archive

Command:

tar -xvf downloaded_files.tar

Create a Compressed Archive (Gzip)

Command:

tar -czvf photos_backup.tar.gz ~/Pictures/Vacation/

Extract a Gzipped Archive

Command:

tar -xzvf photos_backup.tar.gz

Create a Bzip2 Compressed Archive

Command:

tar -cjvf code_archive.tar.bz2 ~/Projects/Coding/

Extract a Bzip2 Archive

Command:

tar -xjvf code_archive.tar.bz2

Create an XZ Compressed Archive

Command:

tar -cJvf music_collection.tar.xz ~/Music/

Extract an XZ Archive

Command:

tar -xJvf music_collection.tar.xz

Extract to a Specific Directory

Command:

tar -xvf game_mods.tar -C ~/Games/Mods/

Useful Tar Options

Option Description
-c Create a new archive.
-x Extract files from an archive.
-v Verbose mode (shows progress of files being processed).
-f Specifies the archive file name.
-z Compress using gzip.
-j Compress using bzip2.
-J Compress using xz.
-C Extract to a specific directory.
--exclude Exclude files or directories from the archive.

Advanced Tar Examples

Backup Home Directory Excluding Cache

tar -czvf home_backup.tar.gz /home/username --exclude="/home/username/.cache"

Create Archive with Absolute Paths Preserved

tar -cpvf full_system_backup.tar / --exclude=/proc --exclude=/tmp --exclude=/mnt

Archive Large Files in Background

tar -cvf large_video_archive.tar ~/Videos/ &

Tips & Best Practices

  1. Always verify your archive using tar -tvf archive.tar before deleting original files.
  2. For backups, prefer compression methods like xz for maximum space saving.
  3. Exclude unnecessary directories (like cache, logs) to keep archive size minimal.

Want to learn more Linux commands? Check out:

Each command post on our site comes with a FREE cheat sheet for you to download.

Exit mobile version