linux命令分為兩種:
內部命令
linux內置的指令
外部命令
對應一個位于文件系統某目錄下的可執行程序
通過type指令可區分命令類型
[root@senlong ~]# type cd # 內部命令cd is a shell builtin[root@senlong ~]# type cat # 外部命令cat is /bin/cat[root@senlong ~]# which cat # 外部命令路徑/bin/cat[root@senlong ~]# whereis cat # 外部命令路徑cat: /bin/cat /usr/share/man/man1/cat.1.gzshell程序搜尋可執行程序文件的路徑定義在PATH環境變量中;
[root@senlong ~]# echo $PATH # 輸出環境變量內部命令幫助文檔
help COMMAND
[root@senlong ~]# help cdcd: cd [-L|-P] [dir] Change the shell working directory. Change the current directory to DIR. The default DIR is the value of the HOME shell variable. ...外部命令幫助文檔
外部命令幫助文檔的獲取有多種途徑,基本上使用man即可滿足需求
[root@senlong ~]# man catman手冊查看方式:
gg:頭部
G: 尾部
d: 向下翻半屏
u: 向上翻半屏
j: 向下一行
k: 向上一行
空格:向下翻一屏
SYNOPSIS段落符號
[] 可選內容
<> 必選內容
a|b 二選一
... 同一內容可出現多次
SYNOPSIScat [OPTION]... [FILE]...
文本搜索:
/KEYWord:以KEYWORD指定的字符串為關鍵字,從當前位置向文件尾部搜索;不區分字符大小寫;n: 下一個N:上一個
?KEYWORD:以KEYWORD指定的字符串為關鍵字,從當前位置向文件首部搜索;不區分字符大小寫;n: 跟搜索命令同方向,下一個N:跟搜索命令反方向,上一個
history命令
作用:管理命令歷史登錄shell時,會讀取命令歷史文件中記錄下的命令:~/.bash_history登錄進shell后新執行的命令只會記錄在緩存中;這些命令會用戶退出時“追加”至命令歷史文件中;
參數-a: 追加本次會話新執行的命令歷史列表至歷史文件中;-d: 刪除歷史中指定的命令;-c: 清空命令歷史;
快捷操作!#: 調用歷史中第#條命令;!string:調用歷史中最近一個以string開頭的命令;!!: 上一條命令
IO重定向及管道
程序是由指令+數據組成的。換言之,程序是對讀入的數據進行處理,再輸出數據。數據的輸入(Input),輸出(Output),簡稱為IO,在沒有指定輸入輸出的情況下,默認為標準輸入和標準輸出。打開的文件都有一個文件描述符(fd: file descriptor)表現為一個數字
標準輸入:keyborad(鍵盤), 文件描述符:0
標準輸出:monitor(顯示屏), 文件描述符:1
標準錯誤輸出:monitor, 文件描述符:2
I/O重定向:改變標準輸入與輸出的默認位置
標準輸出重定向
輸出重定向:COMMAND > NEW_POS, COMMAND >> NEW_POS
>:覆蓋重定向,目標文件中的原有內容會被清除;
>>: 追加重定向,新內容會追加至目標文件尾部;
[root@senlong ~]# ls /etc > /tmp/etc.out注意:覆蓋文件內容具有風險
[root@senlong ~]# set -C # 禁止將內容覆蓋輸出至已有文件中[root@senlong ~]# ls /etc > /tmp/etc.out-bash: /tmp/etc.out: cannot overwrite existing file[root@senlong ~]# ls /etc >| /tmp/etc.out # 強制覆蓋[root@senlong ~]# set +C # 允許將內容覆蓋輸出至已有文件中標準錯誤輸出重定向
2>: 覆蓋重定向錯誤輸出數據流;
2>>: 追加重定向錯誤輸出數據流;
標準輸出和錯誤輸出各自定向至不同位置:COMMAND > /path/to/file.out 2> /path/to/error.out 這種寫法會創建兩個文件,一個為空
合并標準輸出和錯誤輸出為同一個數據流進行重定向:只會生成一個文件
&>:覆蓋重定向
&>>:追加重定向
[root@senlong tmp]# echo $PATH &> /tmp/path.out標準輸入重定向
輸入重定向:<
tr命令:轉換或刪除字符 默認接受鍵標準輸入(鍵盤輸入)
[root@senlong tmp]# tr abc ABC # 將abc轉成ABChellohelloalpha AlphA[root@senlong tmp]# cat testhello world[root@senlong tmp]# tr eo EO < ./testhEllO wOrldHERE Documentation:<< 創建文檔
cat << EOF
cat > /path/to/somefile << EOF
[root@senlong tmp]# cat << EOF> how are you?> how old are you?> EOFhow are you?how old are you?[root@senlong tmp]# cat >> /tmp/test.out << EOF> how are you?> how old are you?> EOF[root@senlong tmp]# cat /tmp/test.out how are you?how old are you?tee命令
同時在顯示屏和文件中輸出:一路輸入,兩路輸出
tee [OPTION]... [FILE]...
[root@senlong tmp]# tee /tmp/tee.outhellohelloworldworld[root@senlong tmp]# cat /tmp/tee.out helloworld管道 |
COMMAND1 | COMMAND2 | COMMAND3 |...
Note:最后一個命令會在當前shell進程的子shell進程中執行;
[root@senlong tmp]# echo $PATH | tr 'a-z' 'A-Z'/USR/LIB/GOLANG/BIN/LINUX_AMD64:/USR/LOCAL/SBIN:/USR/LOCAL/BIN:/SBIN:/BIN:/USR/SBIN:/USR/BIN:/ROOT/BIN:/USR/LOCAL/GIT/BIN:/ROOT/BIN[root@senlong tmp]# echo $PATH | tr 'a-z' 'A-Z' | tr -d 'U'/SR/LIB/GOLANG/BIN/LINX_AMD64:/SR/LOCAL/SBIN:/SR/LOCAL/BIN:/SBIN:/BIN:/SR/SBIN:/SR/BIN:/ROOT/BIN:/SR/LOCAL/GIT/BIN:/ROOT/BIN練習
1.將/etc/passwd文件中的前5行內容轉換為大寫后保存至/tmp/passwd.out文件中
[root@senlong tmp]# head -n 5 /etc/passwd | tr 'a-z' 'A-Z' > /tmp/passwd.out[root@senlong tmp]# cat /tmp/passwd.out ROOT:X:0:0:ROOT:/ROOT:/BIN/BASHBIN:X:1:1:BIN:/BIN:/SBIN/NOLOGINDAEMON:X:2:2:DAEMON:/SBIN:/SBIN/NOLOGINADM:X:3:4:ADM:/VAR/ADM:/SBIN/NOLOGINLP:X:4:7:LP:/VAR/SPOOL/LPD:/SBIN/NOLOGIN2.將登錄至當前系統上用戶信息中的后3行的信息轉換為大寫后保存至/tmp/who.out文件中
[root@senlong tmp]# who | tail -n 3 | tr 'a-z' 'A-Z' > /tmp/who.out[root@senlong tmp]# cat /tmp/who.out ROOT PTS/0 2017-01-16 17:05 (10.0.2.2)
新聞熱點
疑難解答