VMware ESXi and vSphere Cluster Management

tar Command: Create, Compress, and Extract Archives in Linux

Learn how to use the Linux tar command to create, inspect, compress, and extract archives with gzip, bzip2, and xz.

The tar command is a Unix and Linux utility for creating, listing, and extracting archives. The name comes from tape archive: tar was originally designed to store collections of files on magnetic tape.

An archive is one file containing multiple files and directories, including useful metadata such as paths and permissions. Tar combines files into an archive, but tar itself is not primarily a compression tool. Compression is a separate step performed by formats such as gzip, bzip2, or xz. Tar can invoke these compressors while creating or extracting an archive.

Common uses include distributing software source code, creating backups, and packaging files for transfer between systems.

Archive Names and Compression Formats

An uncompressed tar archive conventionally ends in .tar. A compressed tar archive is often called a tarball. Its filename usually identifies both the tar archive and the compression format.

ExtensionArchive/compression typeTypical tar option
.tarUncompressed tar archiveNo compression option
.tar.gz or .tgzTar archive compressed with gzipz
.tar.bz2Tar archive compressed with bzip2j
.tar.xzTar archive compressed with xzJ

These extensions are naming conventions, not guarantees. The extension should match the actual format so people and tools can identify the file correctly.

Basic tar Command Structure

tar [options] archive-name files-or-directories

The options select the operation and control behavior such as compression, verbosity, archive-file selection, and extraction destination. The f option means “use the following value as the archive filename,” so the archive name must follow the option group containing f.

tar -cvf archive.tar files-to-include

Short options can commonly be combined. In -cvf archive.tar, c creates an archive, v enables verbose output, and f tells tar that archive.tar is the archive file. Avoid confusing the archive name with the input paths: everything after the archive filename is normally an item to add or process.

Common tar Options

OptionMeaningTypical use
cCreate an archiveBuild a new tar archive
xExtract archive membersUnpack an archive
tList archive contentsInspect members without extracting
fSpecify the archive fileProvide the archive filename after the option
vVerbose outputDisplay files as they are added, listed, or extracted
zUse gzip compressionCreate or extract .tar.gz and .tgz files
jUse bzip2 compressionCreate or extract .tar.bz2 files
JUse xz compressionCreate or extract .tar.xz files
-CChange directory for an operationExtract into a chosen destination

Creating an Uncompressed Archive

Archive Multiple Files

To create an uncompressed archive containing two files:

tar -cvf archive.tar results.txt sample_text

Here, c creates the archive, v prints each added filename, and f identifies archive.tar as the output file. The source files are results.txt and sample_text.

The source names must match existing files exactly. Linux distinguishes names such as sample_text and sample.txt.

Archive a Directory

tar -cvf project.tar project/

This stores the project/ directory and its nested contents. When extracted, tar can recreate that directory structure.

Tar stores the paths supplied on the command line. For example, archiving project/ usually creates members beginning with project/, while changing into a directory before archiving can produce shorter paths. Choose source paths deliberately because those paths affect where files appear during extraction.

Creating Compressed Tarballs

Gzip Compression

Use z to create a gzip-compressed tarball directly:

tar -cvzf compress.tgz results.txt sample.txt

The resulting file is a tar archive compressed with gzip. Gzip compresses the combined archive as a whole rather than independently creating a separate gzip file for every source file.

The longer naming convention is also common:

tar -cvzf project.tar.gz project/

The source filenames must exist in the current directory or be supplied with correct paths.

Bzip2 and Xz Compression

tar -cvjf project.tar.bz2 project/
tar -cvJf project.tar.xz project/

The lowercase j selects bzip2, while the uppercase J selects xz. Use the option matching the archive's actual compression format.

Extracting Archives

Extract an Uncompressed Archive

tar -xvf archive.tar

The x option extracts members, and v displays their names. By default, tar writes the extracted files into the current directory—the directory in which the command runs.

Extract a Gzip-Compressed Tarball

