37

I have a bash script that creates a '.tar' file. Once the file is created, I would like to test its integrity and send an email to the root user if the integrity is bad.

I know I would need to use the command tar -tf /root/archive.tar to check the integrity of the file, but how would I implement this in a bash if statement and check for errors?

3 Answers 3

48

If tar finds errors in its input it will exit(3)¹ with a non-zero exit value. This — with most tar implementations — is also done when listing archive contents with t. So you could simply check for the exit value of tar to determine if something has gone wrong:

if ! tar tf /root/archive.tar &> /dev/null; then
    write_an_email_to_root
fi

If your tar does not find all errors with t, you could still extract the archive to stdout and redirect stdout to /dev/null, which would be the slower but more reliable approach:

if ! tar xOf /root/archive.tar &> /dev/null; then
    write_an_email_to_root
fi

¹ This notation denotes the manpage, not the actual call. See man 3 exit.

6
  • 1
    You'll probably want to redirect the tar output to /dev/null, as you probably don't actually want to see it.
    – Kevin
    May 16, 2014 at 4:09
  • 1
    Not all tar implementations detect or report all errors with t (bsdtar doesn't, you can use tar xOf file.tar > /dev/null there). Not all tar implementations would exit with exit status 2, star (255) ot bsdtar (1) don't, but what matters is that the exit status is non-zero here. May 16, 2014 at 6:18
  • 1
    I edited my post accordingly. With exit(2) I was regarding to the appropriate manpage, not the exact exit value (which is why I had »non-zero« explicitly mentioned in the following sentence). Annotated this (and changed the section to the right one -.-). May 16, 2014 at 14:50
  • your answer is only about capability of listing and extraction, if I open tar archive with an editor and change something your solution won't catch any kind of error ... sorry Jul 6, 2019 at 3:09
  • I agree with @THESorcerer because checking tar integrity meaning you need to compare the current tar file with the previous checksum. if it doesn't match, it's considered broken. But the answer is to validate whether the tar file is corrupt or not (normally we test it by listing and extracting at the background). So where is the integrity test ?
    – MaXi32
    Jun 1, 2020 at 14:56
2

sorry, but you cannot, seems like tar lacks the capability of testing (for example if you forget about *nix and try to test it with winrar, result will be: "The command is not supported for this type of archive" )

how I pointed out in comments above, tar literally lacks internal CRC to have a term of comparison, therefore if you change a tar archive with an editor, listing and extraction may work flawless with no error, but extract corrupted data

in conclusion, I end up here hoping for a solution, but there is not one, fortunately there are two good news:

there are very, very, VERY rare tar archives that are not also compressed with another program (like gzip, bzip2, etc.) therefore that program will have a testing solution and people who don't compress them are kinda lame, REALLY LAME

for my personal problem, I'm lucky, after extracting it, I just found a md5sum file with CRC of all files inside (even the person who put arhive tar on net and didn't want to compress it, he still wanted to be able to be verified)

3
  • 8
    No need to yell at people. There are plenty of reasons not to compress a tarfile. One of them is if you tar a dir of JPEGs that won't compress much more but take time and energy by running them through a compressor. May 7, 2020 at 20:36
  • 1
    and people who don't compress them are kinda lame, REALLY LAME wow... Exactly what @HolgerBöhnke says, there's absolutely no sense in recompressing a compressed file. An archive can as well be just a collection of already compressed files, merged into one singe file for convenience.
    – Daniel F
    Jun 5, 2020 at 21:41
  • Not sure why an answer stating there is no solution to the OP's problem would show up five years after an actual solution was posted.
    – mightypile
    Jul 16, 2022 at 20:47
-9

You can do it by passing the following arguments to tar:

$ tar -cvzf test.tar test_file
2
  • 2
    One-line answers are often not that helpful. Consider expanding your post to include some source material (e.g., documentation) that supports your solution.
    – HalosGhost
    Aug 7, 2014 at 5:56
  • 11
    -c creates the file named via -f ('test.tar'). -z gzips the archive. -v makes it verbose. I'm pretty sure this is not an accurate solution.
    – EpicVoyage
    Dec 28, 2015 at 5:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .