How to Create a tar.gz File in Linux

By 

Updated on

5 min read

Creating a tar.gz archive file in Linux using the tar command

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 .

TaskCommand
Create from filestar -czf archive.tar.gz file1 file2
Create from a directorytar -czf archive.tar.gz /path/to/dir/
Archive without parent pathtar -czf archive.tar.gz -C /path/to/parent dirname
Create a .tgz filetar -czf archive.tgz file1 file2
Create with verbose outputtar -czvf archive.tar.gz file1 file2
Create from wildcard matchtar -czf images.tar.gz *.jpg
Exclude a file or directorytar -czf archive.tar.gz --exclude=dir /path/
Create in a specific locationtar -czf /backup/archive.tar.gz /path/to/dir/
List archive contentstar -tzf archive.tar.gz

Syntax

The general syntax for creating a .tar.gz file is:

txt
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:

Terminal
tar -czf archive.tar.gz file1 file2

On success, the command does not print any output. To verify the archive was created, list the directory contents with ls :

Terminal
ls -lh archive.tar.gz

To create the archive in a specific directory, provide the full path to the archive file:

Terminal
tar -czf /home/user/archive.tar.gz file1 file2

To see each file as it is added, include the -v (verbose) flag:

Terminal
tar -czvf archive.tar.gz file1 file2
output
file1
file2

Archive a Directory

To create a .tar.gz archive from the contents of a directory, pass the directory path as the source:

Terminal
tar -czf web_backup.tar.gz /var/www/website

To confirm what went into the archive, list its contents with the --list (-t) option:

Terminal
tar -tzf web_backup.tar.gz
output
var/www/website/
var/www/website/index.html
var/www/website/css/style.css

By 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:

Terminal
tar -czf images.tar.gz *.jpg

Archive 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:

output
tar: Removing leading '/' from member names

The 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:

Terminal
tar -czf web_backup.tar.gz -C /var/www website

The 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:

Terminal
tar -czf archive.tgz file1 file2

Every 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:

Terminal
tar -czf archive.tar.gz --exclude=node_modules --exclude='.git' /var/www/website

To exclude multiple patterns, repeat --exclude for each one. You can also use wildcards:

Terminal
tar -czf archive.tar.gz --exclude='*.log' /var/www/website

Troubleshooting

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:

Terminal
tar -cf - file1 file2 | gzip > archive.tar.gz

This 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

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