Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tee to a compressed file

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.

like image 394
Yorkwar Avatar asked Mar 28 '13 02:03

Yorkwar


People also ask

What does the tee command do?

The tee command, used with a pipe, reads standard input, then writes the output of a program to standard output and simultaneously copies it into the specified file or files. Use the tee command to view your output immediately and at the same time, store it for future use.

How do I uncompress and compress files in Linux?

Both Linux and UNIX include various commands for Compressing and decompresses (read as expand compressed file). To compress files you can use gzip, bzip2 and zip commands. To expand compressed file (decompresses) you can use and gzip -d, bunzip2 (bzip2 -d), unzip commands.

How do I gzip in stdout?

The gzip program compresses and decompresses files on Unix like system. You need to pass the -c or --stdout , or --to-stdout option to the gzip command. This option specifies that output will go to the standard output stream, leaving original files intact.

What does tee mean in Linux?

What Does tee Command Do in Linux? The tee command reads standard input (stdin) and writes it to both standard output (stdout) and one or more files. tee is usually part of a pipeline, and any number of commands can precede or follow it.


1 Answers

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.

like image 91
Jonathan Leffler Avatar answered Sep 25 '22 15:09

Jonathan Leffler