出力リダイレクト
linux特有ですよね
[考え方]
標準出力って考え方が存在します
たとえばlsコマンドを普通に実行すると
# ls -l
drwxr-xr-x 2 root root 4096 1月 11 08:53 Desktop
rw------- 1 root root 1199 11月 21 19:19 anaconda-ks.cfg
rw-r--r-- 1 root root 37121 11月 21 19:18 install.log
rw-r--r-- 1 root root 3865 11月 21 19:06 install.log.syslog
と画面に出力されます
そう、デフォルトの標準出力は「画面」なのです
これをファイルに出力したり、プリンタに出力したりすることも(当然ですが)出来ます
これを出力リダイレクトというらしいです
同じコマンドで出力先をファイルにしてみます
# ls -l > testfile
# cat testfile
drwxr-xr-x 2 root root 4096 1月 11 08:53 Desktop
rw------- 1 root root 1199 11月 21 19:19 anaconda-ks.cfg
rw-r--r-- 1 root root 37121 11月 21 19:18 install.log
rw-r--r-- 1 root root 3865 11月 21 19:06 install.log.syslog
[記号]
記号 | 使い方 | 出力先 |
---|---|---|
> | >[ファイル名] | 結果をファイルに出力 |
>> | >>[ファイル名] | 結果をファイルに追記 |
2> | 2>[ファイル名] | 発生したエラーをファイルに出力 |
2>> | 2>>[ファイル名] | 発生したエラーをファイルに追記 |
> 2>&1 | >[ファイル名]2>&1 | 結果とエラーをファイルファイルに出力 |
>> 2>>&1 | >[ファイル名]2>&1 | 結果とエラーをファイルに追記 |
[使用例]
# ls damiefile
ls: damiefile: そのようなファイルやディレクトリはありません damiefileというファイルはない
# ls damiefile 2> err-txt エラーが出たときはerr-txtにその内容を出力する
# cat err-txt
ls: damiefile: そのようなファイルやディレクトリはありません エラーがerr-txtに出力されている
タグ
このページへのコメント
d9y1tJ Thanks again for the blog post.Much thanks again. Keep writing.
Windowsでも同じようにできます。
Linux特有というわけではないです。