Is it possible to compress (create a compressed archive) data while reading from stdin on Linux?
Asked
Active
Viewed 1.3e+01k times
148
-
1I'm voting to close this question as off-topic because it belongs to Unix & Linux – Dan Dascalescu Jun 30 '19 at 10:53
3 Answers
218
Yes, use gzip for this. The best way is to read data as input and redirect the compressed to output file i.e.
cat test.csv | gzip > test.csv.gz
cat test.csv
will send the data as stdout and using pipe-sign gzip will read that data as stdin. Make sure to redirect the gzip output to some file as compressed data will not be written to the terminal.

Alex Riley
- 169,130
- 45
- 262
- 238

Space
- 7,049
- 6
- 49
- 68
-
5Note that the test.csv.gz files remains of 0 size until the data flux is closed. – MUY Belgium Apr 26 '13 at 10:00
-
-
13
-
3
-
1@MUY Belgium: not exactly, it starts writing only after 16KB of compressed blocks. See [here](https://unix.stackexchange.com/questions/643078/tool-library-to-compress-decompress-on-the-fly/643084#643084) – Francois Oct 05 '21 at 15:37
113
Yes, gzip
will let you do this. If you simply run gzip > foo.gz
, it will compress STDIN to the file foo.gz. You can also pipe data into it, like some_command | gzip > foo.gz
.

jtbandes
- 115,675
- 35
- 233
- 266