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 read•Last updated: January 19, 2025
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Create
tar -cvf a.tar dir/Create archivetar -czvf a.tar.gz dir/Create gziptar -cjvf a.tar.bz2 dir/Create bzip2Extract
tar -xvf a.tarExtract archivetar -xzvf a.tar.gzExtract gziptar -xvf a.tar -C dir/Extract to dirList
tar -tvf a.tarList contentstar -tzvf a.tar.gzList gzip contentstar -tf a.tar | grep patSearch archiveOptions
-cCreate archive-xExtract archive-vVerbose-fSpecify file-zgzip compression-jbzip2 compressionDownloadable 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
| -c | Create a new archive |
| -x | Extract files from archive |
| -t | List contents of archive |
| -v | Verbose output |
| -f | Specify archive filename |
| -z | Compress with gzip (.tar.gz) |
| -j | Compress with bzip2 (.tar.bz2) |
| -J | Compress 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.tarExtract a gzip compressed archive
bash
tar -xzvf archive.tar.gzExtract 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.txtInfo
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.gzAdvanced 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.gzAdd files to existing archive
bash
# Append to uncompressed tar
tar -rvf archive.tar newfile.txtWarning
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/documentsBackup 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.gzCompression 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 -czvfto create gzip archives - Use
tar -xzvfto extract gzip archives - Use
tar -tvfto list contents - Use
-Cto specify extraction directory - Use
--excludeto skip files/directories
Official Documentation
For authoritative information, refer to the official documentation: