Catalog / Tar Command Cheatsheet

Tar Command Cheatsheet

A comprehensive cheat sheet for the tar (tape archive) command, covering common operations like creating, extracting, compressing, and listing archives, along with practical examples.

Basic Operations

Creating Archives

Create a basic tar archive:

tar -cf archive.tar /path/to/files

-c: create a new archive.
-f: specify the archive file name.

Create a gzipped tar archive:

tar -czf archive.tar.gz /path/to/files

-z: compress the archive with gzip.

Create a bzip2 compressed tar archive:

tar -cjf archive.tar.bz2 /path/to/files

-j: compress the archive with bzip2.

Create a xz compressed tar archive:

tar -cJf archive.tar.xz /path/to/files

-J: compress the archive with xz.

Adding verbosity to creation:

tar -cvf archive.tar /path/to/files

-v: enables verbose mode, it lists the files being processed during archive creation.

Extracting Archives

Extract a tar archive:

tar -xf archive.tar

-x: extract files from an archive.

Extract a gzipped tar archive:

tar -xzf archive.tar.gz

-z: decompress the archive with gzip.

Extract a bzip2 compressed tar archive:

tar -xjf archive.tar.bz2

-j: decompress the archive with bzip2.

Extract a xz compressed tar archive:

tar -xJf archive.tar.xz

-J: decompress the archive with xz.

Extracting with verbosity:

tar -xvf archive.tar

-v: enables verbose mode, it lists the files being processed during extraction.

Extract to specific directory:

tar -xf archive.tar -C /path/to/destination

-C: specifies the destination directory.

Advanced Usage

Listing Archive Contents

List the contents of a tar archive:

tar -tf archive.tar

-t: list the contents of the archive.

List the contents of a gzipped tar archive:

tar -tzf archive.tar.gz

-z: decompress the archive with gzip.

List the contents of a bzip2 compressed tar archive:

tar -tjf archive.tar.bz2

-j: decompress the archive with bzip2.

List the contents of a xz compressed tar archive:

tar -tJf archive.tar.xz

-J: decompress the archive with xz.

Listing verbosely:

tar -tvf archive.tar

-v: enables verbose mode, displaying file permissions, size, modification date, and owner.

Appending Files

Append files to an existing tar archive:

tar -uf archive.tar /path/to/new_file

-u: append files to the archive if they are newer than the existing copies or do not exist in the archive.

Note: Appending to compressed archives is not directly supported. You’ll need to extract, append, and then re-compress.

Excluding Files

Exclude specific files or directories when creating an archive:

tar -czf archive.tar.gz /path/to/files --exclude='/path/to/exclude'

--exclude: exclude specified pattern.

Using a file containing a list of patterns to exclude:

tar -czf archive.tar.gz /path/to/files --exclude-from=exclude_file.txt

--exclude-from: read exclude patterns from file.

Compression Options

Gzip

Using gzip for compression:

tar -czf archive.tar.gz /path/to/files

Suitable for general-purpose compression.

Adjusting gzip compression level (1-9, default is 6):

gzip -9 file.txt

-9: best compression (slowest).
-1: fastest compression (least compression).

Bzip2

Using bzip2 for compression:

tar -cjf archive.tar.bz2 /path/to/files

Generally provides better compression than gzip but is slower.

Adjusting bzip2 compression level (1-9):

bzip2 -9 file.txt

-9: best compression (slowest).
-1: fastest compression (least compression).

XZ

Using xz for compression:

tar -cJf archive.tar.xz /path/to/files

Offers the best compression ratio but is the slowest. Good for archiving data for long-term storage where space is a premium.

Adjusting xz compression level (0-9):

xz -9 file.txt

-9: best compression (slowest).
-0: fastest compression (least compression).

Troubleshooting

Common Issues

‘Cannot open: No such file or directory’

Verify the path to the archive or files exists and is accessible.

‘Not in gzip format’ or similar errors during extraction

Ensure you are using the correct extraction flag (-z, -j, -J) for the archive’s compression type.

Permission denied errors

Use sudo if you lack permissions to create or extract archives in the specified location. Also, review the permissions of the files being archived.

Truncated or Corrupted Archive

This may happen if the archive creation was interrupted or the download was incomplete. Try recreating the archive or re-downloading it.

Large Files and Archives

For very large archives, consider using pigz (parallel gzip) or lbzip2 (parallel bzip2) for faster compression and decompression. Install them separately if they’re not already available:

sudo apt-get install pigz lbzip2

Then, use them with tar:

tar --use-compress-program=pigz -czf archive.tar.gz /path/to/files

Filename Length Limitations

Older versions of tar may have limitations on filename lengths. Using GNU tar (common on Linux systems) generally avoids these issues. For compatibility, consider the --format=gnu option:

tar --format=gnu -czf archive.tar.gz /path/to/files