Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 10 down vote accepted

"-f" tells tar that the next parameter is the file name of the archive or STDOUT if it is "-".

share|improve this answer
    
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
4  
@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
    
@David - Very interesting, cheers! –  Toby Aug 28 '10 at 18:17

Quite simple. If you oppress 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$ 
share|improve this answer
3  
> If you oppress the -f parameter, output is passed to stdout. This is true with GNU tar, but I remember that on Solaris the default was a tape device (/dev/rmt0 or something like this). –  Cristian Ciupitu Aug 27 '10 at 18:42

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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