How To Create Tar Gz File - Linuxize
Maybe your like
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 #
| 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:
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 file2On success, the command does not print any output. To verify the archive was created, list the directory contents with ls :
Terminalls -lh archive.tar.gzTo create the archive in a specific directory, provide the full path to the archive file:
Terminaltar -czf /home/user/archive.tar.gz file1 file2To see each file as it is added, include the -v (verbose) flag:
Terminaltar -czvf archive.tar.gz file1 file2outputfile1 file2Archive 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/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:
Terminaltar -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:
Terminaltar -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:
Terminaltar -czf archive.tar.gz --exclude='*.log' /var/www/websiteTroubleshooting #
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.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 .
If you have any questions, feel free to leave a comment below.
Tags
tarLinuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
SubscribeUnsubscribe anytime. We respect your inbox.
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 pageTag » Archive Tar.gz Unix
-
How To Create Tar.gz File In Linux Using Command Line - NixCraft
-
How To Compress And Extract Files Using The Tar Command On Linux
-
How To Extract / Unzip Tar.gz Files From Linux Command Line
-
Nén Và Giải Nén Trong Linux: Zip, Tar.gz Và 2
-
18 Useful Tar Command Examples For Every Linux Sysadmin
-
How To Create A .tar.gz File On Linux
-
Use Tar To Combine Multiple Files Into An Archive File - IU KB
-
How To Create Tar.gz File In Linux - Ubiq BI
-
Linux Tar Command – How To Compress Files In Linux
-
How To Tar A Folder In Linux - Linux Hint
-
Tar.gz - Linux Tar Command Examples - Techplayon
-
Tar Command In Linux With Examples - GeeksforGeeks
-
Tar [Wiki Ubuntu-fr]
-
Extraire Et Décompresser Les Fichiers .tar.gz (pour Linux Et Windows)