7

From man tar:

-f, --file ARCHIVE
use archive file or device ARCHIVE

Please consider:

tar -zxvf myFile.tar.gz

As far as I understand, z means "gzipped tarball", x means "extract", v means "verbose output" but about f I am not sure.

If we already give the file name myFile.tar.gz, why is the f argument needed?

2 Answers2

5

It's the option you use to specify the actual pathname of the archive you would want to work with, either for extracting from or for creating or appending to, etc. If you don't use -f archivename, different implementations of tar will behave differently (some may try to use a default device under /dev, the standard input or output stream, or the file/device specified by an environment variable).

In the command line that you quote,

tar -zxvf myFile.tar.gz

which is the same as

tar -z -x -v -f myFile.tar.gz

you use this option with myFile.tar.gz as the option-argument to specify that you'd like to extract from a particular file in the current directory.

Consult the manual for tar on your system to see what data stream or device the utility would use if you don't use the -f option.

The GNU tar implementation, for example, has a --show-defaults option that will show the default options used by tar, and this will probably include the -f option (this default may be overridden by setting the TAPE environment variable).

Kusalananda
  • 333,661
  • It's the option you use to specify the actual pathname of the archive you would want to work with, either for extracting from or for creating or appending to, etc. what is miss is - if the file path was already given, why does -f still needed? –  Jul 15 '19 at 03:20
  • @JohnDoea The archive name is optional and the -f option is therefore needed. Various implementations of tar uses different defaults for the default archive. On macOS, standard input/output is used, on OpenBSD, the /dev/rst0 SCSI tape device is used, and GNU tar seems to behave differently on different Unixes. This is why I wrote that last short paragraph (now last two paragraphs). – Kusalananda Jul 15 '19 at 06:46
  • @JohnDoea See updates to answer. – Kusalananda Jul 15 '19 at 06:57
4

So there are many other situations where you need the f option.

-f Archive

      Uses the Archive variable as the archive to be read or written. When this flag is not specified, the tar command uses a system-dependent default file name of the form /dev/rmt0. If the Archive variable specified is - (minus sign), the tar command writes to standard output or reads from standard input. If you write to standard output, the -c flag must be used.

From the tar man page.

Examples: To extract all files in the /tmp directory from the archive file on the /dev/rmt2 tape device and use the time of extraction as the modification time, enter:

tar -xm -f/dev/rmt2 /tmp

To create a new archive file that contains the file1 file and pass the archive file to the dd command to be written to the /dev/rmt1 device, enter:

tar -cvf - file1 | dd of=/dev/rmt1 conv=sync

To archive all directories and complete filenames listed in input list file infile into ar.tar, enter :

tar cvfL ar.tar infile

Where infile contains the pathnames of files that are to be archived. To archive files within directories listed in the input list file infile into ar.tar, enter:

tar cvRfL ar.tar infile

asktyagi
  • 675
  • the default for GNU tar on most Linux distros is to read the archive from stdin, not from /dev/rmt0. tar --show-defaults shows that: --format=gnu -f- -b20 .... BSD tar will use /dev/st0 by default on linux. –  Jul 10 '19 at 06:17
  • 1
    @mosvy there are several ways to override that default, and even between GNU and BSD the difference means you shouldn't write code that assumes a particular setting. Try export TAPE=/dev/null sometime. – Chris Davies Jul 10 '19 at 06:57
  • Surely there are ways to override it. The most usual is the -f switch ;-) –  Jul 10 '19 at 06:58
  • 1
    Note the very different semantics of the "bundled options" compared to the ordinary (dashed) options: If using "bundled options", as in tar cvRfL infile, the pathname argument to f is tho next word on the command line, not the L. Had you used tar -cvRfL infile, then the argument to -f would have been L, not the infile word on the command line. – Kusalananda Jul 10 '19 at 07:16