Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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: superuser.com/questions/133186/… –  Rhyuk Jul 13 '12 at 19:07
1  
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

2 Answers 2

up vote 11 down vote accepted

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

tar cfT tarfilename.tar /dev/null
share|improve this answer
1  
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
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
2  
@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

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
1  
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.