What is the rationale for using -f in tar

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?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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).

Solution 2

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.

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

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply