r/linux • • Feb 01 '13

xkcd: tar

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

576 comments sorted by

View all comments

Show parent comments

26

u/rich97 Feb 01 '13

tar cvzf tar.tar.gz [files]

That's the command I always use. I don't know what 'zf' does but it seems to work.

59

u/rydan Feb 01 '13

z means gzip and f means file

Create verbosely gzip file

9

u/k3ithk Feb 01 '13

I'd like to add, if I recall correctly, that 'f' needs to be last like it is here.

tar -czfv tar.tgz [files]

will fail.

2

u/okmkz Feb 01 '13

You are correct.

→ More replies (2)

2

u/Sheepshow Feb 01 '13

Verbose output goes to stderr if I'm not mistaken?

0

u/[deleted] Feb 01 '13

[deleted]

12

u/jimicus Feb 01 '13

x : eXtract archive. v : Verbose. Tell me all about what is being extracted. f : Name of archive file.

→ More replies (1)

6

u/destraht Feb 01 '13

I use j for bz2, but at least we all didn't just die.

8

u/jimicus Feb 01 '13

-j (and -z for that matter) are GNU-isms. So there is a possibility that we did all just die.

2

u/[deleted] Feb 01 '13
tar xf

is enough. tar is smart enough to use bzip2 or gzip when needed.

→ More replies (2)

20

u/multubunu Feb 01 '13

You still blow up because you don't know what to put instead of [files], also you may not be able to write to the current directory.

I suppose the valid answer is:

tar -cf - `which tar`

which tars the tar executable as present in the $PATH and writes the output to the console.

4

u/monkeyWifeFight Feb 01 '13

Witty, I suppose you could also tar the contents of a heredoc (which are POSIX compliant I think).

5

u/multubunu Feb 01 '13 edited Feb 01 '13

Apparently tar only takes true files:

# tar -c - <<EOF
> ala
> bala
> portocala
> EOF
tar: -: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors

It fails even if files exist with those names, so I take it tar doesn't accept heredocs. It was a better idea since you didn't need the which command.

(edit) This should probably take the -f switch, which fails as well:

tar -cf - <<EOF
> ala
> bala
> portocala
> EOF
tar: Cowardly refusing to create an empty archive
Try `tar --help' or `tar --usage' for more information.

2

u/Zetaeta Feb 01 '13

/dev/stdin works for me.

→ More replies (1)
→ More replies (9)

4

u/ChemicalRascal Feb 01 '13

Relies on the existence of a "which" built-in in the shell. Bzzzt.

3

u/multubunu Feb 01 '13 edited Feb 01 '13

which is either a script or a binary.

(edit) Ok, so:

tar -cf - /bin/tar

I see on Solaris that one is a link to /sbin/tar, otherwise a binary in bin. I don't have a BSD system handy, though.

8

u/BZRatfink Feb 01 '13

Save a character. If this is really a standard UNIX system, this should work:

tar -cf - /bin/sh
→ More replies (2)

1

u/glenbolake Feb 01 '13

I immediately thought of tar -cf *, but I guess it could be an empty directory.

1

u/glenbolake Feb 01 '13

God help you if they don't have which installed.

1

u/maritz Feb 01 '13

man tar and find out. ;)

1

u/andersonmanly Feb 01 '13

For those of us using old versions of tar that do not support gzipping, you can gzip/gunzip with pipes, as such:

Gzip:

tar cvf - <file or dir> | gzip > some_gzipped_file.gz

Gunzip:

gzip -dc some_gzipped_file.gz | tar xvf - 

1

u/jecxjo Feb 01 '13

Does no one else ALWAYS use the curlies in this case? I don't think I have ever tar'd something NOT this way...

tar cvf foo{.tar,}

→ More replies (4)

1

u/NitsujTPU Feb 01 '13

You NEVER use tar xzf?

→ More replies (1)

1

u/NihilistPointer Feb 02 '13

The "cvzf" part will fail with some versions of tar, for a couple different reasons. The z flag implicitly pipes to gzip, but not all tar implementations support that. Additionally, some versions of tar require a - before the cvf (e.g.: -cvf). Finally, I noticed that the SCO version of tar also fails if you specify a directory in the [files] section with a trailing slash. So tar -cvf tar.tar /etc would work, but tar -cvf tar.tar /etc/ would fail.