易學筆記-Linux命令-第6章:使用命令
- 使用命令
- 命令的存放形式
- 是一個可執行檔案,它們位於/usr/bin下下,比如用C或者C++編寫的程式或者用指令碼語言編寫的指令碼程式,比如shell,perl,python等等
- 是一個內建的shell自身命令,比如 cd 命令
- 是一個shell函式,常用於環境變數中,後續章節會介紹
- 是一個命令別名,建立在內建命令之上
- type – 說明怎樣解釋一個命令名
- 概念:用於說明某個命令屬於以上四種命令形式中的哪一種
- 語法:type common
- 舉例:
- 可執行程式
[
nexus is /usr/bin/nexus
- 內建shell
[[email protected] ~]# type cd
cd is a shell builtin
- 命令別名
[[email protected] ~]# type cp
cp is aliased to `cp -i'
- 可執行程式
- which – 顯示會執行程式在哪裡
-
[[email protected] ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[[email protected] ~]# which cd
/usr/bin/cd
[[email protected] ~]# which nexus
/usr/bin/nexus
[[email protected] ~]# which cp
alias cp='cp -i'
/usr/bin/cp
[[email protected] ~]#
-
- help -- 幫助選項
- 作用:顯示幫助說明
- 幫助內容兩種顯示形式:一個命令一般只支援一種顯示形式
- help在前面:
- 語法:help common
- 舉例:
[[email protected] ~]# help cd
cd: cd [-L|[-P [-e]]] [dir] [ ]表示可選選項,| 表示互斥選項,也就是要麼-L,要麼-P,兩種不能同時出現
Change the shell working directory.
Options:
-L force symbolic links to be followed
-P use the physical directory structure without following symbolic
links
-e if the -P option is supplied, and the current working directory
cannot be determined successfully, exit with a non-zero status
- help在後面:
- 語法: common --help
- 舉例:
[[email protected] ~]# cd --help
-bash: cd: --: invalid option 說明cd 命令不支援後置help,只支援前置help
cd: usage: cd [-L|[-P [-e]]] [dir]
[[email protected] ~]# mkdir –help mkdir後置help
Usage: mkdir [OPTION]... DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.
Mandatory arguments to long options are mandatory for short options too.
-m, --mode=MODE set file mode (as in chmod), not a=rwx - umask
-p, --parents no error if existing, make parent directories as needed
-v, --verbose print a message for each created directory
-Z set SELinux security context of each created directory
to the default type
--context[=CTX] like -Z, or if CTX is specified then set the SELinux
or SMACK security context to CTX
--help display this help and exit
--version output version information and exit
- help在前面:
- 舉例:
- man – 顯示命令手冊頁
- 作用:以分頁的形式顯示命令內容
- 語法:man common
- 舉例:翻頁操作參照: 第4章:研究作業系統 中的less命令
[[email protected] ~]# man cd
is searched if no file is found in PATH. If the sourcepath option to the
shopt builtin command is turned off, the PATH is not searched. If any
arguments are supplied, they become the positional parameters when file‐
name is executed. Otherwise the positional parameters are unchanged.
Manual page cd(1) line 1/1489 2% (press h for help or q to quit) 1/1489:1表示當前頁 1489表示總的頁手
- man手冊章節安排
章節 內容 1 使用者命令 2 程式介面核心系統呼叫 3 C 庫函式程式介面 4 特殊檔案,比如說裝置結點和驅動程式 5 檔案格式 6 遊戲娛樂,如螢幕保護程式 7 其他方面 8 系統管理員命令 - 通過段代號定位章節,通常預設是第1段
- apropos – 顯示一系列適合的命令
- 作用:按關鍵字查詢手冊頁
- 舉例:
[[email protected] wys_bak]# apropos reset
clearerr (3) - check and reset stream status
feof (3) - check and reset stream status
ferror (3) - check and reset stream status
fileno (3) - check and reset stream status
- 按章節和手冊頁開啟手冊
[[email protected] wys_bak]# man 3 fileno
clearerr, feof, ferror, fileno - check and reset stream status
- whatis– 顯示一個命令的簡潔描述
- 作用:顯示非常簡短的命令說明
-
[[email protected] wys_bak]# whatis mkdir
mkdir (1) - make directories
mkdir (1p) - make directories
mkdir (2) - create a directory
mkdir (3p) - make a directory
- 舉例:
[[email protected] wys_bak]# man 3 fileno
clearerr, feof, ferror, fileno - check and reset stream status
- info – 顯示命令 info,是命令手冊的替代物
- alias – 建立命令別名
- 作用:將多個命令也就是組合命令拼接到一個命令中
- 檢視系統預先定義的別名
[[email protected] wys_bak]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i' 預設提示原來在這裡設定了
alias rm='rm -i' 預設提示原來在這裡設定了
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
- 語法:alias 別名='命令組合',命令組合中多個命令之間用分號隔開
- 舉例:
[[email protected] wys_bak]# alias nexus_home='cd /home/nexus;ls;cd -' 定義別名
[[email protected] wys_bak]# nexus_home 使用命令別名
nexus nexus-2.14.9-01 sonatype-work
/root/wys_bak
[[email protected] wys_bak]#
- 別名注意事項
- 定義好別名後可以在當前視窗的任何目錄執行
- 如果當前視窗關閉後該命令別名就會失效,後續會介紹如何保持視窗永生效
- 刪除別名
[[email protected] wys_bak]# unalias nexus_home
[[email protected] wys_bak]# nexus_home
-bash: nexus_home: command not found