How to Create a tar.gz File in Linux

A tar archive stores a collection of files along with their metadata, such as ownership, permissions, and timestamps. When compressed with gzip, the archive file uses the .tar.gz or .tgz extension.
This guide explains how to create .tar.gz files using the tar command with practical examples.
Quick Reference
| Task | Command |
|---|---|
| Create from files | tar -czf archive.tar.gz file1 file2 |
| Create from a directory | tar -czf archive.tar.gz /path/to/dir/ |
| Create with verbose output | tar -czvf archive.tar.gz file1 file2 |
| Create from wildcard match | tar -czf images.tar.gz *.jpg |
| Exclude a file or directory | tar -czf archive.tar.gz --exclude=dir /path/ |
| Create in a specific location | tar -czf /backup/archive.tar.gz /path/to/dir/ |
For a printable quick reference, see the Tar cheatsheet .
Syntax
The general syntax for creating a .tar.gz file is:
tar -czf archive-name.tar.gz file-or-directory...Here is what each option does:
-c- Creates a new archive.-z- Compresses the archive using gzip.-f archive-name.tar.gz- Specifies the archive file name.file-or-directory...- A space-separated list of files and directories to add to the archive.
The user running the command must have read permissions on the files being archived and write permissions on the directory where the archive will be created.
Create a tar.gz File
To create an archive from two files, run:
tar -czf archive.tar.gz file1 file2On success, the command does not print any output. To verify the archive was created, list the directory contents with ls
:
ls -lh archive.tar.gzTo create the archive in a specific directory, provide the full path to the archive file:
tar -czf /home/user/archive.tar.gz file1 file2To see each file as it is added, include the -v (verbose) flag:
tar -czvf archive.tar.gz file1 file2file1
file2Archive a Directory
To create a .tar.gz archive from the contents of a directory, pass the directory path as the source:
tar -czf web_backup.tar.gz /var/www/websiteBy default, tar archives directories recursively. To disable recursive archiving, use the --no-recursion option.
To create an archive from all .jpg files in the current directory using a wildcard:
tar -czf images.tar.gz *.jpgExclude Files and Directories
Use --exclude to omit specific files or directories from the archive. Provide paths relative to the source:
tar -czf archive.tar.gz --exclude=node_modules --exclude='.git' /var/www/websiteTo exclude multiple patterns, repeat --exclude for each one. You can also use wildcards:
tar -czf archive.tar.gz --exclude='*.log' /var/www/websiteTroubleshooting
tar: file: Cannot stat: No such file or directory
One or more source paths are incorrect or do not exist. Verify each path with ls -l before running tar.
tar: archive.tar.gz: Cannot open: Permission denied
You do not have write permission in the destination directory. Save the archive to a writable location or run the command with appropriate privileges.
Wildcard does not include hidden files
Shell globs such as *.jpg do not match files that start with .. If you need hidden files, archive the directory itself instead of relying only on wildcards.
Archive is larger than expected
You may be including unnecessary directories such as node_modules, .git, logs, or cache files. Add --exclude patterns for those paths.
FAQ
How do I extract a tar.gz file?
Use tar -xzf archive.tar.gz. To extract to a specific directory, add -C /path/to/dir. See the extract tar.gz guide
for full details.
What is the difference between .tar.gz and .tgz?
They are the same format. .tgz is simply a shorter alias for .tar.gz, a tar archive compressed with gzip. Both extensions are equally valid.
What is the difference between .tar.gz and .tar.bz2?
Both are compressed tar archives, but they use different compression algorithms. gzip (.tar.gz) is faster. bzip2 (.tar.bz2) typically produces smaller files but is slower. For most use cases, .tar.gz is the standard choice. See how to extract .tar.bz2 files
for the bzip2 equivalent.
What is the difference between tar.gz and zip?
A .tar.gz file is a single archive that stores file metadata (permissions, ownership, timestamps). A .zip file compresses each file individually. On Linux, tar.gz is the standard for source code distribution and backups. See how to create zip files
for the zip equivalent.
My tar version does not support the -z flag. What can I do?
Older versions of tar may not have built-in gzip support. In that case, pipe the raw archive through the gzip
command:
tar -cf - file1 file2 | gzip > archive.tar.gzThis creates an uncompressed archive on standard output (-) and pipes it to gzip, which writes the compressed result to disk.
Conclusion
To create a .tar.gz file in Linux, use tar -czf archive.tar.gz followed by the files or directories to archive. Add -v for verbose output, and --exclude to skip specific files or directories.
For a full reference of tar options including extraction, see the tar command guide .
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page