Is there a tool that compresses STDIN, and outputs to STDOUT? This tool (or its counterpart) should be able to decompress as well. Something simple is fine, but it must be lossless.


3 Answers 3



Most utilities support outputting to STDOUT. Take for example gzip:

$ echo "asdgasdfasdfasdfasdfasdfasdf" | gzip | xxd
00000000: 1f8b 0800 219b dd57 0003 4b2c 4e49 4f2c  ....!..W..K,NIO,
00000010: 4e49 c386 b900 45ce f97c 1d00 0000       NI....E..|....

I've used xxd as some unprintable characters exist. Run it through gunzip to decompress


xz works pretty similarially:

$ echo "asdfasdfasdf" | xz | xxd
00000000: fd37 7a58 5a00 0004 e6d6 b446 0200 2101  .7zXZ......F..!.
00000010: 1600 0000 742f e5a3 e000 0c00 0b5d 0030  ....t/.......].0
00000020: 9cc8 abf9 a8be f900 0000 0000 9525 d79a  .............%..
00000030: 089a c592 0001 270d f37b f284 1fb6 f37d  ......'..{.....}
00000040: 0100 0000 0004 595a                      ......YZ

and for decompress:

$ echo "asdfasdfasdf" | xz | xz -d

gzip does this. Quoting from the manual page:

Gzip reduces the size of the named files using Lempel-Ziv coding (LZ77). Whenever possible, each file is replaced by one with the extension .gz, while keeping the same ownership modes, access and modification times. (The default extension is -gz for VMS, z for MSDOS, OS/2 FAT, Windows NT FAT and Atari.) If no files are specified, or if a file name is "-", the standard input is compressed to the standard output. Gzip will only attempt to compress regular files. In particular, it will ignore symbolic links.

There are of course others. It is common for Unix/Unix-like systems to filter the standard input to the output. The term "filter" covers a lot of possibilities; compression and decompression are only a few of those.


Yes, there are a lot of, e.g. gzip, bzip2, xz,...

cat testfile|gzip

would output the compressed stdin stream to stdout.

cat file.gz|gzip -d 

would decompress the compressed stream at stdin and output the result of decompression to stdout

The same applies to bzip2 and xz and many other compression utilities, though the options may vary. Check the appropriate man pages

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .