Linux tar Command: Create and Extract Archives

`tar` bundles multiple files and directories into a single archive or extracts them. `.tar` itself is not a compression format, but it can be combined with `gzip`, `bzip2`, or `xz` to create compressed archives.

tar Command

An archive is a bundle that includes file names and directory structure. In GNU tar, `-c` is for creating, `-t` is for listing, `-x` is for extracting, and the archive file name follows `-f`. `-z` is used with gzip, `-j` with bzip2, and `-J` with xz compression. Checking the list and destination before extraction can help prevent files from being unpacked into unexpected paths.

Basic syntax

tar [action] [option] -f archive [file or directory...]

The installed implementation and options may vary depending on the distribution. Check the current system’s documentation with `man tar`.

Examples

Creating a gzip Compressed Archive

This bundles the contents of `project/` along with its directory structure. If `project.tar.gz` already exists, it can be overwritten.

tar -czf project.tar.gz project/

Checking the List Before Extracting

To determine the extraction location and file names, first list the members.

tar -tzf project.tar.gz

Extract to the specified directory

Create restore/ first and then extract into it. Make sure it does not overlap with existing files.

mkdir -p restore
tar -xzf project.tar.gz -C restore/

Checking the contents of an archive

tar combines multiple files into a single archive. The following example creates small files, bundles them, and lists the members without extracting them.

printf 'hello\n' > note.txt
tar -cf bundle.tar note.txt
tar -tf bundle.tar

Example output:

note.txt

-c is for creating, -t is for listing, and -f specifies the archive file. .tar itself does not mean compression, and separate compression can be applied with -z, -J, etc. Before unpacking an archive from an untrusted source, check whether the internal paths include absolute paths or parent directory traversal, and extract it in a separate working directory.

Main Options and Format

Options/Format Description
-c / -t / -x Choose one of the actions: create, list, or extract.
-f file Specify the archive file to read or write.
-z / -j / -J Use gzip, bzip2, or xz compression respectively.
-C directory Change the working directory.
-v Display the names of the files being processed.

Precautions when using

Do not extract an untrusted archive with administrative privileges directly. First, check the filenames of the target files, symbolic links, and the possibility of overwriting existing files, and use an isolated empty directory. If you recreate an existing archive with tar -cf, it writes a new archive rather than appending contents, so the existing contents may be lost.

Frequently Asked Questions

What is the difference between tar and gzip?

tar bundles multiple files and directories into one, while gzip compresses a data stream. tar -czf is a command that combines both functions.

Official Documentation

The exact behavior of the options and differences between implementations can be checked in the official tar documentation.

More in This Category
Linux ls Command: List Files and Directories

Linux ls Command: List Files and Directories

Learn how to use the Linux ls command to list files and directories with permissions, sizes, timestamps, and sorting options, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux paste Command: Join Corresponding Lines Side by Side

Linux paste Command: Join Corresponding Lines Side by Side

Learn how to join Corresponding Lines Side by Side with the Linux paste command, including practical examples, key options, and important precautions.

Linux parted Command: Inspect and Manage Partitions

Linux parted Command: Inspect and Manage Partitions

Learn how to inspect and Manage Partitions with the Linux parted command, including practical examples, key options, and important precautions.

Linux fold Command: Wrap Long Lines at a Fixed Width

Linux fold Command: Wrap Long Lines at a Fixed Width

Learn how to wrap Long Lines at a Fixed Width with the Linux fold command, including practical examples, key options, and important precautions.

Linux ssh-keygen Command: Generate SSH Keys and Inspect Fingerprints

Linux ssh-keygen Command: Generate SSH Keys and Inspect Fingerprints

Learn how to generate SSH Keys and Inspect Fingerprints with the Linux ssh-keygen command, including practical examples, key options, and important precautions.

Linux apt-get Command: Run APT Package Operations in Scripts

Linux apt-get Command: Run APT Package Operations in Scripts

Learn how to run APT Package Operations in Scripts with the Linux apt-get command, including practical examples, key options, and important precautions.

Linux chgrp Command: Change a File's Group

Linux chgrp Command: Change a File's Group

Learn how to change a File's Group with the Linux chgrp command, including practical examples, key options, and important precautions.

Linux umount Command: Unmount a Filesystem Safely

Linux umount Command: Unmount a Filesystem Safely

Learn how to unmount a Filesystem Safely with the Linux umount command, including practical examples, key options, and important precautions.

Linux scp Command: Copy Files over SSH

Linux scp Command: Copy Files over SSH

Learn how to copy Files over SSH with the Linux scp command, including practical examples, key options, and important precautions.

Linux time Command: Measure Command Execution Time

Linux time Command: Measure Command Execution Time

Learn how to use the Linux time command to measure elapsed, user CPU, and system CPU time for a command, with essential options, practical examples, output interpretation, and common troubleshooting tips.