Find the answer to your Linux question:
Results 1 to 4 of 4
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2010
    Staten Island, NY

    Pipelining with tar

    I am currently interning at a place and my job is to essentially learn UNIX. My supervisor gives me problems here and there to help guide me with my learning but for the most part I'm doing this all by self-teaching myself. Needless to say I have run into a few obstacles...for instance

    -Create a *one* line command that, using tar, will collect the full /usr/local directory (you need to run this as root again) and copy the whole /usr/local structure under /opt
    For example /usr/local/bin/hello will become /opt/local/bin/hello, etc.

    I want this as follows:
    1. /usr/local is collected by tar, but the output of this tar command is its stdout.
    2. what you get from the previous stdout, you compress with gzip and send it to stdout again
    3. get this output and decompress with gzip.
    4. get this output and pipe to tar in a way that will extract the tree under /opt.

    If anyone knows how I could go about doing this, please let me know, or at the very least point me in the right direction. What I've got so far (which could be completely wrong) is:

    tar cvf - usr/local/ | gzip -c - | gunzip -c - | tar xvf -

    in theory I feel like this should work (except for extracting the tree under /opt...i'm kinda stuck there) So if anyone knows or has a solution it would be greatly appreciated! and maybe help me out by explaining what is involve and maybe where I went wrong.


  2. #2
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Harrow, UK
    You've got the basic idea, though I don't think you need the zip-unzip section in the middle. Just tar and untar. If you look up the man page for tar you'll see an option to change directories so that the untar operation gets done in /opt.. But I'm not going to tell you what it is.

    We have a house rule on this forum that we don't do people's homework for them because that would be pointless, but most of us are glad to give a helping hand to someone who's obviously tried.
    "I'm just a little old lady; don't try to dazzle me with jargon!"

  3. #3
    Linux Engineer
    Join Date
    Jan 2005
    Saint Paul, MN
    Close but what you will get is "/opt/usr/local/bin/hello" rather than "/opt/local/bin/hello".

    Look at the use of "(" and ")" and compound statements. or the use of ssh rather than the '()".

  4. $spacer_open
  5. #4
    Just Joined!
    Join Date
    Oct 2010
    Staten Island, NY
    Thanks for the help...and yeah I understand the whole not doing other people's homework for them It definitely helps me to learn and figure things out myself...I just sometimes need a push in the right direction

    I think I managed to figure it out...

    tar cvf - usr/local/ | gzip -c - | gunzip -c - | tar -C opt/ -xvf -

    and yeah, the compress just to uncompress right away definitely seems unnecessary but my supervisor said we will be expanding on that later so hopefully it'll all make sense, I think he just wanted me to get the principles down. I have a feeling it has to do with compressing the file for transfer through ssh and uncompressing once its on the other side...

    Thanks again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts