tee to a compressed file
Asked Answered
M

2

8

tee reads from standard input and writes to standard output and a file.

some_command |& tee log

Is that possible for tee to write to a compressed file?

some_command |& tee -some_option log.bz2

If tee can not do that, is there any other command?

I can redirect the output to a compressed file with

some_command |& bzip2 > log.bz2

But with this command, the output to standard output is missing.

Malva answered 28/3, 2013 at 2:44 Comment(0)
G
6

If you're OK having your output on stderr, you can redirect it:

some_command | tee /dev/stderr | bzip2 > log.bz2

This tees the output to both stdout and stderr (| tee /dev/stderr). Then it pipes the stdout to bzip2 (| bzip2 > log.bz2)

Gayle answered 28/3, 2013 at 2:50 Comment(1)
This will have the same problem as gzip: in the event of a system crash, you will have an empty output log file log.bz2Chiromancy
S
15

If your shell is bash (version 4.x), you have 'process substitution', and you could use:

some_command 2>&1 | tee >(bzip2 -c > log.bz2)

This redirects standard error and standard output to tee (like |& does, but I prefer the classic notation). The copy of tee's output is sent to a process instead of a file; the process is bzip2 -c > log.bz2 which writes its standard input in compressed format to its standard output. The other (uncompressed) copy of the output goes direct to standard output, of course.

Suspend answered 28/3, 2013 at 5:34 Comment(0)
G
6

If you're OK having your output on stderr, you can redirect it:

some_command | tee /dev/stderr | bzip2 > log.bz2

This tees the output to both stdout and stderr (| tee /dev/stderr). Then it pipes the stdout to bzip2 (| bzip2 > log.bz2)

Gayle answered 28/3, 2013 at 2:50 Comment(1)
This will have the same problem as gzip: in the event of a system crash, you will have an empty output log file log.bz2Chiromancy

© 2022 - 2024 — McMap. All rights reserved.