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

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??

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

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

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

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