How to get an empty tar archive?

24

3

I am making an RPM in which everything is contained in the .spec file (don't ask :-). rpmbuild requires a "Source" file, so I was trying to create an empty tar file. However, if I don't give any filenames, tar complains:

tar: Cowardly refusing to create an empty archive
Try `tar --help' or `tar --usage' for more information.

How do I create a truly empty tar file?

Sean Reifschneider

Posted 2012-07-13T19:05:49.380

Reputation: 1 387

its been answered before here: http://superuser.com/questions/133186/forcing-tar-to-create-an-empty-archive

– Rhyuk – 2012-07-13T19:07:46.587

3Actually, @Rhuyuk, it hasn't. That post is someone who forgot to list a filename, and the answer helped them realize they needed to pass another argument, resulting in a non-empty archive. This is why I changed the title of that question, and created this question, which DOES answer what the title of that question was asking. – Sean Reifschneider – 2012-07-13T19:13:40.603

Answers

24

You can force GNU and BSD tar to create an empty archive with:

tar -cf tarfilename.tar -T /dev/null

Sean Reifschneider

Posted 2012-07-13T19:05:49.380

Reputation: 1 387

ya, 'f' option must be last, as it specifies the filename. You're going to create an archive named T, but it'll error on missing tarfilename.tar file anyways. – lornix – 2012-07-14T19:41:34.987

Thanks for the pointers, this was a typo above, I didn't mean to include the "-", without it GNU tar 1.26 works fine. Others are probably better with the options split out, but I don't have a system with various tars on to test it with. – Sean Reifschneider – 2012-07-22T05:58:16.263

1One reason to create an empty tar: scripted tar creation. Step 1: Create the tar. Step 2: Add files one by one. You can do this with an "if first file then create else then append" but that's not as smooth. – dpk – 2012-11-29T17:53:52.113

4@dpk: If I understand what you are saying, why don't you just always use append? Append will create the tar file if it doesn't already exist, so you don't need to check if it's the first file, just append and if it's the first file tar will create it. – Sean Reifschneider – 2012-12-16T02:42:38.473

1@SeanReifschneider It looks like that'd work fine. Nice. – dpk – 2012-12-17T23:14:40.150

1@SeanReifschneider: +1 for the hint that append mode creates the archive for the first file if it's not already on disk. I'm using find command to exec tar in append mode in a script so didn't need to create the archive file before-hand. – GuruM – 2013-07-29T08:40:03.737

8

BSD: tar cvf empty.tar --from-file /dev/null

GNU (Linux): tar cvf empty.tar --files-from /dev/null

Solaris: tar cvf empty.tar -I /dev/null

Sild

Posted 2012-07-13T19:05:49.380

Reputation: 181

Finally something that works in Solaris!! Thanks a lot – AJPerez – 2016-11-23T09:03:38.287

6

An empty tar file is just a file with 10240 NUL bytes in it. So to create an empty tar file, you don't even need tar but instead can use either of these:

$ head --bytes=10240 /dev/zero > empty.tar
$ truncate --size=10240 empty.tar
$ fallocate --length=10240 empty.tar
$ dd if=/dev/zero bs=10240 count=1 iflag=fullblock > empty.tar
$ dd if=/dev/zero bs=1 count=10240 > empty.tar

josch

Posted 2012-07-13T19:05:49.380

Reputation: 517

@KamilMaciorowski thanks for your hints about dd usage! I learned something new! About answering the right question: The title indeed asks "how to force tar" but when reading the question text, it becomes clear, that OP actually wants an empty tar file and it should not matter how. – josch – 2019-01-15T06:58:43.807

I went ahead and edited the question. – Kamil Maciorowski – 2019-01-15T07:30:16.233

3

I was able to create an empty archive by archiving a single file, then deleting the file from the archive.

tar cf empty.tar somefile                   # creates archive
tar --delete -f empty.tar somefile          # remove the file
tar tf empty.tar                            # list contents of archive

This works for me.

lornix

Posted 2012-07-13T19:05:49.380

Reputation: 9 633

2I can verify that with GNU tar this will produce the expected 10K file of all NUL characters. – Sean Reifschneider – 2012-07-22T06:00:10.410