Archives and compression

Command tar

Create and extract tar archives

Linux · Bash examples · commands run on the selected server

When to use tar

Use tar to package directory trees and inspect archives before extraction. Extraction can write many paths, so select a dedicated destination and assess the source rather than unpacking unknown content over a live application.

Syntax

tar [OPTIONS] [FILE...]

Square brackets mark optional arguments; an ellipsis means repetition. Replace example file names, user names and server addresses with your own values.

Common options and arguments

-c / -x / -t
Create, unpack or show the contents of an archive.
-f FILE
Archive name.
-z / -j / -J
Compression gzip, bzip2 or xz.

Usage examples

Create a project archive

tar -czf ./project.tar.gz ./project

The project tree is archived with gzip compression into a separate file.

Inspect archive contents before extracting

tar -tzf ./project.tar.gz

Archive entries are listed without extraction, allowing a first review of names and layout.

Unpack into a separate directory

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

The archive is extracted inside the prepared destination directory.

Notes and limitations

  • Before unpacking, check the names and source of the archive. Do not unpack untrusted archives on top of production data.
  • Extracting may overwrite files; use a separate directory and a regular account.

Common questions and troubleshooting

Will tar update an existing destination safely?

Extraction can replace existing paths depending on options and archive contents. Use an isolated destination and compare results before replacing live files.

Is listing an untrusted archive a complete safety check?

No. Names are only one aspect; links, permissions, file types and expansion size also matter. Avoid privileged extraction and use suitable isolation.

Further help

Options vary by version and distribution. Check the installed documentation with this command: man tar.