Creating and extracting tar.gz archives on Linux

Linux 3 min read

On Linux, the most common format for archiving files and folders is tar.gz (or .tgz): the tar program collects the files into a single archive, along with their permissions and owners, whilst gzip compresses them. You will most commonly encounter this format when performing backups, conducting migrations or downloading programs. For information on handling ZIP files, see the article ‘Extracting a ZIP file to a specified folder’.

Creating an archive

tar -czvf weboldal.tar.gz weboldal/

The meaning of the switches:

  • -c (create): create a new archive,
  • -z: gzip compression,
  • -v (verbose): lists the processed files (optional),
  • -f: followed by the name of the archive. This must always appear at the end of the command: in the test, the command tar -cfz w.tgz weboldal created an archive named z, whilst w.tgz was treated as a file to be packed (“Cannot stat: No such file or directory”).

You can specify multiple files or folders:

tar -czvf mentes.tar.gz public_html/ adatbazis.sql .htaccess

Excluding folders and files

tar -czvf weboldal.tar.gz --exclude='weboldal/wp-content/cache' --exclude='*.log' weboldal/

The date in the file name

When performing regular backups, it is useful if the file name includes the date:

tar -czf weboldal-$(date +%Y-%m-%d).tar.gz weboldal/

Extracting the archive

# Kicsomagolás az aktuális könyvtárba
tar -xzvf weboldal.tar.gz

# Kicsomagolás egy megadott könyvtárba (a célkönyvtárnak léteznie kell)
mkdir -p /var/www/uj
tar -xzvf weboldal.tar.gz -C /var/www/uj

If the directory specified after -C does not exist, tar will stop with the following error: tar: /var/www/uj: Cannot open: No such file or directory.

The -x (extract) switch performs extraction. Extraction overwrites files with the same name in the destination directory without prompting.

View content without unpacking

tar -tzvf weboldal.tar.gz

The -t (list) switch lists the contents of the archive. Before extracting the archive, it is worth checking whether it contains a folder or the files directly; in the latter case, the files will be scattered throughout the current directory.

Extracting a single file

Enter the file path exactly as it appears in the output of the tar -tzf command:

tar -xzvf weboldal.tar.gz weboldal/wp-config.php

Other compression formats

The compression type is determined by the switch:

Extension Switch Features
.tar.gz, .tgz -z fast, accessible anywhere
.tar.bz2 -j smaller file, slower
.tar.xz -J the smallest file, the slowest
.tar.zst --zstd fast and highly compressed; requires the zstd program
.tar (none) uncompressed

When extracting files, the modern tart automatically recognises the format, so the compression switch can be omitted:

tar -xvf archivum.tar.xz

tar calls an external program for compression. On the AlmaLinux 10 cloud image, zstd was not installed by default: tar --zstd resulted in the error zstd: command not found. Install it (sudo dnf install zstd or sudo apt install zstd); gzip and xz are available everywhere.

Compressing a single file using gzip

If you only want to compress a single file (such as a database backup or log file), there is no need to use tar-ra`:

# Tömörítés: a fájl helyére adatbazis.sql.gz kerül
gzip adatbazis.sql

# Kicsomagolás
gunzip adatbazis.sql.gz

# Tömörítés úgy, hogy az eredeti is megmaradjon
gzip -k adatbazis.sql

Permissions and owners

tar stores file permissions and ownership within the archive. Upon extraction, it restores ownership only to the root user; when extracted by a standard user, the extracted files become the property of the user performing the extraction. In the test, a file owned by root/root (as shown by the tar -tvzf command) became the property of the user when extracted as a standard user. If the web server cannot access the files after extraction, check the ownership and permissions (see: File permissions (chmod, chown) on Linux).

If you specify an absolute path (e.g. tar -czf mentes.tar.gz /var/www), tar removes the leading / character (and displays the message: tar: Removing leading `/' from member names) and stores the files under the name var/www/.... When extracting, the files are therefore created in the current directory (or at the location specified with the -C switch) and do not overwrite the files in their original locations.

Back: Linux articles (62)