1. Overview

Tape archives (tar) are a widely used format in Linux for packaging multiple files into a single file. For reliability purposes, we sometimes need to verify the integrity of a tar archive. For example, this can be the case when the archive contains important data, such as a system backup.

In this tutorial, we’ll learn the commands to check whether a tar archive integrity is good. Furthermore, we’ll look at how to create an archive with bad integrity, and then we’ll verify our commands to see the correct result.

2. Sample tar Archive

Firstly, let’s create a sample tar archive. For that, we’ll use two files, file1 and file2, and package them into test.tar:

$ tar -cf test.tar file1 file2

Option -c is used to create a new archive, while option -f is for specifying the tar archive name.

If we see no errors, the command should create the archive named test.tar, which we’ll use in the rest of this tutorial.

3. Check tar Integrity

There are at least a couple of ways to check a tar archive’s integrity. Let’s look at two methods and see their advantages and limitations.

3.1. Content Listing

One way of checking the integrity of our tar archive is the content listing:

$ tar -tf test.tar
file1
file2

Here, option -t is used to list the archive content.

We can see that both files are listed and no errors occurred, which means that the archive’s integrity is good.

If we’d like to use this command in an automated script, we can hide its output and check its exit status:

$ tar -tf test.tar &> /dev/null; echo $?
0

Here, the &> operator redirects the output to the /dev/null to hide the listed files, while the command echo $? displays the exit code.

The exit code is 0, so our archive has no errors.

3.2. Content Extraction

The above method is quick, but in some situations, it may not cover all possible errors.

A more reliable way to check the archive’s integrity is to extract all its content to stdout and see if errors occur:

$ tar xOf test.tar &> /dev/null; echo $?
0

Option -x is used to extract the files from the archive, while option -O directs the extracted contents to stdout. The rest of the command is similar to the previous one. We hide the output by using the redirection and print the output exit code at the end of the command.

As a result, our integrity check shows a 0 exit code, which stands for success.

4. Test With a Broken tar Archive

In the previous section, we checked how our commands work when the tar archive integrity is good. Now, let’s test them when a tar archive has corrupted files in it.

4.1. Corrupting the Archive

Firstly, we’ll use the dd command to corrupt our existing test.tar archive:

$ dd if=/dev/zero of=test.tar bs=1 count=1 seek=1 conv=notrunc
1+0 records in
1+0 records out
1 byte copied, 0,00188376 s, 0,5 kB/s

This adds one byte to the middle of our test.tar, which partly corrupts the archive.

4.2. Check Integrity

Let’s do the integrity check now and see if the status code shows any error.

We’ll use the content listing method first:

$ tar -tf test.tar &> /dev/null; echo $?
2

The status error is 2, which means that the integrity is bad now as there are some errors during the archive content listing.

Let’s repeat the test for the content extraction method:

$ tar xOf test.tar &> /dev/null; echo $?
2

Similarly, the output shows a status error.

5. Conclusion

In this article, we’ve looked at the ways to check a tar archive’s integrity. Firstly, we learned two commands to do this. Secondly, we checked how these commands work for the tar files of both good and bad integrity.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.