which命令用於查找並顯示給定命令的絕對路徑。 環境變量PATH中保存了查找命令時需要遍歷的目錄。which指令會在環境變量$PATH設置的目錄裡查找符合條件的文件。也就是說,使用which命令,就可以看到某個系統命令是否存在,以及執行的到底是哪一個位置的命令。
[b] (1)用法:[/b] 用法: which [選項參數] [命令名]
[b] (2)功能:[/b] 功能:查找環境變量中的文件
[b] (3)選項參數:[/b] 1) -n 指定文件名長度,指定的長度必須大於或等於所有文件中最長的文件名。
2) -p 與-n參數相同,但此處的包括了文件的路徑。 3) -w 指定輸出時欄位的寬度。
4) -V 顯示版本信息[b] (4)實例:[/b]
1)[root@localhost sunjimeng]# which pwd 查看命令所在目錄
[root@localhost sunjimeng]# which pwd /usr/bin/pwd [root@localhost sunjimeng]# which head /usr/bin/head [root@localhost sunjimeng]# which cat /usr/bin/cat
[root@localhost sunjimeng]# which adduser /usr/sbin/adduser2)[root@localhost sunjimeng]# which which 用which命令找which命令
[root@localhost sunjimeng]# which which alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' /usr/bin/alias /usr/bin/which3)[root@localhost sunjimeng]# which --help
[root@localhost sunjimeng]# which --help Usage: /usr/bin/which [options] [--] COMMAND [...] Write the full path of COMMAND(s) to standard output. --version, -[vV] Print version and exit successfully. --help, Print this help and exit successfully. --skip-dot Skip directories in PATH that start with a dot. --skip-tilde Skip directories in PATH that start with a tilde. --show-dot Don't expand a dot to current directory in output. --show-tilde Output a tilde for HOME directory for non-root. --tty-only Stop processing options on the right if not on tty. --all, -a Print all matches in PATH, not just the first --read-alias, -i Read list of aliases from stdin. --skip-alias Ignore option --read-alias; don't read stdin. --read-functions Read shell functions from stdin. --skip-functions Ignore option --read-functions; don't read stdin. Recommended use is to write the output of (alias; declare -f) to standard input, so that which can show aliases and shell functions. See which(1) for examples. If the options --read-alias and/or --read-functions are specified then the output can be a full alias or function definition, optionally followed by the full path of each command used inside of those. Report bugs to <[email protected]>.4)[root@localhost sunjimeng]# which --version
[root@localhost sunjimeng]# which --version GNU which v2.20, Copyright (C) 1999 - 2008 Carlo Wood. GNU which comes with ABSOLUTELY NO WARRANTY; This program is free software; your freedom to use, change and distribute this program is protected by the GPL.
[root@localhost sunjimeng]# which -V GNU which v2.20, Copyright (C) 1999 - 2008 Carlo Wood. GNU which comes with ABSOLUTELY NO WARRANTY; This program is free software; your freedom to use, change and distribute this program is protected by the GPL.
[b] 5)其他:[/b] 說明:
1.which 是根據使用者所配置的 PATH 變量內的目錄去搜尋可運行檔的!所以,不同的 PATH 配置內容所找到的命令當然不一樣的! 2.竟然會有兩個 which ,其中一個是 alias 這就是所謂的『命令別名』,意思是輸入 which 會等於後面接的那串命令!
3.bash內建命令:
1).什麼是build in命令: shell內建命令是指bash(或其它版本)工具集中的命令。一般都會有一個與之同名的系統命令,比如bash中的echo命令與/bin/echo是兩個不同的命令,盡管他們行為大體相仿。當在bash中鍵入一個命令時系統會先看他是否是一個內建命令,如果不是才會查看是否是系統命令或第三方工具。所以在bash中鍵入echo命令實際上執行bash工具集中的bash命令也就是內建命令,而不是/bin/echo這個系統命令。
2).內建命令與系統命令 內建命令要比系統論命令有比較高的執行效率。外部命令執行時往往需要fork出(產生出)一個子進程,而內建命令一般不用。 3).查看一個命令是系統命令還是內建命令:type
[root@localhost sunjimeng]# type -a pwd pwd 是 shell 內嵌 pwd 是 /usr/bin/pwd pwd 是 /bin/pwd [root@localhost sunjimeng]# type -a echo echo 是 shell 內嵌 echo 是 /usr/bin/echo echo 是 /bin/echo可以看出,有些命令,echo和pwd同時是內建命令和系統命令。
4.補充:
我們經常在linux要查找某個文件,但不知道放在哪裡了,可以使用下面的一些命令來搜索:
which 查看可執行文件的位置。
whereis 查看文件的位置。
locate 配合數據庫查看文件位置。
find 實際搜尋硬盤查詢文件名稱。