Search code examples
linuxposixcommand-line-interfacegnutar

How to determine a tar archive's format


In Linux, how can I determine the format which was used to create a given tarball?

I'd like to detect whether a given archive was created using posix or gnu format.

I've already read the man page for GNU tar, which explains how to control the format during creation. However, I don't see anything about how to view the format of an existing file.


Solution

  • You can use file under Linux to look at the fingerprint of the uncompressed archive:

    $ touch foo                                 # create test file
    $ tar --format=posix -cf posix.tar foo      # create test posix archive
    $ tar --format=gnu   -cf gnu.tar   foo      # create test gnu archive
    $ file posix.tar gnu.tar
    posix.tar: POSIX tar archive
    gnu.tar:   POSIX tar archive (GNU)
    

    If the archive is compressed, decompress it first, because file won't peer beyond the compression layer:

    $ touch foo                                 # create test file
    $ tar --format=posix -czf posix.tar.gz foo  # create test gzip posix archive
    $ tar --format=gnu   -czf gnu.tar.gz   foo  # create test gzip gnu archive
    $ file posix.tar.gz gnu.tar.gz              # show output when compressed
    posix.tar.gz: gzip compressed data
    gnu.tar.gz:   gzip compressed data
    $ gunzip posix.tar.gz                       # decompress to posix.tar
    $ gunzip gnu.tar.gz                         # decompress to gnu.tar
    $ file posix.tar gnu.tar                    # show output after decompression
    posix.tar: POSIX tar archive
    gnu.tar:   POSIX tar archive (GNU)
    

    Or, check the compressed archives without saving the decompressed file by piping the output directly to file's standard input:

    $ gunzip --stdout posix.tar.gz | file -
    /dev/stdin: POSIX tar archive
    $ gunzip --stdout gnu.tar.gz | file -
    /dev/stdin: POSIX tar archive (GNU)
    

    GNU is based on an older POSIX format, so that is why it says it is both.

    For the nitty gritty details, the format is described in the GNU tar manual here and more details here.