Terminal GuideTerminal Guide

tar Command Guide

tar (tape archive) is the standard tool for creating and extracting archives in Linux. Learn how to compress and decompress files effectively.

6 min readLast updated: January 19, 2025
Dai Aoki

Dai Aoki

CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm

Quick Reference

Create

tar -cvf a.tar dir/Create archive
tar -czvf a.tar.gz dir/Create gzip
tar -cjvf a.tar.bz2 dir/Create bzip2

Extract

tar -xvf a.tarExtract archive
tar -xzvf a.tar.gzExtract gzip
tar -xvf a.tar -C dir/Extract to dir

List

tar -tvf a.tarList contents
tar -tzvf a.tar.gzList gzip contents
tar -tf a.tar | grep patSearch archive

Options

-cCreate archive
-xExtract archive
-vVerbose
-fSpecify file
-zgzip compression
-jbzip2 compression

Downloadable Image Preview

Failed to generate preview

Basic Concepts

tar combines multiple files into a single archive file. It's often used with compression utilities like gzip or bzip2.

Key Options

-cCreate a new archive
-xExtract files from archive
-tList contents of archive
-vVerbose output
-fSpecify archive filename
-zCompress with gzip (.tar.gz)
-jCompress with bzip2 (.tar.bz2)
-JCompress with xz (.tar.xz)

Creating Archives

Create a tar archive (no compression)

bash
tar -cvf archive.tar files/

Create a gzip compressed archive (.tar.gz)

bash
tar -czvf archive.tar.gz files/

Create a bzip2 compressed archive (.tar.bz2)

bash
tar -cjvf archive.tar.bz2 files/

Create an xz compressed archive (.tar.xz)

bash
tar -cJvf archive.tar.xz files/
Tip
The letters in -czvf can be remembered as: Create, gZip, Verbose, Filename.

Extracting Archives

Extract a tar archive

bash
tar -xvf archive.tar

Extract a gzip compressed archive

bash
tar -xzvf archive.tar.gz

Extract to a specific directory

bash
tar -xzvf archive.tar.gz -C /path/to/destination/

Extract specific files

bash
tar -xzvf archive.tar.gz file1.txt file2.txt
Info
Modern versions of tar can auto-detect compression format, so you can often just use tar -xvf for any archive type.

Listing Contents

bash
# List contents without extracting
tar -tvf archive.tar.gz

# List only filenames
tar -tf archive.tar.gz

Advanced Options

Exclude files

bash
# Exclude specific files
tar -czvf archive.tar.gz --exclude="*.log" files/

# Exclude directories
tar -czvf archive.tar.gz --exclude="node_modules" project/

# Exclude from file
tar -czvf archive.tar.gz --exclude-from=exclude.txt files/

Include only certain files

bash
# Archive only .txt files
tar -czvf archive.tar.gz --include="*.txt" files/

Preserve permissions

bash
# Create with permissions
tar -cpzvf archive.tar.gz files/

# Extract with permissions
tar -xpzvf archive.tar.gz

Add files to existing archive

bash
# Append to uncompressed tar
tar -rvf archive.tar newfile.txt
Warning
You cannot append to compressed archives directly. You'll need to extract, add files, and re-compress.

Practical Examples

Backup a directory

bash
tar -czvf backup_$(date +%Y%m%d).tar.gz /home/user/documents

Backup with exclusions

bash
tar -czvf project.tar.gz \
  --exclude="node_modules" \
  --exclude=".git" \
  --exclude="*.log" \
  project/

Archive and split into parts

bash
# Create and split into 100MB parts
tar -czvf - large_directory/ | split -b 100M - archive.tar.gz.

# Combine and extract
cat archive.tar.gz.* | tar -xzvf -

Transfer directory to another machine

bash
# Using SSH
tar -czvf - directory/ | ssh user@host "tar -xzvf - -C /destination/"

Compare archive with filesystem

bash
tar -dvf archive.tar.gz

Compression Comparison

Compression Methods

gzip (.tar.gz)Fast, widely compatible, good compression
bzip2 (.tar.bz2)Better compression, slower than gzip
xz (.tar.xz)Best compression, slowest

Summary

tar is essential for archiving and backup tasks. Key takeaways:

  • Use tar -czvf to create gzip archives
  • Use tar -xzvf to extract gzip archives
  • Use tar -tvf to list contents
  • Use -C to specify extraction directory
  • Use --exclude to skip files/directories

Official Documentation

For authoritative information, refer to the official documentation:

Related Articles