Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 get it to create a truly empty tar file?

share|improve this question
its been answered before here:… – Rhyuk Jul 13 '12 at 19:07
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
up vote 17 down vote accepted

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

tar cfT tarfilename.tar /dev/null
share|improve this answer
Maybe you're using some strange version of GNU tar, but most people will probably need something more like tar -c -T /dev/null -f tarfilename.tar. Not that most people will ever need to create an empty tar archive... – jjlin Jul 14 '12 at 4:24
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
@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
@SeanReifschneider It looks like that'd work fine. Nice. – dpk Dec 17 '12 at 23:14
@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

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.

share|improve this answer
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

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

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .