which command

觀看 help

執行

$ which -h

顯示

Illegal option -h
Usage: /usr/bin/which [-a] args

執行

$ which --help

顯示

Illegal option --
Usage: /usr/bin/which [-a] args

觀看版本

執行

$ which --version

顯示

Illegal option --
Usage: /usr/bin/which [-a] args

觀看 manpage

執行

$ whatis which

顯示

which (1)            - locate a command

執行

$ man which

會看到「說明」。

查詢 which 放在那個位置

執行

$ whereis which

顯示

which: /usr/bin/which /bin/which /usr/share/man/man1/which.1.gz

執行

$ which which

顯示

/usr/bin/which

執行

$ which -a which

顯示

/usr/bin/which
/bin/which

執行

$ type which

顯示

which is hashed (/usr/bin/which)

執行

$ type -a which

顯示

which is /usr/bin/which
which is /bin/which

執行

$ ls /usr/bin/which -l

顯示

lrwxrwxrwx 1 root root 10 Apr 24  2016 /usr/bin/which -> /bin/which

執行

$ stat /usr/bin/which

顯示

File: '/usr/bin/which' -> '/bin/which'
Size: 10            Blocks: 0          IO Block: 4096   symbolic link
Device: 822h/2082d  Inode: 4195706     Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-12-23 20:17:47.509825046 +0800
Modify: 2016-04-24 00:13:14.455049698 +0800
Change: 2016-04-24 00:13:14.455049698 +0800
Birth: -

執行

$ file /usr/bin/which

顯示

/usr/bin/which: symbolic link to /bin/which

執行

$ file /bin/which

顯示

/bin/which: POSIX shell script, ASCII text executable

執行下面指令,來觀看「/usr/bin/which」的內容,會看到「/bin/which」的內容。

$ less /usr/bin/which

執行下面指令,來觀看「/bin/which」的內容。

$ less /bin/which

註:「less」可以改為「more」或「view」或「vim」或「mcedit」或「其他文字編輯器」來觀看。

查詢 which 屬於那個套件

執行

$ dpkg -S /usr/bin/which

或是執行

$ dpkg -S $(which which)

顯示

dpkg-query: no path found matching pattern /usr/bin/which

執行

$ dpkg -S /bin/which

顯示

$ debianutils: /bin/which

執行

$ dpkg -S $(which -a which)

顯示

dpkg-query: no path found matching pattern /usr/bin/which
debianutils: /bin/which

執行

$ grep '/usr/bin/which' /var/lib/dpkg/info/* -R -n

顯示

/var/lib/dpkg/info/debianutils.postinst:14:    if [ ! -e /usr/bin/which ]
/var/lib/dpkg/info/debianutils.postinst:16:     ln -s /bin/which /usr/bin/which
/var/lib/dpkg/info/debianutils.postrm:11:    if test -L /usr/bin/which; then
/var/lib/dpkg/info/debianutils.postrm:12:       rm /usr/bin/which
/var/lib/dpkg/info/keyboard-configuration.postinst:8:# The same as /usr/bin/which - in order to make "which" available before

執行下面指令 (多加了 -A 3)

$ grep '/usr/bin/which' /var/lib/dpkg/info/* -R -n -A 3

顯示

/var/lib/dpkg/info/debianutils.postinst:14:    if [ ! -e /usr/bin/which ]
/var/lib/dpkg/info/debianutils.postinst-15-    then
/var/lib/dpkg/info/debianutils.postinst:16:     ln -s /bin/which /usr/bin/which
/var/lib/dpkg/info/debianutils.postinst-17-    fi
/var/lib/dpkg/info/debianutils.postinst-18-    ;;
/var/lib/dpkg/info/debianutils.postinst-19-
--
/var/lib/dpkg/info/debianutils.postrm:11:    if test -L /usr/bin/which; then
/var/lib/dpkg/info/debianutils.postrm:12:       rm /usr/bin/which
/var/lib/dpkg/info/debianutils.postrm-13-    fi
/var/lib/dpkg/info/debianutils.postrm-14-        ;;
/var/lib/dpkg/info/debianutils.postrm-15-    upgrade|failed-upgrade|purge|abort-install|abort-upgrade)
--
/var/lib/dpkg/info/keyboard-configuration.postinst:8:# The same as /usr/bin/which - in order to make "which" available before
/var/lib/dpkg/info/keyboard-configuration.postinst-9-# /usr is mounted
/var/lib/dpkg/info/keyboard-configuration.postinst-10-which () {
/var/lib/dpkg/info/keyboard-configuration.postinst-11-    local IFS

執行

$ view +14 /var/lib/dpkg/info/debianutils.postinst

或是執行

$ vi +14 /var/lib/dpkg/info/debianutils.postinst

開啟檔案後,就會將游標定位在第14行。

或是執行下面指令,觀看「/var/lib/dpkg/info/debianutils.postinst」這個檔案的「14行~17行」

$ cat /var/lib/dpkg/info/debianutils.postinst | sed -n '14,17p'

或是執行

$ sed -n '14,17p' /var/lib/dpkg/info/debianutils.postinst

顯示

if [ ! -e /usr/bin/which ]
then
    ln -s /bin/which /usr/bin/which
fi

表示「/usr/bin/which」這個檔案,是安裝「debianutils」這個套件時,

透過「/var/lib/dpkg/info/debianutils.postinst」這個「MaintainerScripts」產生的。

使用範例

執行

$ which which

顯示

/usr/bin/which

執行

$ which -a which

顯示

/usr/bin/which
/bin/which

搭配「dpkg -S」來使用

執行

$ dpkg -S $(which ls)

顯示

coreutils: /bin/ls

表示「/bin/ls」是屬於「coreutils」這個套件。

執行

$ dpkg -S $(which -a which)

顯示

dpkg-query: no path found matching pattern /usr/bin/which
debianutils: /bin/which

表示「/bin/which」是屬於「debianutils」這個套件。

註:

關於「$(command)」的用法,

請執行「$ info bash -n 'Command Substitution'」閱讀相關文件的內容,

也可以從「Bash Reference Manual / 3.5.4 Command Substitution」找到。

也可以執行「$ man bash」查詢「Command Substitution」。