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.









