LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   tar options, hyphen? (https://www.linuxquestions.org/questions/linux-software-2/tar-options-hyphen-745335/)

heyduke25 08-05-2009 11:51 AM

tar options, hyphen?
 
yesterday i purchaced an 8gb thumb drive to back up my home directory. using examples from "man tar" i entered something like this:

tar -cfv <path>/home_bu.tar home/

i kept getting errors and a failed backup until i dropped the hyphen in front of operator and options:

tar cfv <path>/home_bu.tar home/

can someone clarify this? distro is fedora 11 and yes, there were problems with .gvfs that had to be corrected.

thanks-

larry

MensaWater 08-05-2009 12:00 PM

The f is in the wrong place. It specifies the file (or device file) to backup to (or restore from in t or x).

Your usage should be something like:

tar cvf <archivefile or devied> <backup item>

e.g.
tar cvf /dev/st0 /
That says to backup everything under the root directory (/) to the first tape drive (/dev/st0).

pwc101 08-05-2009 12:15 PM

tar has 3 types of syntax (according to http://www.gnu.org/software/tar/manual/tar.html#SEC32):
  • long options (--file)
  • short options (-f)
  • old options (f)
For the old option syntax, all the letters must follow "tar" and must all fall into a single clump with no spaces. The order of the letters doesn't really matter, so long as the arguments to those letters follow the same order after the clump of options. For example:
Code:

tar cvbf 20 /dev/rmt0
is synonymous with
Code:

tar cvfb /dev/rmt0 20
These two are both synonymous with
Code:

tar -c -v -b 20 -f /dev/rmt0
I find the last one probably the easiest to understand as the argument to each option is next to that option.

All these examples are taken from the link above.

heyduke25 08-05-2009 12:23 PM

jlightner-

thanks for the help. reading the man page again i find no mention of order required in the options, that is, with:

tar cfv foo.tar foo/

c is the operator for "create." f and v are options "file" and "verbose." i'll have to experiment to find if order is significant. i still don't understand why i had to drop the hyphen in front of the operator to make the backup work. here is a quote from the man page:

EXAMPLES
tar -xvf foo.tar
verbosely extract foo.tar

tar -xzf foo.tar.gz
extract gzipped foo.tar.gz

tar -cjf foo.tar.bz2 bar/
create bzipped tar archive of the directory bar called
foo.tar.bz2

they did not work.

tar cvf home.tar home/

worked fine.

thanks again

heyduke25 08-05-2009 12:34 PM

pwc101

thanks for the reply. i will read thru the link you provided. it has been a while since i used tar to create an archive and i was depending on the man page, my mistake. here again are the man page examples. they appear to be incorrect (or perhaps i am a poor reader.)

EXAMPLES
tar -xvf foo.tar
verbosely extract foo.tar

tar -xzf foo.tar.gz
extract gzipped foo.tar.gz

tar -cjf foo.tar.bz2 bar/
create bzipped tar archive of the directory bar called
foo.tar.bz2

try the third example and you will likely get a failed archive. hate to sound like an old fuddy duddy but i miss the ancient unix man pages.

many thanks for the help

pwc101 08-05-2009 02:09 PM

Quote:

Originally Posted by heyduke25 (Post 3632575)
pwc101

thanks for the reply. i will read thru the link you provided. it has been a while since i used tar to create an archive and i was depending on the man page, my mistake. here again are the man page examples. they appear to be incorrect (or perhaps i am a poor reader.)

EXAMPLES
tar -xvf foo.tar
verbosely extract foo.tar

tar -xzf foo.tar.gz
extract gzipped foo.tar.gz

tar -cjf foo.tar.bz2 bar/
create bzipped tar archive of the directory bar called
foo.tar.bz2

try the third example and you will likely get a failed archive. hate to sound like an old fuddy duddy but i miss the ancient unix man pages.

many thanks for the help

The third example works fine for me.

The important difference is when the options are preceded by a -, then any of those options which require arguments must have those arguments follow the option. However, if the options are not preceded by a -, then any arguments to options which require them must follow the cluster of options in the same order as the options which require them are specified. This is remarkably hard to explain!

For example, let's take three options and work with those.

f - this option requires an argument, in this case, foo.tar.
b - this option requires an argument to specify the block size, we'll use 20.
c - this option does not require an argument, it just tells tar we want to make a new archive.

Using the syntax which does not require a - means any options (c, b and f in this case) must follow tar, and must not have a space in them:
Code:

tar bfc 20 foo.tar /bar
Of the two options which require arguments, b comes first, so its argument must come first after the cluster of arguments. The f comes after the b, so its argument comes second. Since c has no arguments, we can put this anywhere in the combination:
Code:

tar cbf 20 foo.tar bar/
tar bcf 20 foo.tar bar/
tar bfc 20 foo.tar bar/

These are all synonymous.

When the options are preceded by a -, then the argument must follow the option:
Code:

tar -c -b 20 -f foo.tar bar/
Here, each option has its corresponding argument following it.

In your example, you had only one option which required an argument, in which case you can group the options which don't require an argument, and the one the does require an argument must be last in that group, and the argument has to follow it:
Code:

tar -cvf foo.tar bar/
If you put the arguments in a different order when using the - syntax, then whatever follows f will become the file name:
Code:

tar -cfv foo.tar bar/
would create an archive called v which contains foo.tar (if it existed) and the contents of /bar.

Hope this helps.

heyduke25 08-05-2009 03:03 PM

pcw101-

thanks for the tar 101. i finally figured out what i did wrong:

tar -cfv mydir.tar mydir/

was invalid because with the hyphen the v refered to the created tar file, clearly invalid.

tar cfv mydir.tar mydir/

without the hyphen, the option "f" refers to the created tar file which is correct and worked for me. that's a bit obtuse and by the next time i need to do it i will have forgotten. i need to write a script that will do it for me.

thank you for your patience


All times are GMT -5. The time now is 09:27 AM.