What does the -f parameter do in the tar command

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8fe8eee2e2f6cfebeae1e6ecea">[email protected]</a>:/tmp/demo$ tar -c a b c
a0000644000175000017500000000000011435437117010223 0ustar  gammygammyb0000644000175000017500000000000011435437117010224 0ustar  gammygammyc0000644000175000017500000000000011435437117010225 0ustar  <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="99fef8f4f4e0fef8f4f4e0fef8f4f4e0d9fdfcf7f0fafc">[email protected]</a>:/tmp/demo$ ls
a  b  c
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b1d6d0dcdcc8f1d5d4dfd8d2d4">[email protected]</a>:/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:

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e696f6363774e6a6b60676d6b">[email protected]</a>:/tmp/demo$ tar -cf output.tar a b c
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c2a5a3afafbb82a6a7acaba1a7">[email protected]</a>:/tmp/demo$ ls
a  b  c  output.tar
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f99e98949480b99d9c97909a9c">[email protected]</a>:/tmp/demo$

Method 3

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


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

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x