#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Posts
    62
    Rep Power
    14

    Howto pipe: cp | tar | gzip


    Hello!
    Can anyone tell me if it's possible to pipe | this without having to create a physical file anywhere between A and B.tar.gz ??

    This is what I'm trying to do...

    1. File A
    2. Rename A to B
    3. Tar B.tar
    4. gzip -9 B.tar.gz

    So for example...

    cp A B | tar cvf - B | gzip -9 B.tar.gz

    ??

    Thanks!

    regan
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,481
    Rep Power
    1752
    Not with a pipe, main issue with that is that cp had no 'output' beyond the target file, however ...

    Code:
    cp A B && tar cvf - B | gzip -9 B.tar.gz
    may well work (not tested!). You did say rename, so did you want/need the cp to be a mv?
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Posts
    62
    Rep Power
    14
    Originally Posted by SimonJM
    Not with a pipe, main issue with that is that cp had no 'output' beyond the target file, however ...

    Code:
    cp A B && tar cvf - B | gzip -9 B.tar.gz
    may well work (not tested!). You did say rename, so did you want/need the cp to be a mv?
    Hi Simon,


    No the cp was just an example of trying to get a file piped into cat but changing the name in the process. I haven't tested your example yet, but would it make a physical copy with the cp A B command or does && pipe the output instead??

    regan
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,295
    Rep Power
    9400
    So you want to take an existing file and stuff it into a .tar.gz under a different name?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,481
    Rep Power
    1752
    The cp will copy the file as the man page will state. The && then runs the following command if the exit code of the previous command (the cp in this example) was 0.

    As requnix says, tell what you are actually after and we may be able to offer better help.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Posts
    62
    Rep Power
    14
    Originally Posted by requinix
    So you want to take an existing file and stuff it into a .tar.gz under a different name?

    Yes. That's exactly what I want to do.

    (Q. Do you know if tar'ing a single file before gzip actually offers any benefit?).
    Last edited by regan; August 6th, 2011 at 03:22 AM.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,481
    Rep Power
    1752
    If you want the file within the archive to have a different name (i.e., it will have the new name when extracted) you are going to have to do something with the file first - either a cp or (and I am not sure if this would work, you could try a ln to make a link which will only copy the inode data, not the actual file).

    I am not sure there is much difference in which order you do things, tar then gzip or gzip then tar. The only thing is that it is USUAL to gzip then tar, so it will be simpler for people to extract the file if you follow convention. I do recall reading something a fair while back about the relative efficiencies involved when dealing with a number of files with compressed archives, but as I cannot recall what it was and you seem to only be dealing with a single file you should be ok.

    Just checked and that command I suggested is ... well, broken!

    The tar is reading from stdin, which means, of course, that it will fail as it is no longer reading from the pipe ... Plus, of course, you can get tar to the the compressing for you:

    Code:
    cp A B && tar cvzf B.tar.gz B
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481

    tar v flag obsolete


    I recommend not using tar verification, unless perhaps you're actually using a tape drive. I mean, how often to you copy a file and then verify the copy? uh...never.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,481
    Rep Power
    1752
    Apart from the v meaning verbose?
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  18. #10
  19. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481

    whoops.


    Originally Posted by SimonJM
    Apart from the v meaning verbose?
    uh...oops.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,481
    Rep Power
    1752
    But you are correct - in live usage you'd probably wish to remove the v option - particularly since in this case only one file is being tar-ed. Generally what I'd do is redirect stdout and stderr into a separate file and, if need be, look at that if anything started going funny. All depends on how critical it is that you know what happened.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo