23

When try to install dropbox from command line, I read the commands

cd ~ && wget -O - "https://www.dropbox.com/download?plat=lnx.x86_64" | tar xzf -

what does - mean here? is it previous directory?

4
  • 9
    A bit of pedantry: the option you're asking about is f -. This might make finding the answer in the man pages a little easier. – studog Jun 18 '19 at 19:01
  • 4
    It would be really helpful if you could explain what, precisely is unclear to you about the documentation of wget and tar. That way, the respective developers can improve their documentation so that future users don't stumble over the same problems again. In other words: be a hero and make the world a better place! – Jörg W Mittag Jun 18 '19 at 20:07
  • 2
    It would be a nice default for tar to use stdin, but since it’s heritage is to work with tapes it did not do that. Instead it learned how to work with files (with the f option) and then follows the convention to have a - in place of the filename for stdin (In case of eXtract or stdout in case of Create) – eckes Jun 19 '19 at 1:44
  • 1
    @eckes In the case of GNU tar, stdin is the default unless $TAPE is set. – chrylis -cautiouslyoptimistic- Jun 20 '19 at 6:17
42

Some commands accept - in place of a filename, either:

  • To write to standard output instead of to a named file. This is what the - argument passed to wget after -O is doing.
  • To read from standard input instead of from a named file. This is what the - argument passed to tar after xzf.

The command you showed downloads an archive file with wget and unpacks it with tar. To achieve this, the output of wget is piped (|) to the input of tar. This is why wget writes to standard output instead of a file and tar reads from standard input instead of a file.

8
12

That's just a filename that a lot of Unix programs interpret as "instead of actually opening a file, read from stdin (or write to stdout)."

That means reading from the input that gets streamed into the program; in your case, that's the output of wget.

10
  • 6
    Is it really a filename, though? – Eric Duminil Jun 18 '19 at 17:01
  • 7
    @EricDuminil It's a filename in the sense that programs accept it in some places where they would otherwise only accept filenames, but you're right that it's not really about an actual name of a file in some directory. – JoL Jun 18 '19 at 17:55
  • 5
    @EricDuminil: In particular, for programs that follow this convention, you have to use ./- if you actually want to open a file named -. – Jörg W Mittag Jun 18 '19 at 20:05
  • 4
    @EricDuminil In fact: yes! It names a file; in the unix sense, stdin is a file descriptor, and - is the name you supply to use that. It's not a file name from the OS's perspective, though. Only from the user perspective. – Marcus Müller Jun 19 '19 at 6:15
  • 3
    @rexkogitans no. /dev/stdin is just a convenience file name offered by some kernel facility. There's usually no fopen("/dev/stdin", "r") happening anywhere when you write a program that chooses to use stdin; you just go ahead and use the extern FILE* stdin that your libc header provided you with; by the way, that is the integer file descriptor 0. – Marcus Müller Jun 19 '19 at 6:46
5

The - argument to tar specifies that the archive should be read from stdin instead of a file. From the GNU tar manual:

If you use - as an archive-name, tar reads the archive from standard input (when listing or extracting files)

Other commands have the same behavior, and it is specified by the POSIX.1-2017 standard:

Guideline 13:

For utilities that use operands to represent files to be opened for either reading or writing, the '-' operand should be used to mean only standard input (or standard output when it is clear from context that an output file is being specified) or a file named -.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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