Cheat Sheet #day56 - tar

Cheat Sheet #day56 - tar

tar Command Cheatsheet

The tar command is used to create, manipulate, and extract archive files. It is commonly used for bundling multiple files into a single file for easy distribution and backup.

Syntax

tar [OPTION]... [FILE]...

Common Options

  • -c: Create a new archive.

  • -x: Extract an archive.

  • -t: List the contents of an archive.

  • -f FILE: Specify the archive file name.

  • -v: Verbose mode, display the progress.

  • -z: Filter the archive through gzip (use with .tar.gz).

  • -j: Filter the archive through bzip2 (use with .tar.bz2).

  • -J: Filter the archive through xz (use with .tar.xz).

  • --delete: Delete files from the archive.

Examples

Create a Tar Archive

tar -cf archive.tar file1.txt file2.txt
  • Creates archive.tar containing file1.txt and file2.txt.

Create a Tar.gz Archive

tar -czf archive.tar.gz file1.txt file2.txt
  • Creates archive.tar.gz with gzip compression.

Create a Tar.bz2 Archive

tar -cjf archive.tar.bz2 file1.txt file2.txt

Create a Tar.xz Archive

tar -cJf archive.tar.xz file1.txt file2.txt
  • Creates archive.tar.xz with xz compression.

Extract a Tar Archive

tar -xf archive.tar
  • Extracts archive.tar.

Extract a Tar.gz Archive

tar -xzf archive.tar.gz
  • Extracts archive.tar.gz.

Extract a Tar.bz2 Archive

tar -xjf archive.tar.bz2

Extract a Tar.xz Archive

tar -xJf archive.tar.xz
  • Extracts archive.tar.xz.

List Contents of an Archive

tar -tf archive.tar
  • Lists contents of archive.tar.

Verbose Listing

tar -tvf archive.tar
  • Lists contents of archive.tar with details.

Extract to a Specific Directory

tar -xf archive.tar -C /path/to/directory
  • Extracts archive.tar to /path/to/directory.

Add Files to an Existing Archive

tar -rf archive.tar newfile.txt
  • Adds newfile.txt to archive.tar.

Delete Files from an Archive

tar --delete -f archive.tar file_to_delete.txt
  • Deletes file_to_delete.txt from archive.tar.

Combining Options

You can combine options for more concise commands. For example:

tar -czvf archive.tar.gz file1.txt file2.txt
  • Creates archive.tar.gz with gzip compression, verbose mode.

This cheatsheet covers the most common usages and options for the tar command. For more detailed information, refer to the tar man page by typing man tar in your terminal.

Did you find this article valuable?

Support Cloud Tuned by becoming a sponsor. Any amount is appreciated!