Create an Archive
creates, prints each file, names the archive. Keep last: the word after it is the file name.
| Command | What 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
| Command | What 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.
| Command | What 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
| Command | What 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.
| Command | What 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.
| Command | What 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
| Command | What 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.
| Command | What 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.
| Command | What 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.gzis not installed by default (, , ).
Permissions and Overwriting
| Command | What 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 .
| Command | What 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.tarFlags Explained
| Flag | Meaning |
|---|---|
| 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.
| Command | What 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 .