Tar Cheat Sheet

Tar is a widely used command-line utility for archiving and compressing files and directories on Linux systems. It provides a convenient way to create and manage compressed tar archives. Here is a handy cheat sheet for using tar with different operations.

Compressing Data

To compress data into a tar archive using gzip compression, use the following format:

tar -zcvf <name-of-tar>.tar.gz <file-or-directory-to-tar/>

Example:

tar -zcvf /tmp/etc-backup.tar.gz etc/

Explanation of flags:

  • -z: Use GZip compression
  • -c: Create a new archive
  • -v: Show Verbose output (optional)
  • -f: Specify the Filename for the resulting tar archive

Extracting Compressed Tar using GZip

To extract a compressed tar archive using gzip, use the following format:

tar -xzvf <name-of-tar>.tar.gz [-C <path-to-extract-to>]

Example:

tar -xzvf etc-backup.tar.gz -C /tmp

Explanation of flags:

  • -x: Extract existing tar archive
  • -z: Use GZip decompression
  • -v: Show Verbose output (optional)
  • -f: Specify the Filename of the tar archive to extract from
  • -C: Optional Change to a directory before extracting

Displaying Content of Tar using GZip

To display the content of a compressed tar archive using gzip, use the following format:

tar -tf <name-of-tar>.tar.gz

Example:

tar -tf etc-backup.tar.gz

Explanation of flags:

  • -t: List the content of the tar archive
  • -f: Specify the Filename of the tar archive to display

These commands and flags will help you efficiently work with tar archives, compress and decompress data, and manage files and directories on Linux systems.

References