Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

tar Command Guide for Packing, Compressing, and Unpacking Archives

tar is the Linux workhorse for bundling many files into one archive and (optionally) compressing it — almost every .tar.gz you ever unpack will go through this command.


What is tar, and Why Use It?

tar stands for tape archive — it was written in the 1980s for writing backups to tape drives, and the name stuck. Today it does the same job on disk: it collects many files and directories, records their permissions, ownership, and directory structure, and writes the whole thing into a single file. By itself that file is just an archive; tar can also pipe the result through a compressor (gzip, xz, bzip2) so the archive shrinks on the way out.

Why Learn tar?

  • It is the default Linux archive format — software you download from source, distro packages, and container images all arrive as .tar or .tar.gz.
  • One command covers the whole job — create, list, extract, add, remove: tar does all of it with a small set of flags.
  • It preserves metadata — permissions, symlinks, and empty directories survive the round trip, which cp -r does not reliably do for every case.
  • It drives the compressors — tar -z (gzip), -J (xz), -j (bzip2) call the tools in this section for you, so you rarely run them separately.

If you learn one archive command, learn tar. The three-letter pattern tar -czf (create + gzip + file) appears in more tutorials, READMEs, and install scripts than anything else on this page.


1. Create a Compressed Archive (The Everyday Form)

The command you will type most often. The flags are read left to right:

tar -czf backup.tar.gz myproject/
  • -c — create a new archive.
  • -z — filter it through zip/gzip compression (.gz).
  • -f backup.tar.gz — file: the name of the archive to write. The filename comes after the flag.
  • myproject/ — what to pack: a directory, a file, or several of them.

A plain archive with no compression (bigger file, instant to create):

tar -cf backup.tar myproject/

Compression options at a glance:

FlagCompressorResultTypical ratio
(none)—.tarnone — plain bundle
-zgzip.tar.gz~3–5× smaller; fast
-Jxz.tar.xzsmallest; slower
-jbzip2.tar.bz2between gzip and xz; older

Check the result:

ls -lh backup.tar.gz

2. Extract an Archive

Reverse of create — note the flag change from c (create) to x (extract):

tar -xzf backup.tar.gz

This unpacks everything into the current directory. To choose where it lands, add -C (change directory):

tar -xzf backup.tar.gz -C ~/restored/

The directory must already exist — tar will not create it for you.

The flag order never changes: x for extract, z/J/j for the compressor, f for the filename.

3. List the Contents Without Extracting

Sometimes you just want to peek inside before unpacking:

tar -tzf backup.tar.gz
  • -t — tlist (list the table of contents).

Output is one path per line:

myproject/
myproject/README.md
myproject/src/
myproject/src/main.c

Pipe it through grep to find one file across an archive you did not unpack:

tar -tzf backup.tar.gz | grep main

4. See What Is Inside With Details

Add -v (verbose) to list with sizes, dates, and permissions — or to watch the work being done while creating:

tar -tzvf backup.tar.gz
tar -czvf backup.tar.gz myproject/

Verbose output while creating looks like this:

myproject/
myproject/README.md
myproject/src/
myproject/src/main.c

5. Extract Only Some Files

You do not have to unpack everything. Name the paths you want:

tar -xzf backup.tar.gz myproject/src/main.c

Or exclude paths while extracting:

tar -xzf backup.tar.gz --exclude='*.log'

Excluding at creation time works the same way:

tar -czf backup.tar.gz --exclude='.git' --exclude='node_modules' myproject/

That last one is a classic: packing a project directory without the .git history or dependency folders makes tarballs dramatically smaller.

6. Add or Remove Files in an Existing Archive

Archives are not immutable — you can update them:

# Append a new file to an existing (uncompressed or gzip) archive
tar -rf backup.tar newfile.txt

# Remove a path from an archive
tar --delete -f backup.tar myproject/old.txt

Caution: with -r (append) or --delete, the archive must be plain (.tar) — you cannot append to a .tar.gz. If you are compressing, rebuild the archive from scratch instead; it is one command and always correct.

7. Preserve What Matters (Permissions and Ownership)

tar records file metadata by default. When extracting as a normal user you may want it preserved explicitly:

sudo tar -xpvzf backup.tar.gz -C /opt/
  • -p — preserve permissions (extract as the recorded owner).
  • -v — verbose, so you can see what is happening.

Running with sudo when extracting to system locations is normal — this is how many software packages install themselves:

# A common real-world pattern: unpack a source release into /opt
sudo tar -xzf mysoftware-1.0.tar.gz -C /opt/

Common Pitfalls

  • Putting the filename before the flags — tar backup.tar.gz -czf fails; tar -czf backup.tar.gz myproject/ works. The archive name belongs to -f and comes right after it.
  • Forgetting -f — without it, tar writes to standard output (raw bytes) instead of a file, and you get a wall of garbage in the terminal.
  • Extracting into the wrong place — tar unpacks into the current directory unless you give -C. Run pwd first if you are not sure where you are.
  • Mismatched compressor flag — -z on a .tar.xz fails with “not in gzip format”. Match the flag to the extension: -z ↔ .gz, -J ↔ .xz, -j ↔ .bz2.
  • Extracting an archive whose paths start with / — some archives contain absolute paths and would overwrite system files. List first with -t, and extract into a scratch directory if you are unsure.
  • Expecting -r to work on compressed archives — appending only works on plain .tar files; rebuild instead.
PageRelationship
gzipThe compressor -z calls; also compresses single files on its own
zipThe cross-platform alternative archive format
xzThe -J compressor; best ratio for .tar.xz
bzip2The -j compressor; older .tar.bz2 files

tar Cheatsheet

CommandWhat it does
tar -czf backup.tar.gz dir/Create a gzip-compressed archive
tar -cf backup.tar dir/Create a plain (uncompressed) archive
tar -xzf backup.tar.gzExtract a .tar.gz into the current directory
tar -xzf backup.tar.gz -C /path/Extract into a specific directory
tar -tzf backup.tar.gzList the contents without extracting
tar -tzvf backup.tar.gzList with sizes, dates, and permissions
tar -czvf archive.tar.gz dir/Create with verbose progress output
tar -xzf archive.tar.gz --exclude='*.log'Extract everything except matching files
tar --delete -f archive.tar pathRemove one path from an archive
tar -rf archive.tar newfileAppend a file to a plain archive
ls -lh archive.tar.gzCheck the archive size after creation