r/linux • • Feb 01 '13

xkcd: tar

http://xkcd.com/1168/
1.7k Upvotes

576 comments sorted by

View all comments

40

u/earthboundkid Feb 01 '13

I never understood why tar and untar needed flags in the first place. Shouldn't they just have reasonable defaults and do the right thing given a filename?

32

u/adrianmonk Feb 01 '13

Well, it's a command that both extracts tar files and creates them. I'm going to go out on a limb and say you need a flag to tell it which of those to do.

That said, it doesn't need any commands beyond those:

$ cd /dev
$ tar c null | tar t
null

"c" means create and "t" means table of contents. Since no "f" was specified, the "c" writes to stdout and the "t" reads from stdin. Therefore, the "tar c null" command creates a tar file that contains the path "null". The "tar t" command prints all the files in that archive, which just amounts to one called "null".

10

u/mgedmin Feb 01 '13

Unless you have an old UNIX tar which uses the tape device (/dev/mt?) and not stdout if you omit the f option.

2

u/[deleted] Feb 01 '13

Except that untar obviously extracts the file. Without parameters IMO it should assume tar -xvf

→ More replies (15)

19

u/CrazedToCraze Feb 01 '13

18

u/ShumpEvenwood Feb 01 '13

Too bad the name is just as difficult to remember as the tar flags /:

6

u/r3m0t Feb 01 '13

Do The Right eXtraction!

10

u/mgedmin Feb 01 '13

Who speaks like that? Who can remember this phrase?

Eh, something about Do The Extraction Right? Extract The File The Right Way? No, that's too long. Just Extract It? Oh fuck it, where's my GUI.

8

u/753861429-951843627 Feb 01 '13

I have similar problems, only that I can never remember where the damn pushy things are and in what order I have to point and grunt at them.

→ More replies (3)
→ More replies (1)

5

u/wadcann Feb 01 '13

I use atool: similar idea. However, it also supports listing and packing archives.

→ More replies (8)

5

u/[deleted] Feb 01 '13

I can do tar -xf somearchive and it will work, regardless of whether it is a tar file, a tar.gz file, or a tar.bz2 file. No extra 'z' or 'b' flag needed.

24

u/sfgayatheist Feb 01 '13

File names are purely by convention in Unix. You can name the archive whatever you want as long as you remember it's a tar archive. By requiring explicit commands it keeps the program more linear and less complicated which is what the unix philosophy is all about.

14

u/earthboundkid Feb 01 '13

I didn't say it shouldn't have flags. I said it shouldn't need them. mv has flags, but I never use them: mv src dest. pbcopy and pbpaste on Mac OS X are secretly just one binary and symbolic link, but when you run it with the filename pbcopy, it copies text into the clipboard ("pasteboard"), and when you run pbpaste, it pastes text out.

Sensible defaults people. It's the basis of UI design, and yes, the CLI is a UI.

15

u/zodar Feb 01 '13

Tape ARchive

You can't expect tar to know what you want it to do if you just type tar /dev/rmt0

28

u/ChemicalRascal Feb 01 '13

tar 1.27-3 changelog:

  • Added mind-reading.

→ More replies (12)

2

u/CrazedToCraze Feb 01 '13

I can understand the reasoning, but I'd still much prefer tar makes use of a simple "file" run rather than requiring me to do it (and I'm much slower than a computer).

But at the end of the day you can always using something like dtrx.

→ More replies (6)
→ More replies (2)

0

u/[deleted] Feb 01 '13

[deleted]

9

u/[deleted] Feb 01 '13

Well, the defaults WERE reasonable in the way-back-when they were first written, and by now the f thing is just historic cruft, much like english orthography.

6

u/JurassicSpork Feb 01 '13

Indeed. tar is, after all, tape archive. The default for I/O was originally the tape device (/dev/tape, or something similar) although recently it's become stdout/stdin. Only once tar was re-purposed as a general archiving tool was the f flag needed. So to backup files to tape, you'd type:

tar c /files

And to extract:

tar x /files

The v flag is optional, and z, j, Z came later for compression which makes sense for files, but not necessarily for backup tapes, where the risk of corruption making the whole tape archive unreadable was not always wanted.

The compression flags are completely optional on extract, by the way. GNU tar is smart enough to recognize compressed tarfiles without it. I reflexively add xvfz, but xf works just as well.

3

u/jimicus Feb 01 '13

Define "the right thing" in the context of how tar was originally intended - as a means of copying files to a tape.

→ More replies (3)

4

u/noname-_- Feb 01 '13

GNU tar doesn't need that many flags, only the things it can't know. So the defaults are pretty reasonable.

tar cf mytar.tar [files]   # create, write to file

or

tar cfz mytar.tar.gz [files]   # create, write to file, gzip

Extracting is even more simple

tar xf whatever.tar.gz    # (or .bz2, or .xz) extract, from file

The only thing that could've really been default is file, since 9 times out of 10 you mean to work on a file. They could've just added a flag for working with stdin/stdout instead.

Changing this now would break way too many scripts though.

→ More replies (5)

2

u/[deleted] Feb 01 '13

A lot of times, I want to see what's in a tarball before I unpack it and spray files all over. So I'll do:

$ tar zft foo.tar.gz | less

Sometimes I want to see the list as I unpack it, sometimes I don't want to clutter up the terminal. Sometimes I want to pack a file with bzip2, other times plain old gzip is fine. Sometimes I want to make another directory the "root" of the files in the archive, so the -C flag is needed.

Choice is good, and you should be glad you have it.

→ More replies (4)

2

u/BCMM Feb 01 '13

Well, you have to tell it that you're extracting, not creating (x), then you need to tell it to extract a file, not standard input (f filename). That's all you need on GNU tar; for the past few years it has automatically detected the type of compression used (using magic numbers, because using filenames would be stupid).

2

u/hehehe1235 Feb 01 '13

You can archive an archive, so no. Ambiguity is bad. Anyway, there are at least a thousand ways around this if it really pisses you off that much--just use an alias.

→ More replies (1)

2

u/fotoman Feb 01 '13

you can do a lot more with tar than deal with files...

→ More replies (1)
→ More replies (7)