linux 重定向命令

2021-07-11 03:23:03 字數 2715 閱讀 9290

標準輸入,輸出和錯誤

檔案檔案               描述符

輸入檔案—標準輸入      0

輸出檔案—標準輸出      1

錯誤輸出檔案—標準錯誤  2

1.重定向

command> filename      把標準輸出重定向到乙個新檔案中

command>> filename      把標準輸出重定向到乙個檔案中(追加)

command1 > fielname      把標準輸出重定向到乙個檔案中

command> filename 2>&1    把標準輸出和標準錯誤一起重定向到乙個檔案中

command2 > filename     把標準錯誤重定向到乙個檔案中

command2 >> filename     把標準輸出重定向到乙個檔案中(追加)

command>> filename 2>&1   把標準輸出和標準錯誤一起重定向到乙個檔案中(追加)

command< filename >filename2   把command命令以filename檔案作為標準輸入,以filename2檔案作為標準輸出

command< filename    把command命令以filename檔案作為標準輸入

command<< delimiter   把從標準輸入中讀入,直至遇到delimiter分界符

command<&m    把檔案描述符m作為標準輸入

command>&m    把標準輸出重定向到檔案描述符m中

command<&-    把關閉標準輸入

2.雙向重定向

即在重定向資料到目標檔案的同時,還要保證資料能夠正常處理,使用tee命令。

tee [-a]  file

-a 往檔案尾新增內容

last | teelast_backup | cut -d " " -f 1  #tee相當於對last的結果備份了一次

功能說明:讀取標準輸入的資料,並將其內容輸出成檔案。

語   法:tee [-ai][--help][--version][檔案…]

補充說明:tee指令會從標準輸入裝置讀取資料,將其內容輸出到標準輸出裝置,同時儲存成檔案。我們可利用tee把管道匯入的資料存成檔案,甚至一次儲存數份檔案。

參   數:-a附加到既有檔案的後面,而非覆蓋它。如果給予tee指令的檔名稱已經存在,缺省會覆蓋該檔案的內容。加上此引數後,資料會新增在該檔案內容的最後面,而不會刪除原先之內容。

-i 忽略中斷訊號

--version 顯示版本資訊

範   例:

$ cat slayers.story|tee ss-copy1 ss-copy2 ss-copy3

使用示例

示例一 tee命令與重定向的對比

[root@web ~]# seq 5>1.txt

[root@web ~]# cat1.txt

[root@web ~]# cat1.txt >2.txt

[root@web ~]# cat1.txt | tee 3.txt

[root@web ~]# cat2.txt

[root@web ~]# cat3.txt

[root@web ~]# cat1.txt >>2.txt

[root@web ~]# cat1.txt | tee -a 3.txt

[root@web ~]# cat2.txt

[root@web ~]# cat3.txt

[root@web ~]#

示例二 使用tee命令重複輸出字串

[root@web ~]# echo12345 | tee

[root@web ~]# echo12345 | tee -

[root@web ~]# echo12345 | tee - -

[root@web ~]# echo12345 | tee - - -

[root@web ~]# echo12345 | tee - - - -

[root@web ~]#

[root@web ~]# echo -n12345 | tee

12345[root@web ~]#echo -n 12345 | tee -

1234512345[root@web~]# echo -n 12345 | tee - -

123451234512345[root@web~]# echo -n 12345 | tee - - -

12345123451234512345[root@web~]# echo -n 12345 | tee - - - -

1234512345123451234512345[root@web~]#

示例三使用tee命令把標準錯誤輸出也儲存到檔案

[root@web ~]# ls"*"

ls: *: 沒有那個檔案或目錄

[root@web ~]# ls"*" | tee -

ls: *: 沒有那個檔案或目錄

[root@web ~]# ls"*" | tee ls.txt

ls: *: 沒有那個檔案或目錄

[root@web ~]# catls.txt

[root@web ~]# ls"*" 2>&1 | tee ls.txt

ls: *: 沒有那個檔案或目錄

[root@web ~]# catls.txt

ls: *: 沒有那個檔案或目錄

[root@web ~]#

linux 重定向命令

command filename 把標準輸出重定向到乙個新檔案中 command filename 把標準輸出重定向到乙個檔案中 追加 command 1 fielname 把標準輸出重定向到乙個檔案中 command filename 2 1 把標準輸出和標準錯誤一起重定向到乙個檔案中 comma...

linux重定向命令

昨天參加網易ttt的筆試,填空題考了cmd file 2 1的含義,我以前做專案接觸過重定向命令,但沒用這個,然後居然沒寫出正確答案來,慚愧啊。謹記,對遇到的問題要求甚解,要徹底弄懂,並且還要複習!現把所有命令列出來 cmd file 把 stdout 重定向到 file 檔案中 cmd file ...

Linux命令學習 重定向

標準輸入,輸出,和錯誤到目前為止,我們用到的許多程式都會產生某種輸出。這種輸出,經常由兩種型別組成。第一,程式執行結果 這是說,程式要完成的功能。第二,我們得到狀態和錯誤資訊,這些告訴我們程式進展。如果我們觀察乙個命令,像 ls,會看到它的執行結果和錯誤資訊 顯示在螢幕上。與 unix 主題 任何東...