Cheat Sheet

tar Command Examples

tar bundles files into a single archive and, with one more flag, compresses it. These tar command examples cover creating, extracting and unzipping archives, tar.gz, bz2 and xz, listing and excluding files, progress output, and the tar built into Windows 10 and 11.

Last updated September 11, 2026

Create an Archive

creates, prints each file, names the archive. Keep last: the word after it is the file name.

CommandWhat it does
Archive a directory, no compression
Archive specific files
Mix directories and files in one archive
Same command, classic Unix style without the hyphen
Archive the paths listed in a file, one per line
Archive files another command found (see the find cheat sheet)
Keep going instead of failing on a file you cannot read

Extract an Archive

CommandWhat it does
Extract into the current directory
Same, without listing every file
Extract one file, named by its path as stored
Extract only files matching a pattern
Extract a tar.gz
Also fine: extraction detects the compression on its own
Keep existing files instead of overwriting them

Extract to a Directory

tar never creates the target directory, so make it first.

CommandWhat it does
Extract into /opt/app instead of the current directory
Create the target, then extract into it
Drop the archive's top-level folder while extracting
Drop two levels, for archives nested deeper
Archive from another directory without the path prefix

works on both sides: on extract it is where files land, on create it is where tar looks for them.

gzip and tar.gz

CommandWhat it does
Create a gzip-compressed archive
Extract it
List it without extracting
.tgz is the same format, shorter name
picks the compression from the extension you typed

Streaming to another machine compresses and transfers in one step, with nothing written locally:

tar -czf - /var/www | ssh user@backup "cat > www-$(date +%F).tar.gz"

For a backup you repeat, rsync is usually the better tool: it sends only what changed.

bz2 and xz

Same verbs, different letter: for bzip2, capital for xz. gzip is the fastest, xz the smallest.

CommandWhat it does
Create a bzip2 archive
Extract it
Create an xz archive (capital J)
Extract it
Extraction auto-detects the format either way

Zip and Unzip

On Linux, "zip a folder" almost always means make a tar.gz. Real files are a different format, and only bsdtar reads them.

CommandWhat it does
Zip a folder the tar way, one compressed file
Unzip it again into the current directory
Open a real .zip, but only with bsdtar (macOS, Windows)
Unzip a .zip on Linux, where GNU tar cannot
Make a real .zip when the other side needs one

List Contents

CommandWhat it does
List the file names
Long listing with sizes, owners, and dates
List a tar.gz (plain also works)
Check whether a file is in the archive
List only matching files
Print one file's contents without extracting anything

Exclude Files

Quote the patterns so the shell does not expand them first.

CommandWhat it does
Skip files matching a pattern
Skip a directory, named as tar would store it
Skip .git, .svn, and other VCS directories
Read exclude patterns from a file
Repeat the flag for each pattern you want gone

Progress and Verbose

tar says nothing while it works unless you ask. names every file, which is noisy on a large archive; the alternatives below give you a pulse instead.

CommandWhat it does
Print each file name as it is archived
Print the total bytes written when it finishes
Print a dot every 1000 records, a cheap progress bar
No , no output: silence means it worked

For a real percentage and an ETA, pipe through and tell it the expected size:

tar -czf - dir/ | pv -s "$(du -sb dir/ | cut -f1)" > backup.tar.gz

is not installed by default (, , ).

Permissions and Overwriting

CommandWhat it does
Restore the stored permissions as a normal user
Overwrite existing files, which is already the default
Keep existing files and error on the clashes
Same, but skip quietly instead of erroring
Extract as yourself even when running as root
Include hidden files, by archiving instead of

Append to an Archive

Appending only works on an uncompressed ; tar refuses to append to a .

CommandWhat it does
Append a file to the end of the archive
Append only files newer than their archived copy
Append another archive's contents

To add a file to a compressed archive, decompress, append, recompress:

gzip -d archive.tar.gz && tar -rvf archive.tar extra.txt && gzip archive.tar

Flags Explained

FlagMeaning
Create an archive
Extract an archive
List contents
The archive file; keep it last, the next word is the name
Verbose, print each file as it is processed
gzip compression (.tar.gz)
bzip2 compression (.tar.bz2)
xz compression (.tar.xz)
Choose compression from the archive's extension (create only)
Change to dir before archiving or extracting
Append files to an existing .tar
Append only files newer than the archived copy
Preserve permissions on extract (the default when root)
Keep existing files, never overwrite
Extract to standard output instead of the disk
Read the list of files to archive from file
Drop the first N path levels on extract
Skip matching files
Print the bytes written when the archive is done
Print a dot every N records, as a progress sign
Do not fail the whole run on an unreadable file
Do not restore the stored owner when extracting as root
Delete each file after it has been archived

tar on Windows 10 and 11

Windows has shipped bsdtar since the April 2018 update of Windows 10, so these run in Command Prompt and PowerShell with nothing installed.

CommandWhat it does
Extract into the current folder
Extract into a folder that already exists
Create a tar.gz of a folder
List the contents
bsdtar extracts .zip files too
Says which tar you have; the built-in prints bsdtar

The Linux sections above apply as-is; only the paths look different.

Gotchas

  • Flag order bites everyone once: must be last in the cluster. is right; creates an archive literally named .
  • is not an error. tar stores relative paths on purpose so an extraction cannot overwrite system files at their original absolute paths.
  • Archive the folder, not its contents: run from the parent directory. An archive of loose files scatters them all over whatever directory it is extracted in.
  • Ownership depends on who extracts: root restores the original owners, a normal user gets every file as their own. Add to keep permissions when extracting as a normal user.
  • macOS ships bsdtar too. Everyday commands match GNU tar, but is missing there (patterns work without it) and should come before the paths, as written above.
  • tar has no password option. For an encrypted archive, pipe it through gpg: , and reverse it with .

tar FAQ

Which tar command switch compresses the archive?

z for gzip (.tar.gz), j for bzip2 (.tar.bz2), and capital J for xz (.tar.xz). Without one of those, tar only bundles files at full size. -a picks the compressor from the file name you typed, so tar -caf out.tar.xz dir works out the rest. The other letters are the verbs around it: c creates, x extracts, t lists, v prints each file name, and f means the next word is the archive name. That is all tar -czvf is, four separate switches stuck together, and f has to come last or tar treats the following letter as the file name.

Can the tar command unzip a zip file?

It depends on which tar you have. The bsdtar that ships with macOS, Windows 10 and Windows 11 reads .zip fine, so tar -xf archive.zip works there. GNU tar on Linux does not; it fails with an unrecognized archive format error, and the tool you want is unzip archive.zip. Going the other way, tar cannot write .zip at all, so use zip -r out.zip folder when the other side insists on one. On Linux, unzip usually just means extracting a .tar.gz, which is plain tar -xzf.

Does the tar command delete the original files?

No. Creating an archive copies the files and leaves the originals where they are, which is why a tar.gz of a big directory briefly needs room for both. Add --remove-files if you actually want tar to delete each file after it is safely archived. The confusion comes from gzip on its own, which does replace file with file.gz. Extraction is the direction that destroys things: tar overwrites same-named files in the target without asking, so use -k to keep what is already there.

How do I extract a tar.gz file to a specific directory?

Pass -C with the target: tar -xzf app.tar.gz -C /opt/app. tar changes into that directory before unpacking, so it must already exist; create it first with mkdir -p /opt/app. If the archive wraps everything in a single top-level folder you do not want, add --strip-components=1 to drop it, so the files land directly in /opt/app instead of /opt/app/app-1.2.3.

How do I use the tar command in Windows?

Open Command Prompt or PowerShell and run it exactly as you would on Linux. Windows 10 (since the April 2018 update) and Windows 11 ship bsdtar, so there is no WSL, Git Bash or 7-Zip to install: tar -xf archive.tar.gz extracts, tar -czf out.tar.gz Documents creates, tar -tf lists. Paths use backslashes, and -C C:\output still needs the folder to exist first. Run tar --version to see which one you have; the built-in reports bsdtar, and unlike GNU tar it opens .zip files too.

Related cheat sheets