Skip to content

/commands/tar/create-tar-gz

tar -czf: create a .tar.gz archive of a directory

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · tar (GNU tar) 1.35updated
tool
tar
versions
GNU tar 1.30+, bsdtar 3.x
os
linux, macos, windows

tar -czf archive.tar.gz folder/ creates a gzip-compressed archive of folder and everything inside it. The letters mean create, compress with gzip, and file name follows, so -f must be last in the group and directly before the archive name. List several paths to put them all in one archive, add -v to print each file as it is added, and use -C DIR to archive a directory without its parent path.

Example

bash
mkdir -p project/src project/docs
printf 'console.log(1)\n' > project/src/index.js
printf '# Demo\n' > project/docs/README.md
tar -czf project.tar.gz project
tar -tzf project.tar.gz | sort
echo ===
tar -czf docs-only.tar.gz -C project docs
tar -tzf docs-only.tar.gz | sort

Output

project/
project/docs/
project/docs/README.md
project/src/
project/src/index.js
===
docs/
docs/README.md

The first archive contains the project/ directory with its full tree. The second uses -C project to change into project first, so the members start at docs/. tar -tzf lists the members without extracting, which is the quickest check that the archive holds what you expected.

Options

OptionMeaning
-cCreate a new archive (overwrites an existing file with the same name)
-zCompress with gzip; -j bzip2, -J xz, --zstd zstd
-f FILEArchive file name; - means standard output
-vPrint each file name as it is added
-C DIRChange to DIR before adding the following paths
--exclude=PATTERNSkip matching files; put it before the paths
-aPick the compressor from the archive suffix (.tar.gz, .tar.xz)
-hArchive the files symlinks point to, not the links
--remove-filesDelete the original files after adding them (GNU; use with care)

Other formats and targets

CommandCreates
tar -cf out.tar dirUncompressed tar
tar -cjf out.tar.bz2 dirbzip2-compressed tar
tar -cJf out.tar.xz dirxz-compressed tar (small, slow)
tar --zstd -cf out.tar.zst dirzstd-compressed tar (GNU tar 1.31+, needs zstd)
tar -caf out.tar.xz dirCompressor chosen from the suffix
tar -czf - dir | ssh host "cat > dir.tar.gz"Stream the archive to another machine
tar -czf backup-$(date +%F).tar.gz dirDate in the file name, for example backup-2026-10-23.tar.gz

Pitfalls

  • -f in the wrong place. In tar -cfz out.tar.gz dir the archive name becomes z, so tar writes an uncompressed archive called z and fails with out.tar.gz: Cannot stat: No such file or directory, because it treats out.tar.gz as an input file. Keep f last: -czf.
  • Absolute paths. tar -czf etc.tar.gz /etc prints Removing leading '/' from member names and stores etc/.... That is intended; use -C / and a relative path to avoid the message.
  • No input. tar -czf out.tar.gz with no paths fails with Cowardly refusing to create an empty archive.
  • Archive inside the source. For an uncompressed archive, GNU tar 1.35 skips it with archive cannot contain itself; not dumped. When the output goes through a compressor tar cannot always tell, so write the archive outside the directory you archive.
  • Windows. Git Bash ships GNU tar; Windows 10 and later also ship tar.exe (bsdtar) in C:\Windows\System32, which accepts the same -czf form.

Confirm it worked

tar -tzf archive.tar.gz | head lists the first members, and gzip -t archive.tar.gz && echo ok tests the compressed data. A file that is not gzip data fails with gzip: FILE: not in gzip format from gzip -t and gzip: stdin: not in gzip format from tar -tzf.

FAQ

How do I create the archive without the parent folder inside it?

Use -C to change into the directory first and archive its contents, tar -czf site.tar.gz -C /var/www/site . The members then start with ./ instead of var/www/site/.

Is .tgz the same as .tar.gz?

Yes. Both names mean a tar archive compressed with gzip; tar reads them the same way. .tgz is a short form from systems that limited file name extensions.

How do I make a smaller archive?

Use a stronger compressor, -J for xz (.tar.xz) or --zstd for zstd (.tar.zst, GNU tar 1.31+), or set the gzip level with GZIP=-9 or -I 'gzip -9'. xz is the smallest and the slowest; zstd is fast with good ratios.

Sources