Home·Terminal commands

tar

Archives

Usage

tar -czf <archive.tar.gz> <path>

Bundles files into one archive but does not compress on its own — `-z` (gzip), `-j` (bzip2) and `-J` (xz) do that part — and you pick exactly one of `-c` to create, `-x` to extract or `-t` to list, keeping `-f` as the last flag so the archive name follows it directly.

Common flags

FlagMeaning
-c / -x / -tCreate / extract / list. Exactly one of the three, and it comes first.
-f <file>The archive name, and it must be the last flag before that name.
-z / -j / -Jgzip / bzip2 / xz. GNU tar detects the format when extracting, so -z is optional with -x.
-vPrint the file names as they go past.
-C <dir>Change into that directory first. Position in the command line matters.
--exclude='node_modules'Skip matching paths. Write it before the source paths.
--strip-components=1Drop the top-level directory when extracting.
--zstdUse zstd instead of gzip (GNU tar 1.31 and newer).

Examples

tar -czf site.tar.gz -C /var/www .

Archive the contents of a directory without the leading path.

tar -xzf site.tar.gz -C /tmp/restore

Extract into a directory that already exists.

tar -tzf site.tar.gz | head

Look inside before extracting, so nothing lands in $PWD by surprise.

Bundling and compressing are separate jobs: tar bundles, gzip shrinks, and .tar.gz is both.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does tar do?

Bundles files into one archive but does not compress on its own — `-z` (gzip), `-j` (bzip2) and `-J` (xz) do that part — and you pick exactly one of `-c` to create, `-x` to extract or `-t` to list, keeping `-f` as the last flag so the archive name follows it directly.

Q. How do I type it?

tar -czf <archive.tar.gz> <path> — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

8 are listed here; the full set is in man tar. This command sits under Archives.

Related commands

man page: man tar