In the following command for example, tar -xzv works but tar -xzvf fails:

wget ${target_url} -O - | tar -xzv --strip-components 1 -C ${web_application_root}/${domain}

I understand I download a data stream to STDOUT with wget and pipe it as STDIN to tar to create an (overriden) directory file with that data.
I know tar -xzvf fails because a data stream which wasn't stored to a file, isn't a file for the shell; so telling tar -f "I work with a file" destroys the operation.

I had a case of not noticing -f and a case I forgot I should remove it when I pipe; these cases happened likely because I almost always do use -f with tar.
Is there a version of tar, or a tweak to make tar, ignoring -f in case data stream wasn't stored to a file?

1. What is the rationale for using -f in tar
2. tar without -f option in extraction
3. Piping of stream from wget to tar, and extracting it to a specific location failed

  • 1
    1, what does "consolidated to a file" mean? 2. No, magic Do-What-I-Mean (DWIM) technology does not exist...you're just going to have to learn to use the right commands and command-line options for the particular task at hand. It's not as hard as you think, it just takes a little practice. – cas Aug 20 '19 at 11:55
  • 2
    I would hate tar that behaves this way. -f requires an option-argument, this is simple and clear. "Errors" because of my mistakes don't really annoy me. Actual unexpected behavior because some tool tried to be "smart", applied its "fuzzy logic" and backfired – this would be annoying. – Kamil Maciorowski Aug 20 '19 at 12:08
  • @cas it is my bad phrasing for "stored in a file". – user149572 Aug 20 '19 at 19:11
  • @KamilMaciorowski I guess if and when it will be only helpful is subjective but in general I agree with you. – user149572 Aug 20 '19 at 19:12

-f doesn’t change tar’s behaviour with regards to streams or files or anything like that; it specifies the name of the archive to use.

I’m not aware of any variant of tar which can handle the absence of an argument to the -f option, nor am I aware of any reliable way to do so. As cas says, you need to remember that -f always takes an argument. One way to make it less error-prone is to always separate it out; it’s easier to miss an f option in a sequence such as -xzvf than with a separate -f.

It’s better still to always use -f, with - if necessary; that avoids depending on the behaviour of tar with no -f option (which varies depending on compile-time options and the TAPE variable). One way to solve the problem is to always start with -f - after the actions and change - if appropriate.

  • Looks like you did not use modern tar implementations. star reads/writes from/to stdin since 1982 and I have seen gtar binaries that have been configured to do the same. – schily Nov 21 '19 at 12:03
  • Looks like you did not read the question or answer. Here’s a hint: star: Missing arg for '-f' option. – Stephen Kitt Nov 21 '19 at 12:51
  • OK, but the OP wanted to use tar without -f and I did read it the way that he was interested in using tar without -f. – schily Nov 21 '19 at 13:11
  • Not quite: the OP wanted to use tar with -f but without having to specify a file (“I had a case of not noticing -f and a case I forgot I should remove it when I pipe.”) This question arose after the OP missed a - at the end of a command he copied and pasted, from another Q&A (the mistake was discussed in now-deleted comments). Your point makes sense in light of my recommendation in the last paragraph of my answer, but only in a world where everyone uses a tar which defaults to stdin/stdout instead of TAPE or whatever... – Stephen Kitt Nov 21 '19 at 13:17

Your Answer

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