本文最后更新于2016年5月8日,已超过 1 年没有更新,如果文章内容失效,还请反馈给我,谢谢!





locate the binary, source, and manual page files for a command


Since whereis uses chdir(2V) to run faster, pathnames given with the -M, -S, or -B must be full; that is, they must begin with a ‘/’.
whereis has a hard-coded path, so may not always find what you’re looking for.



shows the full path of (shell) commands.

Which takes one or more arguments. For each of its arguments it prints to stdout the full path of the executables that would have been executed when this argument had been entered at the shell prompt. It does this by searching for an executable or script in the directories listed in the environment variable PATH using the same algorithm as bash(1).


$ whereis identify    # whereis命令会在特定目录(但特定目录是哪些?)中查找符合条件的文件
$ whereis -B /usr/local identify    # 在特定目录中查找符合条件的文件(没有用...)
$ whereis -B /usr/local/ImageMagick-6.9.3-10/bin identify    # 没有用...
$ which identify      # which命令会在 $PATH 中查找,找到一个就退出


# set -x

echo -e "\$PATH = $PATH"
echo -e "[whereis identify]\t$(whereis identify)"
echo -e "[which identify]\t$(which identify)"
echo -e "\n"

export PATH=/usr/local/ImageMagick-6.9.2-10/bin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin/:/usr/local/sbin
echo -e "\$PATH = $PATH"
echo -e "[whereis identify]\t$(whereis identify)"
echo -e "[which identify]\t$(which identify)"
echo -e "\n"

export PATH=/usr/local/ImageMagick-6.9.3-10/bin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin/:/usr/local/sbin
echo -e "\$PATH = $PATH"
echo -e "[whereis identify]\t$(whereis identify)"
echo -e "[which identify]\t$(which identify)"


#!/usr/bin/env python
# coding=utf-8

# A minimal version of the UNIX which utility, in Python.
# Author: Vasudev Ram -

from __future__ import print_function
import sys
import os
import os.path
import stat

def usage():
    sys.stderr.write("Usage: python name\n")
    sys.stderr.write("or: name\n")

def which(name):
    found = 0
    for path in os.getenv("PATH").split(os.path.pathsep):
        full_path = path + os.sep + name
        if os.path.exists(full_path):
            if os.stat(full_path).st_mode & stat.S_IXUSR:
                found = 1
            found = 1
    # Return a UNIX-style exit code so it can be checked by calling scripts.
    # Programming shortcut to toggle the value of found: 1 => 0, 0 => 1.
    sys.exit(1 - found)

def main():
    if len(sys.argv) != 2:

if "__main__" == __name__:

linux下which、whereis、locate、find 命令的区别


声明: 除非注明,ixyzero.com文章均为原创,转载请以链接形式标明本文地址,谢谢!

3 thoughts on “Linux上的which和whereis命令”

  1. 只要是可执行文件,which都会进行查找,即便该可执行文件内容为空:
    export PATH=~/bin:$PATH
    mkdir -p ~/bin
    touch ~/bin/hiexec
    which hiexec

    chmod u+x bin/hiexec
    which hiexec

  2. 漏洞预警 | GhostScript 沙箱绕过(命令执行)漏洞
    8 月 21 号,Tavis Ormandy 通过公开邮件列表,再次指出 GhostScript 的安全沙箱可以被绕过,通过构造恶意的图片内容,将可以造成命令执行、文件读取、文件删除等漏洞。

    GhostScript 被许多图片处理库所使用,如 ImageMagick、Python PIL 等,默认情况下这些库会根据图片的内容将其分发给不同的处理方法,其中就包括 GhostScript。

    ImageMagick 是一款广泛使用的图像处理软件,有相当多的网站使用它来进行图像处理。它被许多编程语言所支持,包括 Perl,C++,PHP,Python 和 Ruby 等,并被部署在数以百万计的网站,博客,社交媒体平台和流行的内容管理系统(CMS),例如 WordPress 和 Drupal。
    Python PIL 是 Python 语言中处理图片的第三方模块。


电子邮件地址不会被公开。 必填项已用*标注