Quick take: The tar command bundles files into a single archive and can compress it. Create with tar -czf archive.tar.gz files, extract with tar -xzf archive.tar.gz, and list with tar -tzf archive.tar.gz.

Introduction

The tar command (tape archive) packs many files and directories into one archive file, optionally compressed with gzip or bzip2. It is the standard way to bundle backups, distribute source code, and move directory trees between machines.

This guide explains the three operations you actually use — create, extract, and list — and the compression and convenience flags that go with them.

Syntax

The basic syntax of the tar command is:

tar [OPERATION] [OPTIONS] [ARCHIVE] [FILES...]

Common Options and Parameters

The most useful options and parameters for the tar command:

OptionDescription
-cCreate a new archive.
-xExtract files from an archive.
-tList the contents of an archive.
-f FILEUse the given archive file (always required).
-zCompress/decompress with gzip (.tar.gz).
-jCompress/decompress with bzip2 (.tar.bz2).
-JCompress/decompress with xz (.tar.xz).
-vVerbose — list files as they are processed.
-C DIRChange to DIR before extracting (extract to a target).
--exclude=PATExclude files matching a pattern.

Practical Examples

Real tar commands you can run today:

# Create a gzip-compressed archive
tar -czf backup.tar.gz /home/irfan/docs
# Extract a gzip archive
tar -xzf backup.tar.gz
# Extract into a specific directory
tar -xzf backup.tar.gz -C /restore/
# List the contents without extracting
tar -tzf backup.tar.gz
# Create a bzip2 archive (smaller, slower)
tar -cjf logs.tar.bz2 /var/log
# Exclude a directory while archiving
tar -czf site.tar.gz site/ --exclude='site/cache'

Backup Workflows with tar

tar is the foundation of countless backup scripts because it captures an entire directory tree — permissions and structure intact — into one portable file. A typical timestamped backup looks like this:

# Dated, compressed backup of a site
tar -czf "/backup/site-$(date +%F).tar.gz" /var/www/site

# Back up while excluding caches and logs
tar -czf app.tar.gz app/ --exclude='app/cache' --exclude='*.log'

# Stream a backup straight to a remote server over SSH
tar -czf - /data | ssh user@host 'cat > /backup/data.tar.gz'

That last pattern — piping tar through SSH — moves a whole directory to another machine without a temporary file, which is invaluable on systems short on disk space.

Common tar Mistakes

The most common error is option order: the -f flag must come immediately before the archive name, because it consumes the next argument as the filename. tar -cfz is wrong; tar -czf archive.tar.gz is right.

A second pitfall is creating a “tar bomb” — an archive whose files extract into the current directory instead of a single folder, scattering files everywhere. Always inspect first with tar -tzf archive.tar.gz to see the top-level layout, and extract into a clean directory with -C if you are unsure.

Tips and Best Practices

  • Remember the create/extract pair: -czf to compress, -xzf to extract. The f must come last because it names the file.
  • Use -t to inspect an archive before extracting so you know what it will create and where.
  • Use -C to extract somewhere other than the current directory instead of moving files afterwards.

Final Thoughts

tar is the backbone of Linux backups and software distribution. Learn the two everyday recipes — tar -czf to create and tar -xzf to extract — and the helpers -t, -C, and --exclude, and you can package or unpack any directory tree with confidence.

FAQ: tar Command in Linux

How do I extract a tar.gz file?+

Use tar -xzf archive.tar.gz. The x extracts, z handles gzip, and f names the file. Add -C /path to extract into a specific directory.

How do I create a tar.gz archive?+

Run tar -czf archive.tar.gz files-or-directory. The c creates, z compresses with gzip, and f sets the output filename.

How do I list the contents of a tar file without extracting?+

Use tar -tzf archive.tar.gz to list a gzip archive, or tar -tf archive.tar for an uncompressed one.

What is the difference between tar.gz and tar.bz2?+

Both are tar archives with compression. gzip (-z) is faster with good compression; bzip2 (-j) compresses smaller but is slower. xz (-J) compresses the smallest of the three.

Why does tar need the f option?+

The -f flag tells tar which archive file to read or write. Without it, tar tries to use a default device, so you almost always include -f followed by the filename last.

Need help with Linux servers or infrastructure?

Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.

Hire Me for Support