tar -xvzf compress.tgz

Here, x extracts, v displays names, z handles gzip compression, and f identifies compress.tgz as the input archive.

For a gzip file named archive.tar.gz, the equivalent command is:

tar -xzvf archive.tar.gz

Extract to a Chosen Directory

mkdir extracted-files && tar -xvf archive.tar -C extracted-files

The -C option changes tar's extraction destination. The destination directory must already exist, which is why mkdir runs first. A dedicated directory prevents extracted files from being mixed with existing work.

Inspecting Archive Contents

List an archive without extracting it by replacing x with t:

tar -tf archive.tar

Use verbose mode when you need more detail, such as permissions, ownership, file sizes, timestamps, and paths:

tar -tvf archive.tar

For compressed archives, use the matching compression option:

tar -tzvf archive.tar.gz

Inspecting an unfamiliar archive before extraction is a good safety practice. It shows whether the archive contains a top-level directory, unexpected absolute paths, or names that could conflict with existing files.

Safe Extraction and Path Awareness

Extraction can overwrite files that already exist at the same paths. Before unpacking an archive, list its contents and consider extracting into a new, empty directory:

mkdir review-area && tar -xzf archive.tar.gz -C review-area

Archive member paths determine where files are placed relative to the extraction directory. An archive containing project/readme.txt creates a project directory below the destination. An archive containing files at its top level places those files directly in the destination.

Do not use elevated privileges such as sudo unless the archive contents and target location genuinely require them. Running extraction as a privileged user can create files owned by another account or overwrite protected files.

Common Errors and Option Mistakes

Source File Cannot Be Found

If tar reports that a source file does not exist, the command may contain a typo, use the wrong working directory, or refer to a missing file. Check exact names and paths:

ls

Then rerun tar with the correct source argument. For example, sample_text and sample.txt are different names.

Archive File Cannot Be Opened

If tar cannot open the archive, verify its location and filename:

ls -l archive.tar

When using f, put the archive filename immediately after the option group that contains f, such as:

tar -cvf archive.tar project/

If the filename is omitted or placed incorrectly, tar may treat an intended archive name as an input file or report an unexpected error.

Wrong Compression Option

On systems where automatic compression detection is unavailable or not being used, extracting a compressed archive without its matching option can produce unreadable-data errors. Use z for gzip, j for bzip2, J for xz, and no compression option for an uncompressed tar archive.

Destination for -C Does Not Exist

Create the destination before extraction:

mkdir extracted-files

Then run tar with -C extracted-files. If the directory is elsewhere, provide an appropriate relative or absolute path.

Existing Files Are Overwritten

This usually means the archive was extracted into a populated directory. List the archive first and use a new destination directory to reduce conflicts.

Quick Reference

  • Create an uncompressed archive: tar -cvf archive.tar files-or-directories
  • Create a gzip tarball: tar -cvzf archive.tar.gz files-or-directories
  • Create a bzip2 tarball: tar -cvjf archive.tar.bz2 files-or-directories
  • Create an xz tarball: tar -cvJf archive.tar.xz files-or-directories
  • List contents: tar -tvf archive.tar
  • List gzip contents: tar -tzvf archive.tar.gz
  • Extract an uncompressed archive: tar -xvf archive.tar
  • Extract a gzip tarball: tar -xzvf archive.tar.gz
  • Extract to a destination: tar -xvf archive.tar -C existing-directory

Exam-Relevant Notes

  • tar archives; compression reduces size. Tar can combine with gzip, bzip2, or xz, but archiving and compression are distinct operations.
  • f identifies the archive filename. The archive filename follows the option group containing f.
  • c, x, and t mean create, extract, and list.
  • z, j, and J select gzip, bzip2, and xz respectively.
  • -C requires an existing directory when selecting an extraction destination.
  • Inspect unfamiliar archives before extracting them into a directory containing important files.

Once you understand the operation option, the archive filename, and any matching compression option, most tar commands follow the same predictable pattern.