Everton Evaristo
Everton Evaristo

Reputation: 81

How to create an empty tar file in AIX

How do you create an empty tar file in AIX?

$touch myfile.tar doesn't work.

Upvotes: 6

Views: 2101

Answers (4)

Fravadona
Fravadona

Reputation: 17208

You can use the POSIX pax command for generating an empty TAR archive:

pax -x ustar -w < /dev/null > empty.tar

Different OSs will generate different file sizes (ranging from 1024 to 10240 Bytes in my tests), all of those containing only NUL Bytes. Hence, an other possibility would be to use dd for generating the empty TAR file.

remark: With the POSIX TAR header being defined as 512 Bytes, I don't understand why the generated files are bigger than that.


When you "untar" any of those files with:

tar xvf empty.tar

It gives no output, no error, and doesn't create any file nor directory.

Upvotes: 2

Lorinczy Zsigmond
Lorinczy Zsigmond

Reputation: 1910

An empty file won't do, it has to contain some zeroes (n*512 bytes) -- this comes from tar being tape archiver. (This zeroes are interpreted as EOF, that's why you cannot simply concatenate the output of more tar c commands.)

A file containing n*512 zeroes can be used with t (toc) and x (extract) operations, but not whith r (append) operation (at least with AIX's default tar).

Still you can create an archive containing a dummy file, which you can even remove at the end of the process with dd. Example script:

#!/bin/sh

set -ev

export PATH=/usr/bin

# create 'collect_tmp.tar' with a dummy file in it

echo 'These files have been restored from a tar-archive.' >ReadMeFromTar.TXT
tar -cvf collect_tmp.tar ReadMeFromTar.TXT
rm ReadMeFromTar.TXT
ls -l collect_tmp.tar

if [ $# -gt 0 -a "x$1" = 'xempty' ]; then
    echo empty
else
    # add some files into it 'collect_tmp.tar' with a dummy file in it

    tar -rvf collect_tmp.tar /etc/motd
    tar -rvf collect_tmp.tar /etc/hosts
    tar -rvf collect_tmp.tar "$0"

    tar -tvf collect_tmp.tar
fi

dd if=collect_tmp.tar of=collect.tar bs=1024 skip=1
rm collect_tmp.tar

tar -tvf collect.tar

Notes:

  1. The error message you get from AIX!tar when trying to append an empty (or containing n*512 zeroes) tar file: r tar: Unexpected end-of-file while reading from the storage media.

  2. Why 1024 bytes: tar adds 512 bytes long header (or a multiple of 512 in special cases) before the content of the file, and it stores the content padded with zeroes to n*512 bytes. So an empty file takes 512 bytes, a short file (like above) takes 1024 bytes in the tar-archive.

Off-topic: GNU!tar is way superior to AIX!tar (you can say it is over-featured), but is still adds these tailing zeroes (because of POSIX-compatibility, tape-compatibility, inertia) which is no problem, unless you want append to a compressed archive -- the zeroes between the old and new files won't be removed. Workaround:

/opt/freeware/bin/tar -b 1 ... -cf - |\
/opt/freeware/bin/head -c -1024 |\
/opt/freeware/bin/xz >>appendtome.tar.xz

Upvotes: 0

kvantour
kvantour

Reputation: 26491

Just creating an empty file will not be recognized as a tar-file. To be recognized as a tar-file, it must satisfy a particular format. There are multiple tar-file formats. An overview can be found here.

The IBM AIX operation system seems support the United States Tape Archiver (USTAR) format as well as the old UNIX format [AIX 1] and does this trough the command tar [AIX 2]. It can also deal with other interchangable formats such as pax [AIX 3] and cpio [AIX 4] via the commands pax and cpio. The USTAR format and the interchangable formats, pax and cpio, are all POSIX defined [POSIX 1]

Not directly supported by AIX, but worth mentioning is the wide-spread GNU-tar format [GNU 1].

All mentioned archiving formats have a similar format: a sequential list of file objects (meta-data and file content) followed by two zero-filled blocks (a block is 512 bytes big).

This means that, in order to create an empty tar-ball, you need to have no file objects but two zero-filled blocks. In other words, have minimally 1024 zero bytes in your file. The following two commands generate such file:

$ dd if=/dev/zero of=empty.tar bs=512 count=2
$ head --bytes 1024 /dev/zero > empty.tar

Note: there is something called blocking which just tells the system to perform IO-operations in chunks of BLOCKING_FACTOR * 512 bytes. In most systems (GNU, AIX) this value is set to 20 and unneeded garbage bytes are converted to zero's. That is why an empty file, generated with GNU tar is 10240 bytes big.

$ tar -cf empty.tar --files-from=/dev/null
$ stat -c "Size: %s" empty.tar
Size: 10240
$ od empty.tar
0000000 000000 000000 000000 000000 000000 000000 000000 000000
*
0024000

Upvotes: 3

Rahul
Rahul

Reputation: 77896

Try this. Works in Linux.

tar cvf your-empty-tar-filename.tar --files-from /dev/null

Upvotes: -1

Related Questions