Skip to main content

Command referencetar

tar command (Linux / bash / sh / zsh)

Linuxbashshzshfileintermediate

What it does

Create, inspect, and extract archive files. When people search for the tar command linux archive, they usually want tar's everyday flags, safe examples, and how it differs from related tools. Below you'll find syntax, options, and copyable examples for Bash/sh/zsh.

Create, inspect, and extract archive files.

Syntax

tar [OPTION]... [FILE]...

Common options

Flag / optionMeaning
-cCreate an archive.
-xExtract an archive.
-tList archive contents.
-f FILEUse the named archive file.
-zFilter through gzip.

Practical examples

Create archive

tar -czf backup.tar.gz project/

Creates a gzip-compressed project archive.

Inspect first

tar -tf backup.tar.gz

Lists members without extracting.

Extract safely

mkdir restore && tar -xzf backup.tar.gz -C restore

Extracts into a dedicated directory.

Common mistakes

  • Forgetting `-f` before the archive filename.
  • Extracting an untrusted archive without checking paths for traversal or overwrites.

FAQ

What does tar do in Linux?

Create, inspect, and extract archive files.

How do I use tar from the terminal?

Start with `tar [OPTION]... [FILE]...`, then choose only the options needed for the task.

What is a practical tar example?

Try `tar -czf backup.tar.gz project/` — Creates a gzip-compressed project archive.

Can I use tar in a shell script?

Yes. tar works in Bash, sh, and Zsh, but scripts should handle quoting, errors, and command output explicitly.

Browse the full Linux and Windows command reference to search more bash, CMD, PowerShell, Conda, and GitHub CLI commands.