Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

can someone tell me if is possible to directly compress stdout?

I would like to do this in one step without creating temporary file files.txt...

Example:

cat file* > files.txt
tar -czf files.tar files.txt
rm files.txt

Is this possible?

share|improve this question
    
Just to confirm - you're trying to create a tarball containing a single file, right? –  Jeremy Kerr Nov 8 '12 at 8:49
    
This is correct. I just need to tar "files.txt" and noting else. –  Wolfy Nov 8 '12 at 8:52
    
Not trying to be critical, just wondering about your methodology here: Is there a reason you're trying to create a tar file containing just the one file? –  Jeremy Kerr Nov 8 '12 at 9:53
    
I do this because this files.txt is huge (100 MB+) and I need to transfer it from server A to server B. (compressed file is 90% smaller) –  Wolfy Nov 8 '12 at 10:29
    
Note that just tar-ing the file does not provide compression. It's just a method of packaging many files up into one file. –  Jeremy Kerr Nov 8 '12 at 15:24

1 Answer 1

up vote 4 down vote accepted

You can bypass the tar part of .tar.gz in this case, since you don't need to handle multiple files, and skip straight to gzip for compression.

To compress

gzip < file > file.gz

To decompress

gzip -d file.gz

or equivalently

gunzip file.gz

You can also use the bzip2 and xz compression utilities with similar syntax. xz usually produces smaller compressed files than either gzip or bzip2.

(Footnote)

If you are creating a compressed file and then using scp to transfer it, you can use built-in compression:

cat file | ssh -C user@remote 'cat >> remote_file'

or

scp -C file user@remote:/path/on/remote

or

rsync -avz file user@remote:/path/on/remote    
share|improve this answer
    
I need to create a .gz file from stdout, I would like to skip my cat and rm step (see example)... can this be done? –  Wolfy Nov 8 '12 at 16:00
    
Wolfy: when using the commands from this answer, you don't need the cat or rm steps. It's a single command to compress the file. –  Jeremy Kerr Nov 9 '12 at 0:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.