本篇重點 基本重定向用法:<、<<、>、>>、 2>、2>> 重定向的判讀方式 重定向符號的前後順序不影響命令的結果 特殊重定向用法:/dev/null、&>、 2>&1 、 1>&2 在 Linux 中,資料重定向是一個非常重要且強大的功能。它可以將命令的輸入或輸出重定向到不同的文件或設備,讓我們能更靈活地處理資料。
基本重定向用法 標準輸入 <
輸入重定向將文件內容作為命令輸入
範例
number.txt
command
1 2 3 4 5 6 7 8 9 10 sort -n < number.txt4 9 11 44 63 85
sort -n < number.txt
,-n
選項是指定對數字進行排序
<<
多行輸入重定向將多行文字作為命令的輸入,直到遇到指定的結束標記
範例
command
1 2 3 4 5 6 7 8 9 cat << EOF Hello World EOF Hello World
EOF
是一個告訴 linux 開始與結束的標記,用於長文本段落或多行命令的輸入。
EOF
本身並沒有特別的含義,可以用其他任意的字串來代替,只要確保開始標記和結束標記一致即可
動手做做看 標準輸出 實例搶先看 實例搶先看 >
輸出重定向將命令的標準輸出重定向到文件,會覆蓋文件的原有內容
範例
command
1 2 3 4 5 6 7 8 echo hello world > output.txtcat output.txthello world
>>
附加重定向將命令的標準輸出附加到文件末尾,不會覆蓋文件的原有內容
範例
command
1 2 3 4 5 6 7 8 9 echo hello world >> output.txtcat output.txthello world hello world
動手做做看 錯誤輸出 實例搶先看 實例搶先看 2>
錯誤輸出重定向將命令的錯誤輸出重定向到文件,會覆蓋文件的原有內容
範例
command
1 2 3 4 5 6 7 8 9 10 ls non_existing_file 2> error.logcat error.logls : cannot access 'non_existing_file' : No such file or directory
2>>
錯誤輸出附加重定向將命令的錯誤輸出附加到文件末尾,不會覆蓋文件的原有內容
範例
command
1 2 3 4 5 6 7 8 9 10 11 ls non_existing_file 2>> error.logcat error.logls : cannot access 'non_existing_file' : No such file or directoryls : cannot access 'non_existing_file' : No such file or directory
動手做做看 綜合應用 標準輸入結合標準輸出 command
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 cat << EOF > input.txt hello test 123 EOF cat input.txthello test 123cat < input.txt > output.txtcat output.txthello test 123
標準輸出結合錯誤輸出 command
1 2 3 4 5 6 7 8 9 10 11 12 13 14 cat non_existing_file > output.log 2> error.logcat output.logcat error.logcat : non_existing_file: No such file or directory
動手做做看 重定向的判讀方式 [重定向符號] + [文件或字串]
根據重定向符號和文件或字串來判斷,因此符號的前後順序不影響命令的結果
Shell 在執行命令之前會先解析所有的重定向符號,再執行命令。
Shell 是用來和操作系統進行溝通的命令行界面(CLI),允許用戶通過輸入命令來控制系統執行程序、管理文件系統等
cat < input.txt > output.txt
可以分解為:
< input.txt
:這部分將 input.txt
的內容重定向到 cat
命令的標準輸入> output.txt
:這部分將 cat
命令的標準輸出重定向到 output.txt
Shell 會先解析重定向符號,再執行 cat
命令,從 input.txt
讀取內容並將其寫入 output.txt
因此不管是 cat < input.txt > output.txt
或是 cat > output.txt < input.txt
結果都是一樣的!符號的前後順序不影響命令的結果。
特殊重定向用法 /dev/null 可以視為垃圾桶,任何資料重定向給它就會丟棄
範例
command
1 2 3 4 5 ls non_existing_file 2> /dev/null
重定向到 /dev/null 表示丟棄所有寫入它的數據,因此無法復原 要謹慎使用!
動手做做看 &> 將命令的標準輸出和錯誤輸出都重定向到同一個文件
範例
command
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 cat non_existing_file &> output.txtcat output.txtcat : cannot access 'non_existing_file' : No such file or directoryecho test > test.txtcat test.txt &> output.txtcat output.txttest
需要重複輸出到同一個檔案時,可以使用 &>>
,將輸出附加到檔案末尾
動手做做看 2>&1、1>&2 2>&1 表示將標準錯誤輸出重定向到標準輸出 1>&2 表示將標準輸出輸出重定向到錯誤輸出 範例
test.txt
2>&1
command
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ls test.txt non_existing_file > output.txt 2>&1cat output.txtls : cannot access 'non_existing_file' : No such file or directorytest.txt ls test.txt non_existing_file 2> output.txt 2>&1cat output.txt
1>&2
command
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ls test.txt non_existing_file 2> output.txt 1>&2cat output.txtls : cannot access 'non_existing_file' : No such file or directorytest.txt ls test.txt non_existing_file > output.txt 1>&2cat output.txt
同時重定向標準輸出和標準錯誤輸出,可以使用
command &> output.log command > output.log 2>&1 command 2> output.log 1>&2 三種方法都有一樣的效果
動手做做看 結論 了解並熟悉資料輸出重定向的用法,能夠讓我們更靈活地處理和管理命令的輸入輸出。以下是一些需要注意的地方:
依不同的需求,使用不同的輸出方式>
和 2>
會覆蓋輸出檔案原本的內容。>>
和 2>>
會將輸出附加到檔案原本的內容末尾。 重定向符號的前後順序不影響命令的結果 如果你有其他有趣的用法或技巧,歡迎在下方留言分享!