Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Don't laugh, but could someone explain ...

Status
Not open for further replies.

Martin999

Technical User
Feb 26, 2004
298
US
Hi guys,

Having a brain dead moment, could some explain the following for me please.

I use the following quite often ...

tar cf - . | (cd todir; tar xfp -)

.. and suddenly realised that whilst I understand the first bit (tar cf - .) as "set the tar file" to standard out, I can't quite get my head round the "meaning" of the - in the seconf path, yes, it cds to "todir" and untars the standard out (which seems to now be a tar file".

Is it really that simple ??

Also, (yes I know "RTFM", but I cannot find anywhere a man page (or web page) that explains - as being stand out, is it only a tar thing.

Many thanks,

Martin
 
In the second bit '-' is now stdin, and stdin, on that side of the pipe, is the same as stdout on the other side.

It looks to me that this is the same as 'cp -rp . todir' but I'm not clever enough to speculate on how it would handle links.

Ceci n'est pas une signature
Columb Healy
 
Also, since tar writes to standard output by default and reads from standard input by default, your command is equivalent to:
Code:
tar c . | (cd todir; tar xp)

Martin999 said:
Also, (yes I know "RTFM", but I cannot find anywhere a man page (or web page) that explains - as being stand out, is it only a tar thing.
It's not just a tar thing. It is understood by many (most?) Unix utilities. And as pointed out, it stands for standard input where appropriate.


columb said:
It looks to me that this is the same as 'cp -rp . todir' but I'm not clever enough to speculate on how it would handle links.
It might just be a cp when both tars happen on the same machine, but consider:
Code:
tar c . |ssh somewhere "cd todir; tar xp"
 
This is how you copy an entire directory tree to another directory, including ownership, permissions, links, etc. This is actually used as an example in the [tt]man[/tt] page for [tt]tar[/tt]. It shows the command as...
Code:
cd fromdir; tar cf - . | (cd todir; tar xfBp -)
The "-" in the first [tt]tar[/tt] means send the output (tar file) to standard out. That gets piped to the other [tt]tar[/tt], that now has [tt]todir[/tt] as it's working directory, where it untars everything coming in through the pipe because of the "-".

This is actually a little dangerous if you happen to typo the [tt]todir[/tt]. That's why I usually do it as...
Code:
cd fromdir; tar cf - . | (cd todir [b]&&[/b] tar xfBp -)
That way it only does the untar if the [tt]cd[/tt] worked.
 
Actually, [tt]tar[/tt] does not send to and read from stdin/stdout by default. By default it will try to use a tape drive. See the following from the [tt]man[/tt] page...

man tar said:
f File. Use the tarfile argument as the name of the
tarfile. If f is specified, /etc/default/tar is not
searched. If f is omitted, tar will use the device
indicated by the TAPE environment variable, if set;
otherwise, it will use the default values defined in
/etc/default/tar. If the name of the tarfile is '-',
tar writes to the standard output or reads from the
standard input, whichever is appropriate. tar can be
used as the head or tail of a pipeline. tar can also
be used to move hierarchies with the command:

example% cd fromdir; tar cf - .| (cd todir; tar xfBp -)
 
Mine's different :p
man tar said:
-f, --file [HOSTNAME:]F
use archive file or device F (default "-", meaning stdin/stdout)

(GNU tar on Debian testing).
 
Mine was Solaris. Just goes to show ya, RTFM (Read The Fine Manuals)! [bigsmile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top