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.
| Extension | Archive/compression type | Typical tar option |
|---|---|---|
.tar | Uncompressed tar archive | No compression option |
.tar.gz or .tgz | Tar archive compressed with gzip | z |
.tar.bz2 | Tar archive compressed with bzip2 | j |
.tar.xz | Tar archive compressed with xz | J |
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-directoriesThe 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-includeShort 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
| Option | Meaning | Typical use |
|---|---|---|
c | Create an archive | Build a new tar archive |
x | Extract archive members | Unpack an archive |
t | List archive contents | Inspect members without extracting |
f | Specify the archive file | Provide the archive filename after the option |
v | Verbose output | Display files as they are added, listed, or extracted |
z | Use gzip compression | Create or extract .tar.gz and .tgz files |
j | Use bzip2 compression | Create or extract .tar.bz2 files |
J | Use xz compression | Create or extract .tar.xz files |
-C | Change directory for an operation | Extract 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_textHere, 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.txtThe 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.tarThe 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.tgzHere, 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.gzExtract to a Chosen Directory
mkdir extracted-files && tar -xvf archive.tar -C extracted-filesThe -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.tarUse verbose mode when you need more detail, such as permissions, ownership, file sizes, timestamps, and paths:
tar -tvf archive.tarFor compressed archives, use the matching compression option:
tar -tzvf archive.tar.gzInspecting 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-areaArchive 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:
lsThen 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.tarWhen 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-filesThen 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.
fidentifies the archive filename. The archive filename follows the option group containingf.c,x, andtmean create, extract, and list.z,j, andJselect gzip, bzip2, and xz respectively.-Crequires 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.