Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

tar Command Linux Examples

Last Updated on December 11, 2023 by Abhishek Sharma


The tar command in Linux is a versatile utility used for creating, extracting, and managing archive files. Named after "tape archive," it was initially designed for tape backup purposes but has evolved to become a fundamental tool for bundling and compressing files and directories in Unix-like operating systems. In this article, we will explore the various functionalities of the tar command along with illustrative examples.

What is tar Command in Linux?

The tar command in Linux stands as a cornerstone for file archiving and compression, allowing users to bundle files and directories into a single archive. Originating from the tape archive days, the tar command has evolved to become a powerful utility for efficiently managing data in Unix-like operating systems. Its name, short for "tape archive," reflects its historical association with tape backup processes. In modern computing, however, tar is a fundamental tool for creating, extracting, and compressing archives, contributing significantly to streamlined file management and data backup procedures.

Basic Syntax of tar command Linux:

The basic syntax of the tar command involves several options and arguments:

tar options archive_name file1 file2 ... fileN
  • options: Additional settings or flags to customize the archiving process.
  • archive_name: The name of the resulting archive file.
  • file1, file2, …, fileN: The files or directories to be included in the archive.

Examples of tar command in Linux

Below are some examples of tar command in Linux:

1. Creating a Tar Archive:
To create a tar archive named archive.tar containing files file1.txt and file2.txt, the command is:

tar -cvf archive.tar file1.txt file2.txt
  • -c: Create a new archive.
  • -v: Verbosely list the files processed.

2. Compressing with gzip:
To compress a tar archive using gzip, use the -z option:

tar -czvf archive.tar.gz file1 file2
  • -z: Compress the archive using gzip.

3. Extracting Tar Archive:
To extract the contents of a tar archive, use the -x option:

tar -xvf archive.tar
  • -x: Extract files from an archive.

4. Compressing with bzip2:
To compress a tar archive using bzip2, use the -j option:

tar -cjvf archive.tar.bz2 file1 file2
  • -j: Compress the archive using bzip2.

5. Extracting bz2 Archive:
To extract a bzip2 compressed archive, use the -j option:

tar -xjvf archive.tar.bz2

6. Listing Archive Contents:
To list the contents of a tar archive without extracting, use the -t option:

tar -tvf archive.tar
  • -t: List the contents of an archive.

7. Extracting to a Specific Directory:
To extract files to a specific directory, use the -C option:

tar -xvf archive.tar -C /path/to/directory
  • -C: Change to the specified directory before extracting.

8. Creating Incremental Backups:
To create an incremental backup, use the –listed-incremental option:

tar --create --file=backup.tar --listed-incremental=backup.snar /path/to/data

9. Excluding Files:
To exclude specific files while creating an archive, use the –exclude option:

tar -cvf archive.tar --exclude="*.log" /path/to/directory

10. Verifying Archive Integrity:
To verify the integrity of an archive, use the -W option:

tar -Wtvf archive.tar
  • -W: Verify an archive file.

Conclusion
The tar command in Linux is a powerful and flexible tool for archiving and managing files and directories. By combining it with various options, users can create, extract, and compress archives efficiently. Whether you’re creating backups, distributing files, or organizing data, the tar command remains an indispensable utility in the Linux command-line toolkit. Experiment with different options and use cases to become proficient in leveraging the full capabilities of the tar command.

Frequently Asked Questions (FAQs) about the ‘tar’ Command in Linux

Below are some of the FAQs related to tar command in Linux:

1. What is the primary purpose of the tar command in Linux?
The tar command is primarily used for creating, extracting, and managing archive files. It enables users to bundle multiple files and directories into a single archive for various purposes, including backup and distribution.

2. How do I create a tar archive using the tar command?
To create a tar archive, use the tar -cvf command followed by the desired archive name and the list of files to be included. For example: tar -cvf archive.tar file1 file2.

3. Can I compress a tar archive?
Yes, the tar command supports compression with options like -z for gzip and -j for bzip2. For instance: tar -czvf archive.tar.gz file1 file2 (gzip compression).

4. How do I extract the contents of a tar archive?
To extract the contents of a tar archive, use the tar -xvf command followed by the archive name. For example: tar -xvf archive.tar.

5. What is the difference between gzip and bzip2 compression with the tar command?
Both gzip and bzip2 are compression algorithms supported by the tar command. Gzip generally provides faster compression and decompression, while bzip2 often achieves higher compression ratios at the cost of slightly slower performance.

Leave a Reply

Your email address will not be published. Required fields are marked *