Home > Archive/Compression > tar

tar: Create and Extract File Archives

An essential tool used to bundle multiple files and directories into a single .tar file (archiving), or to extract files from an existing archive. It is frequently used with compression features like Gzip (-z) or Bzip2 (-j).

What is tar?

tar stands for 'Tape Archive' and was originally created for backing up data to magnetic tapes. Today, it is used as a standard Linux tool to bundle multiple files and directories into a single file for easier management and transfer. tar itself only bundles files; compression is specified via separate options (-z, -j, -J).

Core Operations (Modes)

tar operates in one primary mode at a time (create, extract, list).

  • -c (Create): Creates a new archive file.
  • -x (Extract): Extracts files from an archive file.
  • -t (List): Views the contents (list) of an archive file without decompressing it.
  • -f (File): Specifies the target archive 'file'. This is essential for almost all operations.

Key Options (Shell)

tar is used by combining options. The most important is choosing one of `-c`, `-x`, or `-t`.

1. Operation Mode (Choose One)

2. Specify File and Compression

3. Targets and Additional Features

Generated command:

Try combining the commands.

Description:

`tar` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Scenarios Examples (Shell)

Examples of the most frequently used combinations. (The leading `-` for options can be omitted but it's recommended to include it for clarity.)

Create a gzip compressed archive (.tar.gz)

tar -cvzf backup.tar.gz /home/user/data

c (create), v (verbose), z (gzip), f (file) + [output file] + [target]

Extract a gzip compressed archive (.tar.gz)

tar -xvzf backup.tar.gz

x (extract), v (verbose), z (gzip), f (file) + [archive file]

View contents of a compressed archive (.tar.gz)

tar -tvf backup.tar.gz

t (list), v (verbose), f (file) (modern tar auto-detects -z)

Extract to a specific directory

tar -xvzf backup.tar.gz -C /opt/restore

Specify the path with the -C option.

Create a bzip2 compressed archive (.tar.bz2)

tar -cvjf archive.tar.bz2 ./docs

Use the j (bzip2) option instead of z.

Extract a bzip2 compressed archive (.tar.bz2)

tar -xvjf archive.tar.bz2

Archive excluding specific files/directories

tar -cvzf app.tar.gz ./app --exclude="*.log" --exclude="node_modules"

Use the --exclude option. (Can be used multiple times)

Installation

tar is pre-installed on almost all Linux distributions (and macOS). (Usually GNU tar). If for any reason installation is needed, you can install it using the 'tar' package name.

Installed by default

Separate installation is rarely needed. Try `tar --version` to check its version.

Debian/Ubuntu (if needed)

sudo apt update && sudo apt install -y tar

RHEL/CentOS/Fedora (if needed)

sudo dnf install -y tar

Tips & Cautions

Useful Tips

  • Omitting Hyphens (-): By old convention, you can omit the hyphen for the first group of options, e.g., `tar cvf ...` instead of `tar -cvf ...`. However, it is recommended to include the hyphen for clarity.
  • Automatic Compression Detection: Modern `tar` can automatically detect compression type (e.g., gzip, bzip2, xz) based on the file extension during extraction (-x), even without `-z`, `-j`, or `-J`. (e.g., `tar -xf archive.tar.gz` is sufficient.)
  • Preserving Permissions (-p): When restoring system backups, it's crucial to use the `-p` option to preserve original file permissions and ownership.
  • Path Issues (-C): If you want to extract files to a location other than the current directory, using the `-C /path` option is much more efficient than extracting and then moving the files.
  • Caution with Absolute Paths: If you archive files using an absolute path like `/home/user` when creating (-c), they will be extracted to the same absolute path during extraction (-x). (Modern tar might remove leading `/` for security reasons.) It's safer to archive using relative paths, like `cd /home && tar -cvf user.tar ./user`.

Related commands

These are commands that are functionally similar or are commonly used together.



Same category commands