Ubuntu tar Basics: Compression, Extraction & Avoiding Common Mistakes
What You'll Learn
- How to compress, extract, and list contents with
tar - Understand the differences between
.tar.gz/.tgz/.tar - How to avoid common accidents (extracting to wrong locations, overwriting, path issues)
💡 Quick Summary
Start with these commands - they'll cover most cases:
- Create (gzip):
tar -czf archive.tar.gz DIR/ - Extract:
tar -xzf archive.tar.gz - List contents:
tar -tzf archive.tar.gz - Specify destination:
tar -xzf archive.tar.gz -C /path/to/dir
Table of Contents
⚠️ Prerequisites
- OS: Ubuntu
- Shell: bash
- Permissions: Regular user (use
sudowhen needed)
1. tar File Formats Overview
.tar: Files bundled together (no compression).tar.gz/.tgz: Bundled with tar, compressed with gzip.tar.bz2: Bundled with tar, compressed with bzip2.tar.xz: Bundled with tar, compressed with xz
💡 Most commonly seen: .tar.gz / .tgz. Start with these.
2. Creating Archives (Compression)
2-1. Create .tar.gz from Directory (Basic)
$ tar -czf archive.tar.gz DIR/
c: createz: gzip compressionf: specify filename (required - without it, unexpected behavior may occur)
2-2. Bundle Multiple Files/Directories
$ tar -czf archive.tar.gz DIR1/ DIR2/ file.txt
3. Extracting Archives
3-1. Extract to Current Directory (Basic)
$ tar -xzf archive.tar.gz
x: extractz: gzipf: specify file
3-2. Specify Destination Directory (Recommended for Safety)
$ tar -xzf archive.tar.gz -C /path/to/dir
💡 Until you're comfortable, always use -C to avoid extraction accidents.
4. Listing Contents (Always Check Before Extracting)
4-1. List All Contents (.tar.gz)
$ tar -tzf archive.tar.gz
4-2. View Just the Beginning (for Long Lists)
$ tar -tzf archive.tar.gz | head
5. Common Accidents & Prevention (Most Important)
❌ Accident A: Extracting to Wrong Location
Causes:
- Not knowing your current directory
- Not using
-Cflag
Prevention:
- Check current directory before extracting:
pwd - Always specify destination with
-C
$ pwd $ tar -xzf archive.tar.gz -C /tmp
❌ Accident B: Archive Contains Absolute Paths (e.g., starts with /)
Danger:
- Archive paths like
/etc/...are absolute paths - Extraction could overwrite system files
Prevention:
- Check paths with
tar -tzfbefore extracting - When uncertain, extract to safe location like
/tmpfirst
$ mkdir -p /tmp/tar-test $ tar -xzf archive.tar.gz -C /tmp/tar-test
❌ Accident C: Overwriting Existing Files
Prevention:
- Don't extract directly to production directories
- First extract to empty working directory and verify contents
$ mkdir -p ~/work/extract-test $ tar -xzf archive.tar.gz -C ~/work/extract-test
⚠️ Accident D: Files Scattered Instead of Single Directory
Depending on how the archive was created, files may not be in a single directory.
Best Practice (when creating):
- Navigate to the parent of the directory you want to archive
- Example: To create
DIR/as a single directory
$ cd /path/to $ tar -czf DIR.tar.gz DIR/
6. Other Formats (.tar / .tar.bz2 / .tar.xz)
Use these when needed:
6-1. .tar (No Compression)
Create:
$ tar -cf archive.tar DIR/
Extract:
$ tar -xf archive.tar
6-2. .tar.bz2
Extract:
$ tar -xjf archive.tar.bz2
6-3. .tar.xz
Extract:
$ tar -xJf archive.tar.xz
Verification
After extraction, verify files are in the expected location:
$ ls -la
📋 Test Environment
Commands tested on Ubuntu 24.04 LTS / bash 5.2.