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)
2. Extract a .tar.gz or .tgz file
3. Extract .tar.bz2 file
4. Extract .tar.xz file
5. Extract .tar.zst file (Zstandard)
Common extended flags
Flag | Function |
---|---|
-x | Retrieved from |
-f | Use archive file |
-v | Specified output (list of files) |
-C | Change directory before extraction |
–strip-components=N | Remove the leading N elements of the path |
–wildcards | Enable wildcard filtering |
–no-same-owner | Avoid changing file owners on extraction |
–overwrite | Forced overwrite without prompts |
–exclude=PATTERN | Exclude files matching the pattern |
Advanced examples
Extract to a specific directory
Remove the top-level folder (flatten the structure)
Extract specific files
Extracting a matching pattern using wildcards
Extraction with some paths excluded
Benchmark extraction time
Handling edge cases
? File is corrupted
Use tar with –ignore-zeros to skip the corrupted blocks:
? Preview the archive before extracting it
Integrity check (for gzip-compressed tarballs)
Scripting tips for system administrators
Use tar in backup/restore scripts:
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