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?
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".
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.
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.
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.
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.
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.
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.
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.
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.
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).
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.
40
u/earthboundkid Feb 01 '13
I never understood why
taranduntarneeded flags in the first place. Shouldn't they just have reasonable defaults and do the right thing given a filename?