1. 程式人生 > >Linux將輸出放到檔案中

Linux將輸出放到檔案中

 一,如何把命令執行的結果儲存到檔案當中?
  這個問題太簡單了,大家都知道,用 > 把輸出轉向就可以了
  例子:
  [[email protected] ~]$ ls > ls.txt
  [[email protected] ~]$ cat ls.txt
  1.gtkrc-2.0
  2009
  a
  amsn_received
  a.tar.gz
  說明: > 是把輸出轉向到指定的檔案,如檔案已存在的話也會重新寫入,檔案原內容不會保留
       >> 是把輸出附向到檔案的後面,檔案原內容會保留下來

二,如何能在輸出資訊的同時把資訊記錄到檔案中?

  我們在上面的例子中可以看到,我們使用輸出轉向,命令在終端上的輸出轉向到了檔案中,但如果我希望能同時在終端上看到輸出資訊怎麼辦?
  我們可以使用這個命令: tee
  解釋一下tee的作用:
  read from standard input and write to standard output and files
  它從標準輸入讀取內容並將其寫到標準輸出和檔案中
  看例子:
  [[email protected] ~]$ ls | tee ls_tee.txt
  1.gtkrc-2.0
  2009
  a
  amsn_received
  a.tar.gz
  [[email protected]
~]$ cat ls_tee.txt
  1.gtkrc-2.0
  2009
  a
  amsn_received
  a.tar.gz
備註:使用 tee時,如果想保留目標檔案原有的內容怎麼辦?
  可以使用 -a引數
  -a, --append
  append to the given FILEs, do not overwrite
  附加至給出的檔案,而不是覆蓋它

三,多個命令的輸出都需要記錄,可以用script
  script這個命令很強大,可以記錄終端的所有輸出到相應的檔案中
  看例子:
  [[email protected] ~]$ script
  Script. started, file is typescript
  [
[email protected]
~]$ ls
  1.gtkrc-2.0 c.tar kmess-2.0alpha2.tar.gz secpanel-0.5.3-1.noarch.rpm
  2009 DownZipAction.php kmesslog secpanel-0.5.4-2.noarch.rpm
  [[email protected] ~]$ exit
  exit
  Script. done, file is typescript
  [[email protected] ~]$ cat typescript
  Script. started on 2009年02月08日 星期日 18時56分52秒
  [[email protected] ~]$ ls
  1.gtkrc-2.0 c.tar kmess-2.0alpha2.tar.gz secpanel-0.5.3-1.noarch.rpm
  2009 DownZipAction.php kmesslog secpanel-0.5.4-2.noarch.rpm
  [[email protected] ~]$ exit
  exit
  Script. done on 2009年02月08日 星期日 18時57分00秒
  說明:
  1,我們在啟動script時沒有指定檔名,它會自動記錄到當前目錄下一個名為 typescript的檔案中。也可以用 -a引數 指定檔名
  例子:
  [[email protected] ~]$ script. -a example.txt
  Script. started, file is example.txt
  此時終端的輸出內容被記錄到 example.txt這個檔案中
  2,退出script時,用exit
  感到奇怪嗎?事實上script就是啟動了一個shell
  看一下ps auxfww 的資訊就知道了
  lhd 17738 0.1 3.2 152028 33328 ? Sl 18:30 0:03 /usr/bin/konsole
  lhd 17740 0.0 0.1 6372 1720 pts/1 Ss 18:30 0:00 \_ /bin/bash
  lhd 17900 0.0 0.0 5344 628 pts/1 S 19:01 0:00 | \_ script
  lhd 17901 0.0 0.0 5348 464 pts/1 S 19:01 0:00 | \_ script
  lhd 17902 0.5 0.1 6372 1688 pts/2 Ss 19:01 0:00 | \_ bash -i
  3,檢視typescript的內容,可以看到它同時記錄下了script的啟動和結束時間

  四,用script錄製並播放session的內容
  我們可以用 script把整個終端會話的所有操作和輸出錄製下來,然後再用scriptreplay進行播放。
  如果錄製時記錄下來了操作時的時間資料,那麼播放時和操作時的使用時間完全相同。
  這個很有用吧,比如:我們可以把安裝軟體時編譯的過程記錄下來,然後給別人進行演示
  看例子:
  [[email protected] ~]$ script. -t 2>example.time -a example.txt
  Script. started, file is example.txt
  [[email protected] ~]$ ls
  說明: -t 2>example.time -t是把時間資料輸出到標準錯誤(standard error),所以我們使用 2>example.time 把資料轉向到 example.time這個檔案當中
  如何播放所記錄的內容?
  第一步:安裝scriptreplay
  下載
   wget linux/utils/util-linux/util-linux-2.12r.tar.bz2">ftp://ftp.kernel.org/pub/linux/utils/util-linux/util-linux-2.12r.tar.bz2
  解壓
  tar -jxvf util-linux-2.12r.tar.bz2
  之後複製檔案到系統的命令目錄中即可
  [[email protected] 下載]# cp util-linux-2.12r/misc-utils/scriptreplay.pl /usr/bin/scriptreplay
  [[email protected] 下載]# chmod 755 /usr/bin/scriptreplay
  備註: fedora 10的util-linux-ng-2.14.1-3.2.fc10.i386.rpm 此包中已包含 scriptreplay,已無需另行安裝
  第二步:播放所錄製的session內容
  [[email protected] ~]$ scriptreplay example1.time example1.txt
  [[email protected] ~]$ ls
  1.gtkrc-2.0 c.tar [email protected] pass
  [[email protected] ~]$ abcd
  bash: abcd: command not found

  [[email protected] ~]$ exit

