30

When using tar I always include -f in the parameters but I have no idea why.

I looked up the man and it said;

-f, --file [HOSTNAME:]F

use archive file or device F (default
"-", meaning stdin/stdout)

But to be honest I have no idea what that means. Can anyone shed any light on it?

Anthon
  • 79,293
Toby
  • 3,993

3 Answers3

29

The -f option tells tar that the next argument is the file name of the archive, or standard output if it is -.

Kusalananda
  • 333,661
ddeimeke
  • 4,597
  • Ah, cheers! I presumed that the file name would have to appear next anyway so this -f was doing something more special! – Toby Aug 26 '10 at 10:45
  • 7
    @Toby: I suspect this is for historical reasons. "tar" is short for "tape archive", and presumably the original versions didn't envision people using disk files for archives all that often. – David Thornley Aug 27 '10 at 21:10
9

Quite simple. If you omit the -f parameter, output is passed to stdout:

gammy@denice:/tmp/demo$ tar -c a b c
a0000644000175000017500000000000011435437117010223 0ustar  gammygammyb0000644000175000017500000000000011435437117010224 0ustar  gammygammyc0000644000175000017500000000000011435437117010225 0ustar  gammygammygammy@denice:/tmp/demo$ ls
a  b  c
gammy@denice:/tmp/demo$ 

...what a mess!

The -f-parameter (as you quoted) expects a filename (and optionally a hostname), hence the first argument after it is the output filename:

gammy@denice:/tmp/demo$ tar -cf output.tar a b c
gammy@denice:/tmp/demo$ ls
a  b  c  output.tar
gammy@denice:/tmp/demo$ 
gamen
  • 266
2

It lets you specify the file or device you're going to be working with. Either creating, updating or extracting things from it depending on other supplied flags. For example:

# Create a tar file with the contents of somepath/
tar -cvf filename.tar somepath/

# Extract the tar file.
tar -xvf filename.tar
signine
  • 131