Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 365
Christopher Avatar asked Jun 02 '16 23:06

Christopher


People also ask

How is the file of tar format?

A tar (tape archive) file format is an archive created by tar, a UNIX-based utility used to package files together for backup or distribution purposes. It contains multiple files (also known as a tarball) stored in an uncompressed format along with metadata about the archive. Tar files are not compressed archive files.

What reads a .tar file?

WinZip opens and extracts TAR Compressed Archive Files—and many more formats.

What is a tar 1 file?

In computing, tar is a computer software utility for collecting many files into one archive file, often referred to as a tarball, for distribution or backup purposes. The name is derived from "tape archive", as it was originally developed to write data to sequential I/O devices with no file system of their own.


1 Answers

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.

like image 183
rrauenza Avatar answered Nov 03 '22 01:11

rrauenza