相關推薦

Linux輸出放到檔案

 一,如何把命令執行的結果儲存到檔案當中?   這個問題太簡單了,大家都知道,用 > 把輸出轉向就可以了   例子:   [[email protected] ~]$ ls > ls.txt   [[email protected] ~]$ cat ls.txt   1.gtk

linux重定向總結:如何shell命令的輸出資訊自動輸出檔案儲存

在做批量實驗室,例如跑批量MR的作業,我們會寫好shell指令碼,然後啟動指令碼,等所有作業執行完再去看結果,但是這些執行時的資訊如何儲存下來到檔案中呢?下面這個命令可以完成這個任務。 sh batchjob.sh 2>&1 | tee mylog.log

linux整理筆記系列】編譯的warning警告資訊輸出檔案

Linux中,指令碼語言環境中,即你用make xxx即其他一些普通linux命令,比如ls,find等,不同的數字,代表不同的含義:  數字 含義標準叫法 0標準輸入 stdin = standard input 1標準輸出 stdout = standard outpu

一些內容輸出檔案

看到一個面試題,如下: 第八題: 一個字串將其輸入到一個檔案中,程式碼如下: <?php $a = '[{"teamId": "43", "serial": "1"},{"teamId": "1", "serial": "2"},{"teamId": "14", "se

iOS Log日誌輸出檔案儲存

對於那些做後端開發的工程師來說,看LOG解Bug應該是理所當然的事,但我接觸到的移動應用開發的工程師裡面,很多人並沒有這個意識,查Bug時總是一遍一遍的試圖重現,試圖除錯,特別是對一些不太容易重現的Bug經常焦頭爛額。而且iOS的異常機制比較複雜,Objecti

ios NSLog日誌重定向輸出檔案儲存(3)

#pragma mark - app 日誌檔案記錄,用於測試; - (void)redirectNSLogToDocumentFolder { //如果已經連線Xcode除錯則不輸出到檔案 /* if(isatty(STDOUT_FILENO)) {

日誌的錯誤資訊輸出檔案

