Let's say I have a folder Documents and a TAR file Documents.tar, how to check if the tar file contains the same files that are present in the directory?

The more obvious solution to me would be to do:

$ tar xvf Documents.tar -C untarDocs
$ diff -r Documents untarDocs

Unfortunately this is very slow for large TAR files, is there any other alternative?

Using tar -dvf Documents.tar (or --diff, --compare) doesn't work because it doesn't detect a file that is present in the filesystem but not in the TAR file, it just detects a file present in the TAR file but not in the filesystem e.g.:

$ mkdir new
$ touch new/foo{1..4}
$ tar cvf new.tar new/
$ touch new/bar
$ tar --diff --verbose --file=new.tar       #### doesn't detect new/bar #########
$ rm new/foo1
$ tar --diff --verbose --file=new.tar


tar: new/foo1: Warning: Cannot stat: No such file or directory   ### works ###
  • 1
    Do you just want to compare file names or also content and metadata? – Gilles 'SO- stop being evil' Nov 15 '15 at 20:27
  • @Gilles comparing file names would be enough. – marcanuy Nov 15 '15 at 21:03
  • So see my answer and use diffopts=to reduce the amount of meta data to compare. – schily Nov 15 '15 at 23:39

If you want only to compare lists of file- and directory-names, the -d option is not helpful. Instead, diff'ing sorted lists from find and tar -tf would do that.

Starting with the names assumed in OP's original example:

$ tar xvf Documents.tar -C untarDocs
$ diff -r Documents untarDocs

here is a suggested script to diff the filenames:

MYDIR=$(mktemp -d)
tar tf Documents.tar |sort >$MYDIR/from-tar
find Documents |sort >$MYDIR/from-dir
(cd $MYDIR && diff -r from-tar from-dir)
rm -rf $MYDIR

This assumes that Documents.tar contains the same top-level "Documents" directory. If that is not a good assumption, then the lists should be filtered to remove the name of the top-level directory. OP did not indicate that this would be a problem, however.

In any case, the lists must be sorted, because there is no guarantee which order the tar and find programs use.

I used mktemp because of the clue that OP is using GNU tar (the -d option), which makes it likely on Linux.

There is of course no POSIX tar for reference with regard to -d. pax does not do diff's either.

| improve this answer | |
  • Works great. I am using GNU tar as you assumed. – marcanuy Nov 16 '15 at 17:07

With a standard tar archive, this is not possible. This is because a standard tar archive does not include the list of directory contents.

With the star enhacements that are present when star is told to archive everything that is needed for incremental backups, it works:

1) star -c -dump -C somedir . > /tmp/tarfile

2) star -diff -vv -C comparedir < /tmp/tarfile

You may reduce the amount of metadata that is compare using the diffopts= option, see man page.

| improve this answer | |
  • Thanks for you answer It provides another approach, but I do not have star just GNU tar and I must deal with existing tar files, so I am not able to reconstruct them. – marcanuy Nov 16 '15 at 17:12
  • Star can easily be compiled from sources, it is maintained since 1982 and there are packages for most existing linux distros. Check: the schily tool bundle at: sourceforge.net/projects/schilytools/files – schily Nov 16 '15 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.