tar (tape archive) bundles many files into a single archive. Originally for tape backups; now the standard Unix archive format. tar itself doesn't compress — pair with gzip / bzip2 / xz / zstd for that.
tar [mode] [options] -f archive.tar [files...]
Every tar invocation needs exactly ONE mode:
| -c / --create | Create a new archive. |
| -x / --extract | Extract files from an archive. |
| -t / --list | List the contents of an archive (no extraction). |
| -r / --append | Append files to an existing uncompressed archive. |
| -u / --update | Append only files newer than the version in the archive. |
| -d / --diff | Compare archive contents to files on disk. |
| -z / --gzip | gzip — most common, fast, good ratio. Extension: .tar.gz / .tgz. |
| -j / --bzip2 | bzip2 — slower, slightly better ratio than gzip. Extension: .tar.bz2. |
| -J / --xz | xz / LZMA — slow, best ratio for text/source. Extension: .tar.xz. |
| --zstd | zstd — fast like gzip with ratio close to xz. Extension: .tar.zst. |
| -a / --auto-compress | (Create mode) pick compression based on the output filename's extension. |
GNU tar auto-detects compression on read (extract/list/compare). BSD tar usually does too. You can omit the compression flag when reading.
-r and -u only work on uncompressed tar archives. You cannot append to a .tar.gz in place — you'd have to decompress, append, recompress.
| Create gzipped archive | tar -czvf backup.tar.gz /home/user/ |
| Extract gzipped archive | tar -xzvf backup.tar.gz |
| Extract anywhere | tar -xzvf backup.tar.gz -C /restore/ |
| List contents | tar -tzvf backup.tar.gz |
| Strip a leading dir | tar -xzvf release-1.2.3.tar.gz --strip-components=1 |
| Exclude patterns | tar -czvf src.tar.gz --exclude='*.o' --exclude=.git src/ |
| From a file list | tar -czvf backup.tar.gz -T files.txt |
Historically tar accepts flags without the leading dash, in a single cluster. All four of these mean the same thing:
tar -xzvf file.tar.gz
tar xzvf file.tar.gz
tar -x -z -v -f file.tar.gz
tar --extract --gzip --verbose --file=file.tar.gz
The -f flag must come last in any cluster because it takes the filename as its argument. This tool always emits a clean, individually-dashed form for clarity.
• On Linux you have GNU tar. On macOS the default tar is BSD tar (libarchive). Most flags overlap but some are GNU-only — notably --owner, --group, and --transform. Install gnu-tar via Homebrew if you need them on macOS.
• When packing source code, always --exclude=.git (or use git archive instead).
• When unpacking a release tarball, check first with -t — some archives unpack into the current directory rather than a subdirectory ("tarbombs"). Use --strip-components or extract into a fresh dir.
• -p / --preserve-permissions is the default for root; non-root needs to ask for it.
This is an educational tool. The commands it builds are real — they will do exactly what you tell them to. Some flags are destructive (deletion, overwriting, forced operations) and even non-destructive options can cause data loss or system trouble in the wrong circumstances.
• Always review the generated command before running it.
• Test on disposable files and directories first.
• If you do not understand what a flag does, look it up in the official manual page (man command).
The author of these pages is not responsible for any damage, data loss, or other consequences resulting from commands generated, copied, or run from this site. Use at your own risk.
⚠ These tools build real shell commands. Review every command before running it. The author is not responsible for any damage, data loss, or other consequences resulting from commands generated, copied, or run from this site. Use at your own risk.
Send comments and bug reports to chris@chrisspackman.com.
Version 0.2.0 — Last updated: 2026-05-26
This page is Copyright © 2026
Chris Spackman <chris@chrisspackman.com>.
This web site developed entirely on GNU/Linux with Free / Open Source Software.
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.