tar Cheat Sheet
tar archive workflows: create, extract, inspect, compress with gzip/bzip2/xz, exclude paths, preserve permissions, and transfer archives.
tar groups files into an archive while retaining paths and metadata; compression is selected separately. Put options after a leading dash for portable, unambiguous command lines, and inspect an archive before extracting it into a shared directory.
Create archives
The core operation is c for create and f for the archive filename. v lists files as they are processed; omit it for quieter scripts.
tar -cf project.tar project/
tar -cvf project.tar project/
tar -czf project.tar.gz project/
tar -cjf project.tar.bz2 project/
tar -cJf project.tar.xz project/
| Option | Meaning |
|---|---|
-c | Create a new archive. |
-f FILE | Read or write the named archive file. |
-v | Verbose file listing. |
-z | Filter through gzip. |
-j | Filter through bzip2. |
-J | Filter through xz. |
-p | Preserve permissions when applicable. |
List and extract
Use t to inspect members without extracting and x to extract. -C changes the directory before the operation, and a member path can limit extraction to selected content.
tar -tvf project.tar
tar -tzf project.tar.gz
tar -xvf project.tar
tar -xzf project.tar.gz -C ./restored
tar -xzf project.tar.gz -C ./restored src/main.js
List first when an archive may contain absolute paths, .. components, or an unexpected top-level directory. Create the destination directory before using -C.
Compression and common combinations
A tar archive is not inherently compressed. The suffix is a convention; the filter flag tells tar which compressor to invoke.
tar -cvzf logs.tar.gz logs/
tar -cvjf logs.tar.bz2 logs/
tar -cvJf source.tar.xz source/
tar -xvzf logs.tar.gz
tar -xvjf logs.tar.bz2
tar -xvJf source.tar.xz
For a stream on standard input or output, use -f - or omit -f where the implementation permits it:
tar -czf - project/ > project.tar.gz
tar -xzf project.tar.gz -O project/README.md
Exclude and change directories
--exclude removes matching names from the archive. Use -C to avoid storing an unwanted absolute prefix or to combine paths from different roots.
tar -czf source.tar.gz --exclude='node_modules' --exclude='.git' project/
tar -czf release.tar.gz -C build .
tar -cf bundle.tar -C src app.js -C ../config settings.json
Quote wildcard patterns so the shell does not expand them before tar sees them. Check the resulting member list because exclude matching can vary with path prefixes and tar implementations.
Append, update, and limitations
r appends files to an uncompressed archive, while A concatenates another tar archive’s members. Appending to gzip, bzip2, or xz streams is not a reliable way to update compressed archives; recreate them instead.
tar -rf project.tar CHANGELOG.md
tar -Af project.tar extra.tar
tar -uf project.tar src/main.js
tar -czf project-new.tar.gz project/
u updates members only when the filesystem version is newer. It cannot provide the same convenient random update behavior as a filesystem, and compressed archives should generally be regenerated after changes.
Permissions and safer extraction
Use -p to preserve permissions when extracting as a user with sufficient authority. Ownership preservation may require root and should not be enabled casually for untrusted archives.
tar -xpf backup.tar -C ./restore
tar --same-owner -xpf backup.tar -C ./restore
tar --no-same-owner -xzf download.tar.gz -C ./restore
For untrusted input, inspect with tar -tvf, extract into an empty directory, and watch for path traversal entries. GNU tar’s --strip-components=1 can remove a known leading directory during extraction.
tar versus zip
Tar stores a collection of filesystem entries and commonly delegates compression to gzip, bzip2, or xz. Zip combines archiving and per-file compression and is often convenient for cross-platform GUI tools.
| Need | Typical choice |
|---|---|
| Unix backups with metadata | tar with an appropriate compressor |
| One-file gzip stream | tar.gz |
| Broad desktop compatibility | zip |
| Fast source distribution | tar.gz |
| Maximum xz compression | tar.xz |