teeコマンド使うと何が便利なの?

Linuxコマンドを操作する時に、自分がどのくらい便利にコマンドを使いこなしているか考えたてみました。
出来ることなら、Linux標準になっているコマンドをなんのカンペもなくスイスイ打ち込むのって、見ていて非常に格好良いじゃないですか。
プログラムエンジニアの人であれば、そういったタイピングなどに憧れるのは普通だと思いますが、僕もそこそこプログラマーとして仕事をしてきて年月が経つんですが、今更「tee」コマンドというモノを知りました。
いっちょ前だと思っていたのに、自分もまだまだだと思い知らされました。
teeコマンドってどういう機能?
調べればすぐに分かりますが、teeコマンドは、ファイルに出力するコマンドのようです。
1 2 3 |
$ echo "test" | tee hoge.txt $ cat hoge.txt test |
sampleを見るとわかると思いますが、パイプで繋がった処理をファイルに書き出す機能のようですね。
まだ、オプションなどを全て把握していないのですが、単体で使うことは無いコマンドなので、補佐的な機能のようです。
tee使わなくても同じ事できちゃわない?
普段コマンドを使っている人なら、上記のsampleが以下の操作と同じではないかと考えると思います。
1 2 3 4 5 6 7 8 9 |
$ echo "test" > hoge.txt $ cat hoge.txt test # 追記 $ echo "test2" >> hoge.txt $ cat hoge.txt test test2 |
追記もできて、こっちのほうが使いやすいと思うのだが、teeコマンドの優位性はどこにあるんだろう???
オプション機能を調べて、その辺を考えてみたいと思います。
オプション調査
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
$ tee --help Usage: tee [OPTION]... [FILE]... Copy standard input to each FILE, and also to standard output. -a, --append append to the given FILEs, do not overwrite -i, --ignore-interrupts ignore interrupt signals -p diagnose errors writing to non pipes --output-error[=MODE] set behavior on write error. See MODE below --help display this help and exit --version output version information and exit MODE determines behavior with write errors on the outputs: 'warn' diagnose errors writing to any output 'warn-nopipe' diagnose errors writing to any output not a pipe 'exit' exit on error writing to any output 'exit-nopipe' exit on error writing to any output not a pipe The default MODE for the -p option is 'warn-nopipe'. The default operation when --output-error is not specified, is to exit immediately on error writing to a pipe, and diagnose errors writing to non pipe outputs. GNU coreutils online help: <http: //www.gnu.org/software/coreutils></http:> Report tee translation bugs to <http: //translationproject.org/team></http:> Full documentation at: <http: //www.gnu.org/software/coreutils/tee> or available locally via: info '(coreutils) tee invocation' </http:> |
“man tee”で調べてもマニュアルが無かったので、–helpで調べてみると上記のようなテキストが参照できました。
-a 追記
1 2 3 4 5 |
$ echo "test3" >> hoge.txt $ cat hoge.txt test test2 test3 |
どうやら追記ぐらいしか使えそうなオプションがないですね。
標準出力に使える
どうやら、以下のような標準出力に対しても、ファイルに書き出すことができるというのが、teeコマンドの最大の利点のようです。
1 |
$ コマンド 2>&1 | tee 出力ファイル名 |
このシチュエーション、awkを使っている僕としては、あまりないので、teeコマンド、今後使うことあるかな?
もっと便利な使い方があるのを知っている人は、是非教えてください。