tar command (Linux / bash / sh / zsh)
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 / option | Meaning |
|---|---|
| -c | Create an archive. |
| -x | Extract an archive. |
| -t | List archive contents. |
| -f FILE | Use the named archive file. |
| -z | Filter 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.
Related commands
- cpCopy files or directory trees to another location.
- scpCopy files securely between local and SSH-accessible hosts.
- Copy-ItemCopy items between locations supported by PowerShell providers.
- lsList directory entries and inspect file metadata.
- cdChange the shell's current working directory.
- dirList files and subdirectories with names, dates, sizes, and attributes.
- findWalk directory trees and select entries using tests and actions.
- Get-ChildItemEnumerate items and child items in filesystem, registry, and other providers.
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.