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 rsync Command: Synchronize Files and Directories

Linux rsync Command: Synchronize Files and Directories

Learn how to synchronize Files and Directories with the Linux rsync command, including practical examples, key options, and important precautions.

Linux tar Command: Create and Extract Archives

Linux tar Command: Create and Extract Archives

Learn how to create and Extract Archives with the Linux tar command, including practical examples, key options, and important precautions.

Linux groups Command: Show a User's Group Memberships

Linux groups Command: Show a User's Group Memberships

Learn how to show a User's Group Memberships with the Linux groups command, including practical examples, key options, and important precautions.

Linux fmt Command: Reformat Plain-Text Paragraphs

Linux fmt Command: Reformat Plain-Text Paragraphs

Learn how to reformat Plain-Text Paragraphs with the Linux fmt command, including practical examples, key options, and important precautions.

Linux pwd Command: Print the Current Working Directory

Linux pwd Command: Print the Current Working Directory

Learn how to use the Linux pwd command to print the logical or physical absolute path of the current working directory, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux bunzip2 Command: Decompress bzip2 Files

Linux bunzip2 Command: Decompress bzip2 Files

Learn how to decompress bzip2 Files with the Linux bunzip2 command, including practical examples, key options, and important precautions.

Linux cat Command: Display and Concatenate File Contents

Linux cat Command: Display and Concatenate File Contents

Learn how to use the Linux cat command to display text files, concatenate multiple files, number lines, reveal hidden characters, and avoid unsafe redirection mistakes.

Linux env Command: Display Variables and Run Commands with a Custom Environment

Linux env Command: Display Variables and Run Commands with a Custom Environment

Learn how to use the Linux env command to display environment variables or run a command with temporary variable values, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux unalias Command: Remove Command Aliases

Linux unalias Command: Remove Command Aliases

Learn how to use the Linux unalias command to remove one or all aliases from the current shell session, 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.