tar Cheat Sheet
tar -czf archive.tar.gz dir/ # create gzip archive
tar -xf archive.tar.gz # extract (compression auto-detected)
tar -xf archive.tar.gz -C /opt # extract into another directory
tar -tf archive.tar.gz # list contents
tar -caf archive.tar.zst dir/ # pick compression from the file extension
tar -xf archive.tar.gz path/in/archive.txt # extract one file
Memory aid: create, extract, list, always with f followed by the archive file name. Add v for verbose output and a compression letter when creating. Dashes are optional for the bundled short form (tar czf works), but the examples here use them for clarity.
Create archives
| Command | What it does |
|---|---|
tar -cf a.tar dir/ | Uncompressed archive |
tar -czf a.tar.gz dir/ | gzip (.tar.gz / .tgz) |
tar -cjf a.tar.bz2 dir/ | bzip2 |
tar -cJf a.tar.xz dir/ | xz (capital J): smallest, slowest |
tar --zstd -cf a.tar.zst dir/ | zstd: fast with a good ratio |
tar -caf a.tar.xz dir/ | -a: choose compression from the extension |
tar -cvzf a.tar.gz dir/ | Verbose: print each file as it is added |
tar -czf a.tar.gz file1 file2 dir/ | Multiple files and directories |
tar -czf a.tar.gz -C /var/www html | Change directory first, so paths in the archive start at html/ |
tar -I 'zstd -19 -T0' -cf a.tar.zst dir/ | Custom compressor command and level (all CPU threads) |
XZ_OPT='-9 -T0' tar -cJf a.tar.xz dir/ | Max xz compression, multithreaded |
Extract archives
| Command | What it does |
|---|---|
tar -xf a.tar.gz | Extract into the current directory (any supported compression) |
tar -xvf a.tar.gz | Extract and list each file |
tar -xf a.tar.gz -C /tmp/out | Extract into a directory (it must exist) |
tar -xf a.tar.gz dir/file.txt | Extract one member (use the path exactly as -t lists it) |
tar -xf a.tar.gz --wildcards '*.conf' | Extract members matching a pattern |
tar -xf a.tar.gz --strip-components=1 | Drop the top-level directory from extracted paths |
tar -xkf a.tar.gz | Keep existing files, do not overwrite |
tar -xf a.tar.gz --skip-old-files | Skip existing files silently |
tar -xOf a.tar.gz dir/file.txt | Extract a file to standard output (capital O) |
sudo tar -xpf a.tar.gz --same-owner | Restore permissions and ownership (default when run as root) |
List, compare and update
| Command | What it does |
|---|---|
tar -tf a.tar.gz | List member paths |
tar -tvf a.tar.gz | Long listing: permissions, owner, size, date |
tar -tf a.tar.gz | grep nginx | Find a file inside the archive |
tar -df a.tar dir/ | Compare archive with the file system (--diff) |
tar -rf a.tar newfile | Append files (uncompressed archives only) |
tar -uf a.tar dir/ | Append files newer than the archived copy (uncompressed only) |
tar -f a.tar --delete dir/old.txt | Remove a member (uncompressed only) |
gzip -t a.tar.gz | Test that a gzip archive is not corrupted |
Selecting and excluding files
| Command | What it does |
|---|---|
tar -czf a.tar.gz --exclude='*.log' dir/ | Exclude by pattern |
tar -czf a.tar.gz --exclude=dir/cache dir/ | Exclude a directory |
tar -czf a.tar.gz --exclude-vcs dir/ | Skip .git, .svn and similar |
tar -czf a.tar.gz --exclude-caches dir/ | Skip directories containing a CACHEDIR.TAG |
tar -czf a.tar.gz -X excludes.txt dir/ | Read exclude patterns from a file |
tar -czf a.tar.gz -T filelist.txt | Archive the paths listed in a file |
tar -czf a.tar.gz --newer-mtime='2026-10-01' dir/ | Only files modified after a date |
tar -czf a.tar.gz --one-file-system / | Don't cross into other mounted file systems |
tar -czf a.tar.gz -h dir/ | Follow symlinks and archive the files they point to |
Common recipes
Back up /etc with a date in the file name:
sudo tar -czpf "etc-$(date +%F).tar.gz" -C / etc
Back up a home directory without caches and trash (for incremental backups, rsync is often better: see backup home with rsync):
tar --zstd -cpf "home-$(date +%F).tar.zst" \
--exclude='.cache' --exclude='.local/share/Trash' \
-C /home "$USER"
Copy a directory tree to another machine over SSH without a temporary file:
tar -czf - -C /srv data | ssh user@server 'tar -xzf - -C /srv'
Download and extract a release tarball in one step, dropping its top-level folder:
mkdir -p ~/apps/tool
curl -fsSL https://example.org/tool-1.2.3.tar.gz | tar -xzf - -C ~/apps/tool --strip-components=1
Look before you extract, to avoid a tarball that spills files everywhere:
tar -tf unknown.tar.gz | head
tar -tf unknown.tar.gz | cut -d/ -f1 | sort -u
Split a large archive into 2 GiB parts and join them again:
tar -czf - bigdir/ | split -b 2G - bigdir.tar.gz.part-
cat bigdir.tar.gz.part-* | tar -xzf -
Archive only files found by find, safely with odd names:
find . -name '*.pdf' -print0 | tar --null -T - -czf pdfs.tar.gz
Show progress on a big archive (needs pv):
tar -cf - bigdir/ | pv -s "$(du -sb bigdir | cut -f1)" | zstd -T0 > bigdir.tar.zst
Gotchas
ftakes the next argument. In bundled options the file name follows thef:tar -czf out.tar.gz dir/. Writingtar -cfz out.tar.gz dir/creates an archive named z containing out.tar.gz (if it exists) and dir/. Keepflast in the bundle.- Archive first, sources after.
tar -czf dir/ backup.tar.gztries to write the archive to dir/. The order is always archive name, then what to put in it. - Exclude before the path. Put
--excludeoptions before the directory you archive; some tar versions ignore them when placed after, and patterns are matched against archive paths, not absolute paths. - Leading slashes are removed.
tar -czf a.tgz /etcprints "Removing leading '/' from member names" and stores etc/..., so extracting never overwrites system files by accident. Use-C /to silence it. - Extracting overwrites without asking. Files with the same name in the target directory are replaced. List first with
-t, extract into an empty directory, or add-k. - Compression flags on extraction are optional in GNU tar, but the matching tool must be installed (
xz,zstd,bzip2). "Cannot exec" errors usually mean the compressor is missing. - Ownership and permissions. As a normal user, files are extracted as you, with your umask applied. As root, tar restores the stored owners. Use
-pwhen creating and restoring backups. - Watch disk space. Extracting a big archive can fill the disk; see No space left on device. Check sizes first with
tar -tvforgzip -l.
Reference: the GNU tar manual.
Frequently asked questions
How do I extract a tar.gz file in Linux?
Run tar -xzf file.tar.gz. GNU tar detects compression on extraction, so tar -xf file.tar.gz works too. Add -C /target/dir to extract somewhere else.
How do I create a tar.gz archive of a folder?
tar -czf archive.tar.gz folder/. The archive name must come right after -f.
How do I see what is inside a tar file without extracting?
tar -tf archive.tar.gz lists the paths; add -v for sizes, owners and dates.
Which compression should I use: gz, xz or zst?
gzip is the most compatible, xz gives the smallest files but is slow, and zstd is much faster than both at a ratio close to xz. On current distros zstd (--zstd) is a good default.
Spotted a mistake or a command that no longer works? Send a correction — pages are re-checked when reports come in.