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?

  • its been answered before here: superuser.com/questions/133186/… – Rhyuk Jul 13 '12 at 19:07
  • 3
    Actually, @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 Jul 13 '12 at 19:13

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

tar -cf tarfilename.tar -T /dev/null
  • 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 Jul 14 '12 at 19:41
  • 1
    One 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 Nov 29 '12 at 17:53
  • 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 Dec 16 '12 at 2:42
  • 1
    @SeanReifschneider It looks like that'd work fine. Nice. – dpk Dec 17 '12 at 23:14
  • 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 Jul 29 '13 at 8:40

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

  • Finally something that works in Solaris!! Thanks a lot – AJPerez Nov 23 '16 at 9:03

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
  • @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 Jan 15 at 6:58
  • I went ahead and edited the question. – Kamil Maciorowski Jan 15 at 7:30

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.

  • 2
    I can verify that with GNU tar this will produce the expected 10K file of all NUL characters. – Sean Reifschneider Jul 22 '12 at 6:00

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.