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
For a printable quick reference, see the tar cheatsheet .
| Task | Command |
|---|---|
| Create from files | tar -czf archive.tar.gz file1 file2 |
| Create from a directory | tar -czf archive.tar.gz /path/to/dir/ |
| Archive without parent path | tar -czf archive.tar.gz -C /path/to/parent dirname |
| Create a .tgz file | tar -czf archive.tgz file1 file2 |
| 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/ |
| List archive contents | tar -tzf archive.tar.gz |
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/websiteTo confirm what went into the archive, list its contents with the --list (-t) option:
tar -tzf web_backup.tar.gzvar/www/website/
var/www/website/index.html
var/www/website/css/style.cssBy default, tar archives directories recursively. Add --no-recursion to store only the directory entry itself, which is useful when you build the file list yourself with find
and pass it to tar.
To create an archive from all .jpg files in the current directory using a wildcard:
tar -czf images.tar.gz *.jpgArchive Paths and the Leading Slash
Notice that the paths in the listing above start with var/, not /var/. When the source is an absolute path, tar strips the leading slash and prints a warning:
tar: Removing leading '/' from member namesThe archive is still created. The warning means that every member is stored under the full relative path, so extracting the archive recreates the entire var/www/website tree instead of the folder on its own.
To archive the folder without the parent path, change into the parent directory with the --directory (-C) option and pass only the folder name:
tar -czf web_backup.tar.gz -C /var/www websiteThe members now start at website/, so extracting the archive drops that folder wherever you unpack it.
Create a .tgz File
A .tgz file is the same format under a shorter name, kept around for systems that limit filename extensions. Since tar takes the archive name from the -f option, no extra flag is needed:
tar -czf archive.tgz file1 file2Every command in this guide works the same whether you name the output .tar.gz or .tgz.
Exclude 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 .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 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page