什麼是文件描述符FD或者文件句柄?python
經過構建一個帶有編號標記的通道(文件描述符)的進程結構來管理打開的文件。今晨鏈接到文件,從而達到這些文件所表明的的數據內容或者設備。經過使用通道0、一、2(稱爲標準輸入,標準輸出,標準錯誤)的默認鏈接建立進程。進程使用3號及以上標號的通道鏈接其餘文件。git
0 表示stdin(標準輸入),默認鏈接爲鍵盤,僅讀取 1 表示stdout(標準輸出),默認鏈接爲終端,僅寫入 2 表示stderr(標準錯誤),默認鏈接爲終端,僅寫入 3+ 表示filename(其餘文件),能夠讀取/寫入
示例:vim
保存時間戳bash
[root@localhost ~]# date 2018年 08月 24日 星期五 09:03:31 CST [root@localhost ~]# date > /tmp/saved-timestamp [root@localhost ~]# cat /tmp/saved-timestamp 2018年 08月 24日 星期五 09:03:46 CST [root@localhost ~]#
將一個文件的最後100行保存到另外一個文件less
[root@localhost ~]# tail -n 100 /var/log/demsg > /tmp/last-100-boot-messages [root@localhost ~]#
將四個文件合併爲一個ssh
[root@localhost ~]# cat file1 file2 file3 file4 > /tmp/all-four-in-one [root@localhost ~]#
累出家目錄中的隱藏文件名和常規文件名保存到文件中工具
[root@localhost ~]# ls -a > /tmp/my-file-names [root@localhost ~]# cat /tmp/my-file-names . .. anaconda-ks.cfg .bash_history .bash_logout .bash_profile .bashrc .cache .config .cshrc .dbus Desktop .gitconfig initial-setup-ks.cfg .mozilla .pki .ssh .tcshrc .viminfo .xauthKYrxVH .Xauthority [root@localhost ~]#
使用普通用戶對系統目錄進行訪問會被拒絕,將錯誤從定向到文件oop
[root@localhost ~]# find /etc -name passwd 2 > /tmp/errors [root@localhost ~]#
將命令的輸出和錯誤消息分別保存到單獨的文件中spa
[root@localhost ~]# find /etc/-name passwd > /tmp/output 2 > /tmp/errors [root@localhost ~]#
忽略並丟棄錯誤消息命令行
[root@localhost ~]# find /etc/ -name passwd > /tmp/output 2> /dev/null [root@localhost ~]#
head /tail/wc/cut/sort/uniq/diff/patch/tr/grep
進程管道PIping
示例:
將ls長葛市輸出分頁顯示
[root@localhost ~]# ls -l /usr/bin/ | less 總用量 173472 -rwxr-xr-x. 1 root root 41496 11月 6 2016 [ -rwxr-xr-x. 1 root root 107856 8月 3 2017 a2p -rwxr-xr-x. 1 root root 11248 8月 10 2017 abrt-action-analyze-backtrace -rwxr-xr-x. 1 root root 15328 8月 10 2017 abrt-action-analyze-c -rwxr-xr-x. 1 root root 1345 8月 10 2017 abrt-action-analyze-ccpp-local -rwxr-xr-x. 1 root root 6821 8月 10 2017 abrt-action-analyze-core -rwxr-xr-x. 1 root root 11224 8月 10 2017 abrt-action-analyze-oops -rwxr-xr-x. 1 root root 11232 8月 10 2017 abrt-action-analyze-python
計算ls輸出的行數而且保存到文件
[root@localhost ~]# ls | wc -l > /tmp/how-many-files [root@localhost ~]# cat /tmp/how-many-files 3 [root@localhost ~]#
將ls輸出的前10行保存到文件
[root@localhost ~]# ls -t | head -n 10 > /tmp/ten-last-changed-files [root@localhost ~]# cat /tmp/ten-last-changed-files Desktop initial-setup-ks.cfg anaconda-ks.cfg [root@localhost ~]# 只有三行。。。
在終端商顯示ls列表,同時將文件列表存儲到文件中
[root@localhost ~]# ls -l | tee /tmp/saved-output 總用量 8 -rw-------. 1 root root 1635 8月 20 19:18 anaconda-ks.cfg drwxr-xr-x. 2 root root 6 8月 23 18:14 Desktop -rw-r--r--. 1 root root 1666 8月 20 19:21 initial-setup-ks.cfg [root@localhost ~]# cat /tmp/saved-output 總用量 8 -rw-------. 1 root root 1635 8月 20 19:18 anaconda-ks.cfg drwxr-xr-x. 2 root root 6 8月 23 18:14 Desktop -rw-r--r--. 1 root root 1666 8月 20 19:21 initial-setup-ks.cfg [root@localhost ~]#
肯定當前窗口的終端設備,將ls結果做爲郵件發送,並在此窗口查看輸出內容
[root@localhost ~]# ls -l | tee /dev/pts/1 | mail -s subject seven_nighter@163.com 總用量 8 -rw-------. 1 root root 1635 8月 20 19:18 anaconda-ks.cfg drwxr-xr-x. 2 root root 6 8月 23 18:14 Desktop -rw-r--r--. 1 root root 1666 8月 20 19:21 initial-setup-ks.cfg 您在 /var/spool/mail/root 中有郵件 [root@localhost ~]#
xargs命令是給其餘命令傳遞參數的一個過濾器,也是組合多個命令的一個工具,它擅長將標準輸入數據轉換成命令行參數,xargs可以處理管道或者stdin而且將其轉換成特定的命令的參數。xargs也能夠將單行或者多行文本輸入轉換爲其餘格式,例如多行變單行,單行變多行。xargs的默認命令時echo,空格是默認的定界符,這就意味着經過管道傳遞給xargs的輸入將會包含換行和空白,不過經過xargs的處理,換行和空白將被空格取代,xargs是構建單行命令的重要組件之一。
具體操做百度。。
wc - print newline, word,and bytes counts for each file
計算文件的行數,單詞數,字節數
[root@localhost ~]# wc /etc/passwd 42 82 2146 /etc/passwd [root@localhost ~]#
[root@localhost ~]# wc -l /etc/passwd 42 /etc/passwd 行數 [root@localhost ~]#
[root@localhost ~]# wc -w /etc/passwd 82 /etc/passwd 單詞數 [root@localhost ~]#
[root@localhost ~]# wc -c /etc/passwd 2146 /etc/passwd 字節數 [root@localhost ~]#
按列提取文件
-d 指明列分隔符 默認tab
-f 選擇輸出的區域
-c 指定字符位置
[root@localhost ~]# cut -d: -f1 /etc/passwd root bin daemon adm lp sync shutdown halt mail operator
[root@localhost ~]# cut -c1-3 /etc/passwd roo bin dae adm lp: syn shu
sort - sort lines of text files
排序輸出,默認按照首字符從頭到尾的順序排序
-r 逆序(倒敘)
-n 按數字排序
-t 指明分隔符 與 -k 連用
-k 按指定的域排序
[root@localhost ~]# sort -t: -k3 /etc/passwd | cut -d: -f3 | head -n 5 0 1000 1001 107 11 [root@localhost ~]#
[root@localhost ~]# sort -t: -k3 -n /etc/passwd | cut -d: -f3 | head -n 5 0 1 2 3 4 [root@localhost ~]#
[root@localhost ~]# sort -t: -k3 -n -r /etc/passwd | cut -d: -f3 | head -n 5 65534 1001 1000 999 998 [root@localhost ~]#
。。。。更加纖細的請自行百度。