Create a compressed archive
Bundles a folder into a single gzip-compressed .tar.gz archive, listing each file as it's added so you can watch what gets included.
Last updated
tar -czvf archive.tar.gz folder/
How it works
-c creates a new archive; -z compresses it with gzip in the same pass instead of requiring a separate gzip step afterward; -v lists each file as it's added, handy for confirming nothing unexpected got swept in; -f archive.tar.gz names the output file and, like on extraction, must be the last flag in the cluster since the next argument is read as that filename. folder/ is the source being archived — one path here, but tar happily accepts several files or directories in a row.
Compressing in the same pass (rather than tar-then-gzip as two steps) is both simpler to type and avoids leaving an uncompressed intermediate .tar file lying around, which matters when archiving anything large.
Watch out for
- →The trailing slash on folder/ is cosmetic — tar archives the directory and its contents the same either way. What actually matters is the exact path you pass, since that's stored (and later restored) verbatim, including any leading directory components.
- →Run tar from a directory ABOVE the folder you're archiving (or cd into it first) if you don't want your full local path baked into every entry — otherwise anyone extracting it later reproduces your original directory structure exactly.
- →gzip compression is single-threaded and can be noticeably slow on very large archives; GNU tar's --use-compress-program=pigz (if installed) parallelizes it, and -J switches to xz compression instead for a smaller archive at the cost of slower compression time.
Part by part
- -czvf
- The bundled flag form tar is conventionally written in — create, gzip, verbose, file — as a single argument.
- c
- Create a new archive. One letter away from x for extract, and the rest of the command is identical, so read carefully before pressing enter.
- z
- Compress with gzip. Fast and universally supported; xz compresses better but takes considerably longer.
- f archive.tar.gz
- The archive to write. The f goes last in the bundle, because it takes the following argument as its value.
- folder/
- What to include. Naming a directory preserves it as the top level inside the archive, which is what stops extraction scattering files.
Common mistakes
tar -czvf archive.tar.gz *The shell expands the glob, so the archive has no single top-level directory and extraction scatters everything. It also silently skips dotfiles, which is how a .env or .github directory goes missing.
tar -czvf archive.tar.gz folder/tar -czvf folder/archive.tar.gz folder/Writing the archive inside the directory being archived can include the growing file in itself. GNU tar warns and skips it; other implementations produce a corrupt or enormous archive.
tar -czvf archive.tar.gz /home/user/projectAn absolute path makes tar strip the leading slash with a warning, and the archive then carries the full directory chain. Change into the parent and archive by relative name for a clean structure.
Questions
How do I exclude files?
--exclude takes a pattern and can be repeated, as in --exclude=node_modules --exclude='*.log'. Quote the patterns so the shell passes them through rather than expanding them first.
Should I use gzip, bzip2 or xz?
gzip for speed and compatibility, xz when size matters and time does not — xz is typically twenty to thirty per cent smaller and several times slower. bzip2 now sits awkwardly between the two with little reason to choose it.
Why is my archive bigger than the folder?
Usually because the contents are already compressed — images, video, or another archive. Compressing compressed data adds container overhead without removing anything, so plain tar with no -z is sometimes the better choice.
Does tar preserve permissions and timestamps?
Yes, which is much of why it persists. Extracting as a different user may not restore ownership without root, but the permission bits and modification times survive the round trip.