0
0
Linux-cliHow-ToBeginner · 3 min read

How to Create tar.gz File in Linux: Simple Command Guide

To create a tar.gz file in Linux, use the tar command with the -czf options followed by the archive name and files or directories to include. For example, tar -czf archive.tar.gz folder/ compresses the folder into a gzip compressed tarball.
📐

Syntax

The basic syntax to create a tar.gz file is:

  • tar: the command to create or extract tar archives
  • -c: create a new archive
  • -z: compress the archive using gzip
  • -f: specify the filename of the archive
  • archive.tar.gz: the name of the output compressed file
  • files_or_directories: the files or folders you want to include
bash
tar -czf archive.tar.gz files_or_directories
💻

Example

This example creates a tar.gz archive named backup.tar.gz from a folder called project. It compresses the folder and all its contents into one file.

bash
tar -czf backup.tar.gz project/
⚠️

Common Pitfalls

Some common mistakes when creating tar.gz files include:

  • Forgetting the -f option, which causes tar to write to standard output instead of a file.
  • Using the wrong order of options, which can cause errors or unexpected behavior.
  • Not specifying the correct path, resulting in missing files in the archive.

Always use tar -czf followed by the archive name and then the files or directories.

bash
tar -cz backup.tar.gz project/  # Wrong: missing -f option

# Correct way:
tar -czf backup.tar.gz project/
📊

Quick Reference

OptionMeaning
-cCreate a new archive
-zCompress archive with gzip
-fSpecify archive filename
-vVerbose output (show files processed)
-xExtract files from archive (not for creation)

Key Takeaways

Use the command 'tar -czf archive.tar.gz files_or_directories' to create a tar.gz file.
The '-c' option creates the archive, '-z' compresses it with gzip, and '-f' specifies the filename.
Always include the '-f' option to avoid writing output to the terminal.
Check file paths carefully to ensure all desired files are included.
Use '-v' for verbose output to see which files are added during creation.