iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🈳

Creating an Empty Archive (tar)

に公開

Introduction

August 2024 is about to end. Since such an absurdity cannot be allowed, let's resist absurdity in a completely unrelated way.

Refusal to create empty archives

The syntax for creating an archive with GNU tar(1) is as follows. The argument FILE... appears to be optional.

tar --create [--file ARCHIVE] [OPTIONS] [FILE...]

However, when you actually try to run it omitting the FILE... argument, you get scolded with "Cowardly refusing to create an empty archive".

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

Creating an empty archive no matter what

To create an empty archive, we will follow these steps:

  1. Create an archive containing a file.
  2. Delete all files contained in the archive.
  3. An empty archive is completed.

Let's actually try it.

First, create an archive containing a file. Any file to be stored in the archive will do, but we will create an empty file dummy and specify it.

$ >dummy
$ tar --create --file=empty.tar dummy
$ tar --list --verbose --file=empty.tar
-rw-rw-r-- mkn/mkn           0 2024-08-24 20:12 dummy

Then, delete the member dummy from the resulting archive. To delete a member from an archive, use tar --delete.

$ tar --delete --file=empty.tar dummy
$ tar --list --verbose --file=empty.tar

Now an empty archive with no members is complete.

Examining the empty archive

What kind of file does an empty archive actually look like? Let's dump the file contents using everyone's favorite od(1).

$ od empty.tar 
0000000 000000 000000 000000 000000 000000 000000 000000 000000
*
0024000

I see, it seems to be a collection of 10,240 null bytes.

Easily creating an empty archive

In other words, you can easily create an empty archive by running the following command:

$ dd if=/dev/zero of=empty.tar bs=512 count=20
20+0 records in
20+0 records out
10240 bytes (10 kB, 10 KiB) copied, 0.000130402 s, 78.5 MB/s

Conclusion

That's it.

The original idea for this article is from this tweet in 2020.

Discussion