Linux中記錄終端(Terminal)輸出到文本文件
一,如何把命令運行的結果保存到文件當中?
這個(gè)問(wèn)題太簡(jiǎn)單了,大家都知道,用 > 把輸出轉向就可以了
例子:
[lhd@hongdi ~]$ ls > ls.txt
[lhd@hongdi ~]$ cat ls.txt
1.gtkrc-2.0
2009
a
amsn_received
a.tar.gz
說(shuō)明: > 是把輸出轉向到指定的文件,如文件已存在的話(huà)也會(huì )重新寫(xiě)入,文件原內容不會(huì )保留
>> 是把輸出附向到文件的后面,文件原內容會(huì )保留下來(lái)
二,如何能在輸出信息的同時(shí)把信息記錄到文件中?
我們在上面的例子中可以看到,我們使用輸出轉向,命令在終端上的輸出轉向到了文件中,但如果我希望能同時(shí)在終端上看到輸出信息怎么辦?
我們可以使用這個(gè)命令: tee
解釋一下tee的作用:
read from standard input and write to standard output and files
它從標準輸入讀取內容并將其寫(xiě)到標準輸出和文件中
看例子:
[lhd@hongdi ~]$ ls | tee ls_tee.txt
1.gtkrc-2.0
2009
a
amsn_received
a.tar.gz
[lhd@hongdi ~]$ cat ls_tee.txt
1.gtkrc-2.0
2009
a
amsn_received
a.tar.gz
備注:使用 tee時(shí),如果想保留目標文件原有的內容怎么辦?
可以使用 -a參數
-a, --append
append to the given FILEs, do not overwrite
附加至給出的文件,而不是覆蓋它
三,多個(gè)命令的輸出都需要記錄,可以用script
script這個(gè)命令很強大,可以記錄終端的所有輸出到相應的文件中
看例子:
[lhd@hongdi ~]$ script
Script. started, file is typescript
[lhd@hongdi ~]$ 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
[lhd@hongdi ~]$ exit
exit
Script. done, file is typescript
[lhd@hongdi ~]$ cat typescript
Script. started on 2009年02月08日 星期日 18時(shí)56分52秒
[lhd@hongdi ~]$ 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
[lhd@hongdi ~]$ exit
exit
Script. done on 2009年02月08日 星期日 18時(shí)57分00秒
說(shuō)明:
1,我們在啟動(dòng)script時(shí)沒(méi)有指定文件名,它會(huì )自動(dòng)記錄到當前目錄下一個(gè)名為 typescript的文件中。也可以用 -a參數 指定文件名
例子:
[lhd@hongdi ~]$ script. -a example.txt
Script. started, file is example.txt
此時(shí)終端的輸出內容被記錄到 example.txt這個(gè)文件中
2,退出script時(shí),用exit
感到奇怪嗎?事實(shí)上script就是啟動(dòng)了一個(gè)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的內容,可以看到它同時(shí)記錄下了script的啟動(dòng)和結束時(shí)間
四,用script錄制并播放session的內容
我們可以用 script把整個(gè)終端會(huì )話(huà)的所有操作和輸出錄制下來(lái),然后再用scriptreplay進(jìn)行播放。
如果錄制時(shí)記錄下來(lái)了操作時(shí)的時(shí)間數據,那么播放時(shí)和操作時(shí)的使用時(shí)間完全相同。
這個(gè)很有用吧,比如:我們可以把安裝軟件時(shí)編譯的過(guò)程記錄下來(lái),然后給別人進(jìn)行演示
看例子:
[lhd@hongdi ~]$ script. -t 2>example.time -a example.txt
Script. started, file is example.txt
[lhd@hongdi ~]$ ls
說(shuō)明: -t 2>example.time -t是把時(shí)間數據輸出到標準錯誤(standard error),所以我們使用 2>example.time 把數據轉向到 example.time這個(gè)文件當中
如何播放所記錄的內容?
第一步:安裝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
之后復制文件到系統的命令目錄中即可
[root@hongdi 下載]# cp util-linux-2.12r/misc-utils/scriptreplay.pl /usr/bin/scriptreplay
[root@hongdi 下載]# chmod 755 /usr/bin/scriptreplay
備注: fedora 10的util-linux-ng-2.14.1-3.2.fc10.i386.rpm 此包中已包含 scriptreplay,已無(wú)需另行安裝
第二步:播放所錄制的session內容
[lhd@hongdi ~]$ scriptreplay example1.time example1.txt
[lhd@hongdi ~]$ ls
1.gtkrc-2.0 c.tar jeffray_lee@hotmail.com pass
[lhd@hongdi ~]$ abcd
bash: abcd: command not found
[lhd@hongdi ~]$ exit
例子:
[lhd@hongdi ~]$ ls > ls.txt
[lhd@hongdi ~]$ cat ls.txt
1.gtkrc-2.0
2009
a
amsn_received
a.tar.gz
說(shuō)明: > 是把輸出轉向到指定的文件,如文件已存在的話(huà)也會(huì )重新寫(xiě)入,文件原內容不會(huì )保留
>> 是把輸出附向到文件的后面,文件原內容會(huì )保留下來(lái)
二,如何能在輸出信息的同時(shí)把信息記錄到文件中?
我們在上面的例子中可以看到,我們使用輸出轉向,命令在終端上的輸出轉向到了文件中,但如果我希望能同時(shí)在終端上看到輸出信息怎么辦?
我們可以使用這個(gè)命令: tee
解釋一下tee的作用:
read from standard input and write to standard output and files
它從標準輸入讀取內容并將其寫(xiě)到標準輸出和文件中
看例子:
[lhd@hongdi ~]$ ls | tee ls_tee.txt
1.gtkrc-2.0
2009
a
amsn_received
a.tar.gz
[lhd@hongdi ~]$ cat ls_tee.txt
1.gtkrc-2.0
2009
a
amsn_received
a.tar.gz
備注:使用 tee時(shí),如果想保留目標文件原有的內容怎么辦?
可以使用 -a參數
-a, --append
append to the given FILEs, do not overwrite
附加至給出的文件,而不是覆蓋它
三,多個(gè)命令的輸出都需要記錄,可以用script
script這個(gè)命令很強大,可以記錄終端的所有輸出到相應的文件中
看例子:
[lhd@hongdi ~]$ script
Script. started, file is typescript
[lhd@hongdi ~]$ 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
[lhd@hongdi ~]$ exit
exit
Script. done, file is typescript
[lhd@hongdi ~]$ cat typescript
Script. started on 2009年02月08日 星期日 18時(shí)56分52秒
[lhd@hongdi ~]$ 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
[lhd@hongdi ~]$ exit
exit
Script. done on 2009年02月08日 星期日 18時(shí)57分00秒
說(shuō)明:
1,我們在啟動(dòng)script時(shí)沒(méi)有指定文件名,它會(huì )自動(dòng)記錄到當前目錄下一個(gè)名為 typescript的文件中。也可以用 -a參數 指定文件名
例子:
[lhd@hongdi ~]$ script. -a example.txt
Script. started, file is example.txt
此時(shí)終端的輸出內容被記錄到 example.txt這個(gè)文件中
2,退出script時(shí),用exit
感到奇怪嗎?事實(shí)上script就是啟動(dòng)了一個(gè)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的內容,可以看到它同時(shí)記錄下了script的啟動(dòng)和結束時(shí)間
四,用script錄制并播放session的內容
我們可以用 script把整個(gè)終端會(huì )話(huà)的所有操作和輸出錄制下來(lái),然后再用scriptreplay進(jìn)行播放。
如果錄制時(shí)記錄下來(lái)了操作時(shí)的時(shí)間數據,那么播放時(shí)和操作時(shí)的使用時(shí)間完全相同。
這個(gè)很有用吧,比如:我們可以把安裝軟件時(shí)編譯的過(guò)程記錄下來(lái),然后給別人進(jìn)行演示
看例子:
[lhd@hongdi ~]$ script. -t 2>example.time -a example.txt
Script. started, file is example.txt
[lhd@hongdi ~]$ ls
說(shuō)明: -t 2>example.time -t是把時(shí)間數據輸出到標準錯誤(standard error),所以我們使用 2>example.time 把數據轉向到 example.time這個(gè)文件當中
如何播放所記錄的內容?
第一步:安裝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
之后復制文件到系統的命令目錄中即可
[root@hongdi 下載]# cp util-linux-2.12r/misc-utils/scriptreplay.pl /usr/bin/scriptreplay
[root@hongdi 下載]# chmod 755 /usr/bin/scriptreplay
備注: fedora 10的util-linux-ng-2.14.1-3.2.fc10.i386.rpm 此包中已包含 scriptreplay,已無(wú)需另行安裝
第二步:播放所錄制的session內容
[lhd@hongdi ~]$ scriptreplay example1.time example1.txt
[lhd@hongdi ~]$ ls
1.gtkrc-2.0 c.tar jeffray_lee@hotmail.com pass
[lhd@hongdi ~]$ abcd
bash: abcd: command not found
[lhd@hongdi ~]$ exit
*博客內容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀(guān)點(diǎn),如有侵權請聯(lián)系工作人員刪除。
