tar Command in Linux

Topics Covered

Overview

The tar command is one of the most widely used commands in Linux. It is used to create, view, and extract files from archives. The tar command is a powerful tool that can be used to manage large amounts of data efficiently. In this article, we will discuss the syntax, options, and usage of the tar command in Linux.

Linux tar Command Syntax

The syntax for the tar command is as follows:

Where:

  • Options: Used to specify various actions to be taken by tar command.
  • File/directory: Specifies the name of the archive file to be created or the directory to be archived.

tar Command Options:

  • -c: Create a new archive.
  • -x: Extract files from an archive.
  • -t: List the contents of an archive.
  • -f: Use the following archive file.
  • -v: Verbose output.
  • -z: Compress or decompress the archive using gzip.
  • -j: Compress or decompress the archive using bzip2.

Example Usages

  • Create a new archive:

    Explanation: This command will create a new archive named 'archive.tar' and add the files 'file1.txt' and 'file2.txt' to it.

  • Extract files from an archive:

    Explanation: This command will extract all the files from the archive 'archive.tar' to the current directory.

  • List the contents of an archive:

    Explanation: This command will list the contents of the archive 'archive.tar'.

Tips

  • You can add multiple files to an archive by separating their names with a space.

  • You can use the wildcard character '*' to add all files in a directory to an archive.

Advanced Use Cases of tar Command in Linux

  • Compress an archive using gzip:

    Explanation: This command will create a compressed archive named 'archive.tar.gz' and add the files 'file1.txt' and 'file2.txt' to it using gzip compression.

  • Compress an archive using bzip2:

    Explanation: This command will create a compressed archive named 'archive.tar.bz2' and add the files 'file1.txt' and 'file2.txt' to it using bzip2 compression.

  • Extract files from a compressed archive:

    Explanation: This command will extract all the files from the compressed archive 'archive.tar.gz' to the current directory using gzip decompression.

Conclusion

  • The tar command is a powerful tool for managing archives in Linux.

  • You can use it to create, view, and extract archives.

  • It supports a wide range of options for customizing the archive creation and extraction process.

  • You can also use it to compress and decompress archives using gzip or bzip2 compression.

eam