Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills
31.07.2025

How to untar a file in linux: An Advanced guide for power users

Extracting .tar, .tar.gz, .tar.bz2, and other tarball files is an essential skill in Linux system administration and DevOps workflows. While tar may seem simple at first, experienced users can use advanced flags and techniques for efficiency, scripting, and fine-grained control.

This guide will walk you through everything from basic decompression to conditional extraction, benchmarking, and working with corrupted backups.

What is a .tar file?

A .tar file (short for Tape Archive) is a consolidated archive that combines multiple files into one, preserving the file structure, permissions, and timestamps. By default, it is not compress ed. Compression is often overlaid with formats such as .gz, .bz2, .xz, or .zstd.

Basic extraction commands

1. Extracting a .tar file (without compression)

tar -xf archive.tar

2. Extract a .tar.gz or .tgz file

tar -xzf archive.tar.gz

3. Extract .tar.bz2 file

tar -xjf archive.tar.bz2

4. Extract .tar.xz file

tar -xJf archive.tar.xz

5. Extract .tar.zst file (Zstandard)

tar --use-compress-program=unzstd -xf archive.tar.zst

Common extended flags

FlagFunction
-xRetrieved from
-fUse archive file
-vSpecified output (list of files)
-CChange directory before extraction
–strip-components=NRemove the leading N elements of the path
–wildcardsEnable wildcard filtering
–no-same-ownerAvoid changing file owners on extraction
–overwriteForced overwrite without prompts
–exclude=PATTERNExclude files matching the pattern

Advanced examples

Extract to a specific directory

tar -xf archive.tar.gz -C /opt/myapp

Remove the top-level folder (flatten the structure)

tar -xf archive.tar.gz --strip-components=1

Extract specific files

tar -xf archive.tar.gz path/to/file1 path/to/file2

Extracting a matching pattern using wildcards

tar -xf archive.tar.gz --wildcards '*.conf'

Extraction with some paths excluded

tar -xf archive.tar.gz --exclude='*.log'

Benchmark extraction time

time tar -xf archive.tar.gz

Handling edge cases

? File is corrupted

Use tar with –ignore-zeros to skip the corrupted blocks:

tar -xzf broken.tar.gz --ignore-zeros

? Preview the archive before extracting it

tar -tf archive.tar.gz # List of files

Integrity check (for gzip-compressed tarballs)

gzip -t archive.tar.gz && echo "Archive OK"

Scripting tips for system administrators

Use tar in backup/restore scripts:

#!/bin/bash
TARGET_DIR="/var/www"
ARCHIVE="/backups/site-$(date +%F).tar.gz"

tar -czf "$ARCHIVE" -C "$TARGET_DIR" . && echo "Backup saved to $ARCHIVE"

Or automatically unpack and install:

#!/bin/bash
SRC="$1"
DEST="$2"
mkdir -p "$DEST"
tar -xzf "$SRC" -C "$DEST" --strip-components=1

Tar files are essential for packaging, archiving, and deploying Linux. By mastering extraction flags and integrating tar into scripts, you can manipulate archives with surgical precision – extract only what you need, preserve ownership, flatten paths, or automate entire workflows.

Quick reference

# Basic
tar -xf file.tar    # No compression
tar -xzf file.tar.gz # Gzip
tar -xjf file.tar.bz2 # Bzip2
tar -xJf file.tar.xz # XZ
tar --use-compress-program=unzstd -xf file.tar.zst    # Zstandard

# Common options
tar -xvf archive.tar    # Verbose
tar -C /target/dir -xf file.tar.gz    # Extract to folder
tar --strip-components=1 -xf file.tar.gz    # Remove top-level dir

Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills