1. 程式人生 > >【linux】將ubuntu終端輸出為檔案

【linux】將ubuntu終端輸出為檔案

方案一: tee

tee --help

用法:tee [選項]… [檔案]…
將標準輸入複製到每個指定檔案,並顯示到標準輸出。

-a, --append 內容追加到給定的檔案而非覆蓋
-i, --ignore-interrupts 忽略中斷訊號
-p diagnose errors writing to non pipes
–output-error[=MODE] set behavior on write error. See MODE below
–help 顯示此幫助資訊並退出
–version 顯示版本資訊並退出

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.

echo $PATH | tee hello.log
echo $PATH | tee -a hello.log
echo $PATH | tee -a hello.log hello.txt

方案二:script

script --help

Usage:
script [options] [file]

Make a typescript of a terminal session.

選項:
-a, --append append the output
-c, --command run command rather than interactive shell
-e, --return return exit code of the child process
-f, --flush run flush after each write
–force use output file even when it is a link
-q, --quiet be quiet
-t, --timing[=] output timing data to stderr (or to FILE)
-V, --version output version information and exit
-h, --help display this help and exit

輸入指令開始錄製指令碼:

script -a hello.log

之後所有的終端輸出都會寫入hello.log的末尾
輸入exit命令或ctrl+D終止錄製。