18 Tar Command Examples in Linux

Introduction

Before we discuss 18 tar command examples in Linux, let's first understand-What is Tar Command?

The tar command in Linux is used for archiving files and directories into a single file or extracting contents from an existing archive. It is a versatile tool that offers various options for compression, file manipulation, and preserving metadata.

In this tutorial, we will provide an introduction to the tar command, along with its examples.

Advantages

  1. File Compression: The tar command allows for compressing files and directories while archiving them. This helps save storage space and makes it easier to transfer or share files.
  2. Preserving Permissions and Metadata: When creating an archive, tar preserves file permissions, ownership, timestamps, and other metadata. This ensures that the archived files retain their original attributes when extracted.
  3. Multiple Archive Formats: tar supports various archive formats, such as uncompressed TAR, TAR with gzip compression, TAR with bzip2 compression, and more. This provides flexibility in choosing the compression method based on the specific requirements.
  4. Simplicity and Portability: tar is a common command available on almost all Linux distributions, making it highly portable. It has a simple syntax and is easy to use, making it accessible for both beginners and advanced users.
  5. Extensive Options and Features: tar offers a wide range of options and features, including excluding files/directories, appending to an existing archive, creating incremental backups, and more. This allows users to tailor the archiving process precisely to their needs.

1. Creating a Tar Archive File

The below example of the tar command will create a tar archive file vegastack-17-11-2023.tar for a directory /home in the current working directory.

See the example of the tar command in action.

tar -cvf vegastack-17-11-2023.tar /home

Let’s discuss each option used in the above tar command.

  • c – Creates a new .tar archive file.
  • v – Verbosely show the .tar file progress.
  • f – File name type of the archive file.

2. Creating a Tar Archive with Compression

To create a compressed archive file, we use the option 'z' (compress the archive using gzip). For example, the command below will generate a compressed file named 'MyImages-17-11-2023.tar.gz' for the directory /home/MyImages. (Note: 'tar.gz' and 'tgz' are interchangeable terms).

tar cvzf MyImages-17-11-2023.tar.gz /home/vegastack/MyImages 
OR 
tar cvzf MyImages-17-11-2023.tgz /home/vegastack/MyImages

3. Creating a tar.bz2 File in Linux

The bz2 feature compresses and creates an archive file that is smaller in size compared to gzip. However, the bz2 compression method requires more time for both compression and decompression, whereas gzip is faster in both processes.

To create a highly compressed new tar archive named Phpfiles-org.tar.bz2 by bundling all files and subdirectories within the /home/php directory, use the -j option, which instructs tar to utilize the bzip2 compression algorithm, resulting in a smaller file size for efficient storage and transfer.

💡
Note: tar.bz2 and tbz are similar terms, both referring to tb2.
tar cvfj Phpfiles-org.tar.bz2 /home/vegastack/php 
OR 
tar cvfj Phpfiles-org.tar.tbz /home/vegastack/php 
OR 
tar cvfj Phpfiles-org.tar.tb2 /home/vegastack/php

4. Extracting a Tar Archive

To untar or extract a tar file, simply execute the following command using the 'x' option (extract). For instance, the command below will untar the file named vegastack-17-11-2023.tar in the present working directory.

tar -xvf vegastack-17-11-2023.tar

If you want to untar in a different directory then use option -C (specified directory).

tar -xvf vegastack-17-11-2023.tar -C /home/vegastack/

5. Extracting a Compressed tar.gz Archive

To extract the contents of a compressed tar archive file named MyImages-17-11-2023.tar.gz, use the following command.

tar -xvf MyImages-17-11-2023.tar.gz

If you would like to extract in a different directory, just use the option -C, which will extract the files into the specified directory as shown.

tar -xvf MyImages-17-11-2023.tar.gz -C /home/vegastack/

6. Extracting a tar.bz2 Archive

To uncompress the highly compressed tar.bz2 file, simply use the following command, which will untar all the files from the archive file.

tar -xvf Phpfiles-org.tar.bz2

7. Listing Contents of tar Archive

To list or view the contents of the tar archive file, simply run the following command with the -t option (list content), which will display a detailed list of files and directories contained within the vegastack-17-11-2023.tar archive.

tar -tvf vegastack-17-11-2023.tar

8. Viewing Contents of tar.gz Archive

The following command will display a detailed list of files and directories contained within the MyImages-17-11-2023.tar.gz archive.

tar -tvf MyImages-17-11-2023.tar.gz

9. Printing Contents of tar.bz2 Archive

The following command provides an overview of the contents within the Phpfiles-org.tar.bz2 archive without extracting the files.

tar -tvf Phpfiles-org.tar.bz2

10. Extracting a Single File from an Archive

To extract a single file named wp-cron.php from the archive Phpfiles-org.tar.bz2, use the following command. Make sure to provide the correct path to the file you wish to extract.

tar -xvf Phpfiles-org.tar.bz2 home/vegastack/php/wp-cron.php

11. Extracting Multiple Files from an Archive

To extract or untar multiple files from tar, tar.gz, and tar.bz2 archive files, use the following command, which will extract files from the specified archive files.

tar -xvf vegastack-17-11-2023.tar "file1" "file2" 
tar -zxvf MyImages-17-11-2023.tar.gz "file1" "file2" 
tar -jxvf Phpfiles-org.tar.bz2 "file1" "file2"

12. Extract a Group of Files Using Wildcard in Linux

To extract a group of files we use wildcard-based extracting. For example, to extract a group of all files whose pattern begins with .php from a tar, tar.gz, and tar.bz2 archive files, use:

tar -xvf Phpfiles-org.tar --wildcards '*.php' 
tar -zxvf Phpfiles-org.tar.gz --wildcards '*.php' 
tar -jxvf Phpfiles-org.tar.bz2 --wildcards '*.php'

13. Appending Files to an Existing Archive

To add files or directories to the existing tar, tar.gz, and tar.bz2 archive files, use the option -r, which will add the files to an existing archive file.

tar -rvf vegastack-14-09-12.tar xyz.txt 
tar -rvf MyImages-14-09-12.tar.gz xyz.txt 
tar -rvf Phpfiles-org.tar.bz2 xyz.txt

14. Verifying a Tar Archive File

The following command will display a detailed list of files and directories contained within the specified archive file, allowing you to visually verify the archive’s contents. If the archive is corrupted or incomplete, this verification process may reveal errors during the listing.

tar -tvf Phpfiles-org.tar.bz2

15. Checking Tar Archive File Size

To check the size of any tar, tar.gz, and tar.bz2 archive file, use the following command, which will display the size of the archive file in Kilobytes (KB).

tar -czf - vegastack-14-09-12.tar xyz.txt | wc -c 
tar -czf - MyImages-14-09-12.tar.gz xyz.txt | wc -c 
tar -czf - Phpfiles-org.tar.bz2 xyz.txt | wc -c

16. Excluding Files When Creating a Tar Archive

To exclude certain files and directories while creating a tar archive file, you can use the following command with the --exclude an option that will exclude files and directories when creating the tar archive file as shown.

tar --exclude='file1.txt' -zcvf backup.tar.gz /home/vegastack 
tar --exclude='/home/vegastack/uploads' -zcvf backup.tar.gz /home/vegastack

In the above command, we excluded file file1.txt and 'uploads' directory from the /home/vegastack folder.

To exclude files with specific file extensions (.txt) when creating a tar archive file, use:

tar --exclude='*.txt' -zcvf backup.tar.gz /home/vegastack

17. Removing Files From a Tar Archive

The following tar command will delete a file or directory from an already created tar file using the --delete option, as shown.

tar --delete -f backup.tar.gz file1.txt tar --delete -f backup.tar.gz '/home/vegastack/uploads'

18. Extracting File Extension From a Tar Archive

The following tar command will only extract files with the specific extension .png from the tar archive file using the --wildcards option as shown.

tar -xvf backup.tar.gz --wildcards '*.png'

19. Tar Command Usage and Options

Understanding the following various options and usage patterns of the tar command is essential for efficient file archiving, compression, and extraction.

  • -c – create an archive file.
  • -x – extract an archive file.
  • -v – show the progress of the archive file.
  • -f – filename of the archive file.
  • -t – viewing the content of the archive file.
  • -u – archives and adds to an existing archive file.
  • -j – filter the archive through bzip2.
  • -z – filter the archive through gzip.
  • -r – append or update files or directories to the existing archive files.
  • -W – Verify an archive file.
  • -A – concatenates the archive files.
  • --wildcards – Specify patterns in the UNIX tar command.
  • --exclude – excludes files and directories when creating the archive.
  • --delete – remove the file and directory from the archive.

That’s it for now, hope the above tar command examples are enough for you to learn, and for more information please use the man tar command.

FAQs- Tar Command Examples in Linux:

How do I archive files and directories using the tar command?

Use the command tar -cvf <archive-file.tar> <file1> <file2> <directory> to create a new archive file containing the specified files and directories.

How do I extract files from a tar archive?

Run tar -xvf <archive-file.tar> to extract all the files from the specified tar archive to the current directory.

How can I extract a specific file from a tar archive?

Use the command tar -xvf <archive-file.tar> <file> to extract a specific file from the tar archive.

How do I compress a tar archive using gzip? 

Run tar -czvf <archive-file.tar.gz> <file1> <file2> <directory> to create a gzip-compressed tar archive.

How can I extract a tar.gz archive?

Use the command tar -xzvf <archive-file.tar.gz> to extract all the files from the gzip-compressed tar archive.

How can I create a bzip2-compressed tar archive? 

Run tar -cjvf <archive-file.tar.bz2> <file1> <file2> <directory> to create a bzip2-compressed tar archive.

How do I exclude files/directories during archiving? 

Use the option --exclude=<pattern> with the tar command to exclude specific files or directories. For example, tar --exclude='*.txt' -cvf <archive-file.tar> <directory>.

Conclusion

We discussed about tar command along with examples in Linux, and also we have answered some of the FAQs.

If you have any queries, feel free to ask them in the comments section and, we will be happy to respond to them...