How To Create Tar Gz File - Linuxize

A tar archive stores a collection of files along with their metadata — 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 #

TaskCommand
Create from filestar -czf archive.tar.gz file1 file2
Create from a directorytar -czf archive.tar.gz /path/to/dir/
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/

For a printable quick reference, see the Tar cheatsheet .

Syntax #

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

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

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

Terminalls -lh archive.tar.gz

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

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

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

Terminaltar -czvf archive.tar.gz file1 file2outputfile1 file2

Archive a Directory #

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

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

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

Terminaltar -czf images.tar.gz *.jpg

Exclude Files and Directories #

Use --exclude to omit specific files or directories from the archive. Provide paths relative to the source:

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

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

Troubleshooting #

tar: file: Cannot stat: No such file or directoryOne 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 deniedYou 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 filesShell 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 expectedYou 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:

Terminaltar -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 .

If you have any questions, feel free to leave a comment below.

Tags

tar

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

Subscribe

Unsubscribe anytime. We respect your inbox.

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 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page

Tag » Archive Tar.gz Unix