Command Line 資料重導向相關命令

stdinstdoutstderr

  • 0:stdin(Standard input)標準輸入
  • 1:stdout(Standard output)標準輸出
  • 2:stderr(Standard error)標準錯誤輸出

>

redirect output to a file(將stdout導向檔案,會覆寫原有檔案)

# 將現在路徑輸出至 path.txt
pwd > path.txt

>>

redirect output to an existing file(將stdout導向檔案末尾,原檔案內容保留)

# 將現在路徑內的檔案列表輸出至 path.txt 末,但保留 path.txt 原有的資料
ls >> path.txt

<

read input from a file (將檔案當作stdin

# 列出 text.txt 內含有字元 t 的行
grep t < text.txt

# 輸出至 output.txt
grep t < text.txt > output.txt

|

pass output to another command(將stdout當作另一指令的stdin

# 列出現在資料夾內含有'a'字元的檔案
ls | grep a

# 將`ls -l`的輸出用`less`分頁
ls -l | less

tee

both redirect output to a file and pass it to another command(>|的綜合,將stdout導向檔案之後,後面繼續接|,再將stdout當作另一指令的stdin),目的是為了導向到多個輸出。

可以想像成大寫的 T,就很好理解它的管道概念了。

ls | tee list.txt | grep a

# -a:追加而不覆蓋目標文件
ls | tee -a list.txt | grep a

stderr的重導

stderr預設會輸出至終端,如果想要輸出至檔案,也可以用|&代替|,則stderr會與stdout一起輸出至管道後方指定的程式或檔案。

標準檔案控制代碼的重導

可將檔案描述符(數字)放在重導向符號前,影響重導向的資料流

# `stderr`會輸出至stderr.txt,`stdin`則一樣輸出到終端
cat input.txt 2> stderr.txt

# `stderr`會與`stdin`融合輸出至result.txt
find / -name .profile > result.txt 2>&1

參考資料

最後更新時間: