1. 程式人生 > >【轉】 Linux中記錄終端輸出到txt文字檔案

【轉】 Linux中記錄終端輸出到txt文字檔案

轉載: http://blog.csdn.net/tengh/article/details/41823883

 

一,把命令執行的結果儲存到檔案當中:用 > 把輸出轉向就可以了
  例子:
  $ ls > ls.txt    #或者 ls-->ls.txt    #把ls命令的執行結果儲存到檔案ls.txt中
  
  說明: > 是把輸出轉向到指定的檔案,如檔案已存在的話也會重新寫入,檔案原內容不會保留
       >> 是把輸出附向到檔案的後面,檔案原內容會保留下來

二,在輸出資訊的同時把資訊記錄到檔案中: tee 命令  
  解釋一下tee的作用:
   “read from standard input and write to standard output and files”,它從標準輸入讀取內容並將其寫到標準輸出和檔案中

        引數:  -a, --append,“append to the given FILEs, do not overwrite“,附加至給出的檔案,而不是覆蓋它

  例子:
  $ ls | tee ls.txt   #將會在終端上顯示ls命令的執行結果,並把執行結果輸出到ls.txt 檔案中   
  $ls | tee -a ls.txt   #保留ls.txt檔案中原來的內容,並把ls命令的執行結果新增到ls.txt檔案的後面。

三,多個命令的輸出都需要記錄: script 命令
  script這個命令很強大,可以記錄終端的所有輸出到相應的檔案中
  例子:
  1.$ script
  Script. started, file is typescript
  2.$ ls
  …… 內容省略
  3.$ exit
  exit
  Script. done, file is typescript
  4. $cat typescript   #就會把上面綠色的部分再顯示一次:  

  說明:
  1,我們在啟動script時沒有指定檔名,它會自動記錄到當前目錄下一個名為 typescript的檔案中。也可以用 -a引數 指定檔名
  例子:
  $script. -a example.txt  #終端的輸出內容被記錄到 example.txt這個檔案中
  2,退出script時,用exit,事實上script就是啟動了一個shell