Cheatsheet: Tar

Last updated 2026-09-21

Basic Archiving Commands

Create an archive from a directory

tar -cvf archive_name.tar /path/to/directory

Create a compressed archive using gzip

tar -czvf archive_name.tar.gz /path/to/directory

Create a compressed archive using bzip2

tar -cjvf archive_name.tar.bz2 /path/to/directory

Create an archive from multiple files

tar -cvf archive_name.tar file1 file2 file3

Create an archive without baking in the full source path (cd into the dir first with -C, archive '.')

tar -czvf archive_name.tar.gz -C /path/to/directory .

Create an archive while excluding matching files (e.g. skip logs)

tar -czvf archive_name.tar.gz --exclude='*.log' /path/to/directory

Extracting Archives

Extract a tar archive

tar -xvf archive_name.tar

Extract a gzip-compressed tar archive

tar -xzvf archive_name.tar.gz

Extract a bzip2-compressed tar archive

tar -xjvf archive_name.tar.bz2

Extract to a specific directory

tar -xvf archive_name.tar -C /path/to/destination

Extract only one specific file from the archive

tar -xzvf archive_name.tar.gz path/to/file.txt

Extract files matching a glob pattern (needs --wildcards, GNU tar)

tar -xzvf archive_name.tar.gz --wildcards '*.txt'

Listing & Verifying Archive Contents

List the contents of an archive

tar -tvf archive_name.tar

List the contents of a gzip-compressed archive

tar -tzvf archive_name.tar.gz

List the contents of a bzip2-compressed archive

tar -tjvf archive_name.tar.bz2

Count how many entries an archive contains, without extracting

tar -tzf archive_name.tar.gz | wc -l

Compare an already-extracted archive's files against what's on disk (real content check, unlike -t)

tar -df archive_name.tar

Advanced Options

Add files to an existing uncompressed archive (append doesn't work on .tar.gz/.tar.bz2)

tar -rvf archive_name.tar new_file

Delete a file from an archive (use with caution)

tar --delete -f archive_name.tar file_to_remove

Create an archive and split it into multiple parts

tar -cvf - /path/to/directory | split -b 500M - archive_part

Extract a single file from an archive

tar -xvf archive_name.tar file_name

FAQ