Back to Blog

How to Compress and Extract Files With TAR

How to Compress and Extract Files With TAR
Command of the Day
tar -czvf archive.tar.gz /path/to/folder

What it does: Creates a gzip-compressed tar archive with verbose progress output.

The tar (Tape Archive) command is the undisputed standard in the Linux world for packaging and compressing files and directories into a single archive file.

Whether creating automated server backups, distributing source code, or saving disk space on your RivinCloud VPS, mastering tar flags is a foundational skill.


1. Understanding Core TAR Flags

Remembering tar flags is easy once you understand their individual meanings:

  • -c: Create a new archive.
  • -x: Extract files from an archive.
  • -v: Verbose mode (shows the list of files being processed).
  • -f: File name (must immediately precede the archive name).
  • -t: Test/List table of contents without extracting.
  • -z: Filter archive through gzip (.tar.gz).
  • -j: Filter archive through bzip2 (.tar.bz2).
  • -J: Filter archive through xz (.tar.xz).
  • -C: Change directory before extracting.

2. Creating Archives

1. Create a Standard Gzip Archive (.tar.gz)

The most widely used format balancing speed and compression ratio:

tar -czvf backup.tar.gz /var/www/html/

2. Create High-Compression XZ Archive (.tar.xz)

Best for cold storage backups where maximum compression is desired:

tar -cJvf cold_backup.tar.xz /var/data/

3. Exclude Specific Folders (e.g. node_modules or cache)

tar --exclude='node_modules' --exclude='.git' -czvf project.tar.gz ./my-project

3. Extracting Archives

1. Extract a .tar.gz Archive to Current Directory

tar -xzvf backup.tar.gz

2. Extract to a Specific Destination Directory (-C)

tar -xzvf backup.tar.gz -C /var/www/restored/

3. Extract a .tar.xz Archive

tar -xJvf backup.tar.xz

4. Extract a Single Specific File from an Archive

tar -xzvf backup.tar.gz var/www/html/config.php

4. Inspecting Archives Without Extracting

To check what files are inside a tarball without filling up disk space:

tar -tvf backup.tar.gz

To search for a specific file inside:

tar -tvf backup.tar.gz | grep "wp-config.php"

Common Mistakes

  • Putting the -f flag before other letters: The -f flag expects the archive filename as its immediate next parameter. Always put f last (e.g., -czvf file.tar.gz, not -cfzv file.tar.gz).
  • Extracting without checking archive paths: Always check contents with -tvf first to avoid accidentally dumping hundreds of uncontained files into your current working directory.

Troubleshooting

  • "tar: Error is not recoverable: exiting now": Often caused by a corrupted download or insufficient free disk space in the destination filesystem. Check df -h.
  • "Cannot open: No such file or directory": When using -C /target/path, ensure the target destination directory exists before running tar.

Best Practices

[!TIP] Use .tar.gz for regular daily backups due to its fast compression speed and low CPU overhead. For large database dumps or monthly long-term storage, use .tar.xz for maximum disk space savings.


Frequently Asked Questions

What is the difference between .tar and .tar.gz?

A .tar file is simply an archive (packaging multiple files into one file with no compression). A .tar.gz file is that archive compressed using gzip algorithms.

How do I compress multiple folders into one tarball?

Simply list them separated by spaces:

tar -czvf site_and_db.tar.gz /var/www/html /backups/db.sql

Conclusion

The tar command is an indispensable utility for Linux administrators. Use these commands to streamline your archiving, backup, and extraction tasks on your RivinCloud VPS!