可以理解為linux跟蹤打開文件,而分配的一個數字,這個數字有點類似c語言操作文件時候的句柄,通過句柄就可以實現文件的讀寫操作
用戶可以自定義文件描述符范圍是:3-num,這個最大數字,跟用戶的:ulimit –n 定義數字有關系,不能超過最大值
查看系統文件描述符最大值
[root@localhost ~]# ulimit -n
1024
linux系統默認文件描述符
標准輸入,標准輸出和標准錯誤默認使用鍵盤和顯示器作為關聯設備與操作系統進行交互完成最基本的輸入,輸出操作
語法:命令 [文件描述符] >或>> 文件或文件操作符或設備
查看當前目錄下的文件
[[email protected] ~]# ls
test.txt
查看當前目錄下的test.txt文件和一個不存在的test1.txt文件
[[email protected] ~]# ls test.txt test1.txt
ls: cannot access test1.txt: No such file or directory
test.txt
將正確的輸出重定向到文件success.txt中
[[email protected] ~]# ls test.txt test1.txt 1>success.txt
ls: cannot access test1.txt: No such file or directory
[[email protected] ~]# cat success.txt
test.txt
將錯誤的輸出重定向到文件error.txt中
[[email protected] ~]# ls test.txt test1.txt 2>error.txt
test.txt
[[email protected] ~]# cat error.txt
ls: cannot access test1.txt: No such file or directory
&1:代綁定到表標准輸出
&2:代表綁定到標准錯誤輸出
&-:代表關閉與他綁定的文件描述符
關閉標准錯誤輸出
[[email protected] ~]# ls test.txt test1.txt >success.txt 2>&-
[[email protected] ~]# cat success.txt
test.txt
將標准錯誤輸出綁定到標准輸出
[[email protected] ~]# ls test.txt test1.txt > all.txt 2>&1
[[email protected] ~]# cat all.txt
ls: cannot access test1.txt: No such file or directory
test.txt
將標准錯誤輸出重定向到黑洞設備
[[email protected] ~]# ls test.txt test1.txt 2>/dev/null
test.txt
&>:混合輸出(不分對錯)
[[email protected] ~]# ls test.txt test1.txt &> all.txt
[[email protected] ~]# cat all.txt
ls: cannot access test1.txt: No such file or directory
test.txt
注意事項:
將命令中接收輸入的途徑由默認的鍵盤改為其他文件而不是等待從鍵盤輸入
通過重定向輸入可以使一些交互式操作過程能夠通過讀取文件來完成
操作符: <
使用passwd設置密碼時,每次都根據提示輸入密碼比較煩瑣
改用重定向輸入將可以忽略交互式的過程而自動完成密碼設置
使用非交互式的去執行設置密碼
[[email protected] ~]# touch passwd.txt
[[email protected] ~]# echo 123456 > passwd.txt
[[email protected] ~]# passwd test --stdin < passwd.txt
Changing password for user test.
passwd: all authentication tokens updated successfully.
管道命令操作符是:”|”,它僅��處理經由前面一個指令傳出的正確輸出信息
也就是 standard output 的信息,對於 stdandard error 信息沒有直接處理能力
然後,傳遞給下一個命令,作為標准的輸入 standard input
command1正確輸出,作為command2的輸入 然後comand2的輸出作為,comand3的輸入 ,comand3輸出就會直接顯示在屏幕上面了
通過管道之後:comand1,comand2的正確輸出不顯示在屏幕上面
[[email protected] ~]# ls -f | grep all
all.txt