這是一個Task public static void main(String[] args) throws IOException { EntityLinkWordMegerBolt csb =

shell 講字串當命令輸出寫入檔案,並處檔案讀出來

#!/bin/sh -x str=cxx* key=keys #command=${key} ${str} #echo ${command} #將以cxx為字首的key 打印出來 並追加到檔案 keys_file redis-cli ${key} ${str} >

終端內容輸出檔案,又顯示在終端

查了查:發現都是用命令的,有用重定向的有用tee命令的:-------------------------------------------》》》》有時我們想要在螢幕上輸出資訊,又想同事保留下輸出的資訊到檔案中,方便分析的話,tee命令可以幫上大忙。 tee名稱:從標準輸入

COCO2017標註檔案的bbox顯示在圖片上

  from pycocotools.coco import COCO import cv2 import pandas as pd def showNimages(imageidFile, annFile, imageFile, resultFile): """

ubuntu下python跑任務輸出檔案遇到的一些問題(輸出重定向)

之前主要是參考https://www.cnblogs.com/chason95/articles/9760291.html 一般使用 python test.py > ./log.txt 或 python test.py | tee ./log.txt 然後就會快取很多輸出後才能在螢幕或log中查到

Django views.py檔案有的資料寫成全域性變數 這樣每個函式都可以用到

在Django中,有時幾個頁面中要顯示的資料一樣,或者說一個頁面中,點選連結只會重新整理其中一部分內容,其他的不變。 這樣,我們就可以將相同的、需要渲染在模板中的資料,寫成全域性的,這樣減少程式碼的重複書寫 例如: 當點選某一個月份文章歸檔時,只有文章那一塊會發生變化,其他都不變化

java輸入學生資訊,按總分成績由高到低排列,輸出檔案

<span style="font-family: Arial, Helvetica, sans-serif;">主函式</span>import java.io.BufferedOutputStream; import java.io.Buffer

Linux多個檔案生成.so檔案

本工程共有三個檔案:myhello.c、myhello.h、Makefile myhello.c程式碼如下: #include <stdio.h> #include <string.h> #include "myhello.h" voi

,編寫一個程式,a.txt檔案的單詞與b.txt檔案的單詞交替合併到c.txt檔案,a.txt檔案的單詞用回車符分隔,b.txt檔案中用回車或空格進行分隔.

在java面試寶典看到這樣一題,看到答案真蛋疼,看了半天才明白,可能每個人的想法不一樣!答案也不知道是那位前輩寫的,讓人理解起來太費精了!老饒彎子,以下是本人自已整理的,去面試如果真有這麼一題,應該不成問題了。。。。。。。如有不是的地方還望指點 package accp

Linux 下查詢 .so 檔案函式入口

有時候會想知道 .so 檔案中某些資訊,比如版本,函式入口等。strings 命令是個很有用的命令,比如我想知道 libstdc++.so 這個動態庫裡面是否有 refill 函式,可以用 $ strings libstdc++.so | grep ref

linux C 修改文字檔案指定行的內容

1、可隨意修改內容 /* * Author:aniu12<[email protected]> * Date: 2014.02.13 */ #include<stdio.h> #include<stdlib.h> #in

/proc/uptime檔案的數轉化成long然後轉成日期

思路: 1、格式化讀取檔案中兩個數,儲存在兩個字串中。 2、/proc/uptime中表示秒,捨棄小數。將整數部分轉為long。 3、由long轉日期。 #include <stdio

C小程式—一個磁碟檔案的資訊複製到另一個磁碟檔案

#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){FILE *fp1, *fp2;      //定義指向FILE型別檔案的指標變數char ch, fil

linux命令新增Path

將可執行程式新增到PATH中有三種方法:1、直接在命令列中設定PATH     # PATH=$PATH:/usr/local/redis/bin     使用這種方法,只對當前會話有效,也就是說每當登出或登出系統以後,PATH設定就會失效。 2、在profile中設定PAT