tar -czf: create a .tar.gz archive of a directory
- 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
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 | sortOutput
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
| Option | Meaning |
|---|---|
| -c | Create a new archive (overwrites an existing file with the same name) |
| -z | Compress with gzip; -j bzip2, -J xz, --zstd zstd |
| -f FILE | Archive file name; - means standard output |
| -v | Print each file name as it is added |
| -C DIR | Change to DIR before adding the following paths |
| --exclude=PATTERN | Skip matching files; put it before the paths |
| -a | Pick the compressor from the archive suffix (.tar.gz, .tar.xz) |
| -h | Archive the files symlinks point to, not the links |
| --remove-files | Delete the original files after adding them (GNU; use with care) |
Other formats and targets
| Command | Creates |
|---|---|
| tar -cf out.tar dir | Uncompressed tar |
| tar -cjf out.tar.bz2 dir | bzip2-compressed tar |
| tar -cJf out.tar.xz dir | xz-compressed tar (small, slow) |
| tar --zstd -cf out.tar.zst dir | zstd-compressed tar (GNU tar 1.31+, needs zstd) |
| tar -caf out.tar.xz dir | Compressor 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 dir | Date in the file name, for example backup-2026-10-23.tar.gz |
Pitfalls
-fin the wrong place. Intar -cfz out.tar.gz dirthe archive name becomesz, so tar writes an uncompressed archive calledzand fails without.tar.gz: Cannot stat: No such file or directory, because it treatsout.tar.gzas an input file. Keepflast:-czf.- Absolute paths.
tar -czf etc.tar.gz /etcprintsRemoving leading '/' from member namesand storesetc/.... That is intended; use-C /and a relative path to avoid the message. - No input.
tar -czf out.tar.gzwith no paths fails withCowardly 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) inC:\Windows\System32, which accepts the same-czfform.
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.