Tar Command - Archive and Compression Utility

Learn how to use the tar command for creating, extracting, and managing archive files. Covers common tar operations for .tar, .tar.gz, and .tar.bz2 formats.

Tar Command Guide

The tar command is a powerful utility for creating, extracting, and manipulating archive files, commonly used on Unix-like operating systems. It's essential for bundling multiple files into a single archive and can be combined with compression utilities like gzip or bzip2 to reduce file size.

Extracting Archives with Tar

To extract an uncompressed archive:

tar -xvf /path/to/foo.tar

To extract a tar archive into a specified directory:

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

To extract a .tgz or .tar.gz archive:

tar -xzvf /path/to/foo.tgz
tar -xzvf /path/to/foo.tar.gz

To extract a .tar.bz2 archive:

tar -xjvf /path/to/foo.tar.bz2

Creating Archives with Tar

To create an uncompressed archive:

tar -cvf /path/to/foo.tar /path/to/foo/

To create a .tgz or .tar.gz archive:

tar -czvf /path/to/foo.tgz /path/to/foo/
tar -czvf /path/to/foo.tar.gz /path/to/foo/

To create a .tar.bz2 archive:

tar -cjvf /path/to/foo.tar.bz2 /path/to/foo/

Listing Archive Contents

To list the content of a .tgz or .tar.gz archive:

tar -tzvf /path/to/foo.tgz
tar -tzvf /path/to/foo.tar.gz

To list the content of a .tar.bz2 archive:

tar -tjvf /path/to/foo.tar.bz2

Advanced Tar Operations

To create a .tgz archive and exclude specific file types:

tar -czvf /path/to/foo.tgz --exclude=\*.{jpg,gif,png,wmv,flv,tar.gz,zip} /path/to/foo/

To use parallel (multi-threaded) implementation of compression algorithms:

# For gzip
tar -z ... -> tar -Ipigz ...
# For bzip2
tar -j ... -> tar -Ipbzip2 ...
# For xz
tar -J ... -> tar -Ipixz ...

To append a new file to an existing tar archive:

tar -rf <archive.tar> <new-file-to-append>

External Resources