{"id":2648,"date":"2016-05-08T01:00:35","date_gmt":"2016-05-07T17:00:35","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2648"},"modified":"2016-05-08T01:00:35","modified_gmt":"2016-05-07T17:00:35","slug":"linux%e4%b8%8a%e7%9a%84which%e5%92%8cwhereis%e5%91%bd%e4%bb%a4","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2648.html","title":{"rendered":"Linux\u4e0a\u7684which\u548cwhereis\u547d\u4ee4"},"content":{"rendered":"<p>=Start=<\/p>\n<p>\u7ecf\u5e38\u7528Linux\u7cfb\u7edf\u7684\u4eba\u5e94\u8be5\u90fd\u63a5\u89e6\u8fc7which\u548cwhereis\u547d\u4ee4\uff0c\u5e76\u4f1a\u7528\u8fd9\u4e9b\u547d\u4ee4\u8fdb\u884c\u6587\u4ef6\u67e5\u627e\/\u5b9a\u4f4d\u3002\u4f46\u5176\u4e2d\u7684\u7528\u6cd5\u548c\u533a\u522b\u4e00\u822c\u4e0d\u4f1a\u6df1\u7a76\uff0c\u8fd9\u91cc\u6211\u5148\u7b80\u5355\u8bf4\u4e00\u4e0b\uff0c\u5f85\u4e4b\u540e\u9605\u8bfb\u4e86which\u548cwhereis\u547d\u4ee4\u7684\u6e90\u7801\u4e4b\u540e\u518d\u6765\u7ec6\u8bf4\u3002<\/p>\n<p>####<\/p>\n<blockquote><p>whereis<\/p>\n<p style=\"padding-left: 30px;\">locate the binary, source, and manual page files for a command<\/p>\n<p>BUGS<\/p>\n<p style=\"padding-left: 30px;\">Since whereis uses chdir(2V) to run faster, <span style=\"color: #0000ff;\"><strong>pathnames given with the -M, -S, or -B must be full; that is, they must begin with a \u2018\/\u2019.<\/strong><\/span><br \/>\n<strong><span style=\"color: #ff0000;\">whereis has a hard-coded path<\/span>, so may not always find what you\u2019re looking for.<\/strong><\/p>\n<\/blockquote>\n<p>####<\/p>\n<blockquote><p>which<\/p>\n<p style=\"padding-left: 30px;\">shows the full path of (shell) commands.<\/p>\n<p>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 <span style=\"color: #ff0000;\"><strong>in the directories listed in the environment variable PATH<\/strong><\/span> using the same algorithm as bash(1).<\/p><\/blockquote>\n<p>####<\/p>\n<pre class=\"lang:default decode:true\">$ whereis identify    # whereis\u547d\u4ee4\u4f1a\u5728\u7279\u5b9a\u76ee\u5f55\uff08\u4f46\u7279\u5b9a\u76ee\u5f55\u662f\u54ea\u4e9b\uff1f\uff09\u4e2d\u67e5\u627e\u7b26\u5408\u6761\u4ef6\u7684\u6587\u4ef6\n$ whereis -B \/usr\/local identify    # \u5728\u7279\u5b9a\u76ee\u5f55\u4e2d\u67e5\u627e\u7b26\u5408\u6761\u4ef6\u7684\u6587\u4ef6(\u6ca1\u6709\u7528...)\n$ whereis -B \/usr\/local\/ImageMagick-6.9.3-10\/bin identify    # \u6ca1\u6709\u7528...\n$ which identify      # which\u547d\u4ee4\u4f1a\u5728 $PATH \u4e2d\u67e5\u627e\uff0c\u627e\u5230\u4e00\u4e2a\u5c31\u9000\u51fa<\/pre>\n<p>\u4e0b\u9762\u662f\u6d4b\u8bd5which\u548cwhereis\u547d\u4ee4\u529f\u80fd\u533a\u522b\u7684\u811a\u672c\uff1a<\/p>\n<pre class=\"lang:default decode:true \">#!\/bin\/bash\n# set -x\n\necho -e \"\\$PATH = $PATH\"\necho -e \"[whereis identify]\\t$(whereis identify)\"\necho -e \"[which identify]\\t$(which identify)\"\necho -e \"\\n\"\n\nexport PATH=\/usr\/local\/ImageMagick-6.9.2-10\/bin:\/usr\/local\/bin:\/bin:\/usr\/bin:\/sbin:\/usr\/sbin\/:\/usr\/local\/sbin\necho -e \"\\$PATH = $PATH\"\necho -e \"[whereis identify]\\t$(whereis identify)\"\necho -e \"[which identify]\\t$(which identify)\"\necho -e \"\\n\"\n\nexport PATH=\/usr\/local\/ImageMagick-6.9.3-10\/bin:\/usr\/local\/bin:\/bin:\/usr\/bin:\/sbin:\/usr\/sbin\/:\/usr\/local\/sbin\necho -e \"\\$PATH = $PATH\"\necho -e \"[whereis identify]\\t$(whereis identify)\"\necho -e \"[which identify]\\t$(which identify)\"<\/pre>\n<p>\u7136\u540e\u518d\u653e\u4e00\u4e2a\u7528Python\u5b9e\u73b0\u7684\u300cwhich\u547d\u4ee4\u300d\uff1a<\/p>\n<pre class=\"lang:default decode:true \">#!\/usr\/bin\/env python\n# coding=utf-8\n\n# A minimal version of the UNIX which utility, in Python.\n# Author: Vasudev Ram - www.dancingbison.com\n\nfrom __future__ import print_function\nimport sys\nimport os\nimport os.path\nimport stat\n\ndef usage():\n    sys.stderr.write(\"Usage: python which.py name\\n\")\n    sys.stderr.write(\"or: which.py name\\n\")\n\ndef which(name):\n    found = 0\n    for path in os.getenv(\"PATH\").split(os.path.pathsep):\n        full_path = path + os.sep + name\n        if os.path.exists(full_path):\n            \"\"\"\n            if os.stat(full_path).st_mode &amp; stat.S_IXUSR:\n                found = 1\n                print(full_path)\n            \"\"\"\n            found = 1\n            print(full_path)\n    # Return a UNIX-style exit code so it can be checked by calling scripts.\n    # Programming shortcut to toggle the value of found: 1 =&gt; 0, 0 =&gt; 1.\n    sys.exit(1 - found)\n\ndef main():\n    if len(sys.argv) != 2:\n        usage()\n        sys.exit(1)\n    which(sys.argv[1])\n\nif \"__main__\" == __name__:\n    main()<\/pre>\n<h6>\u66f4\u591a\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<p><a href=\"http:\/\/www.blogjava.net\/tinysun\/archive\/2010\/09\/30\/333460.html\">linux\u4e0bwhich\u3001whereis\u3001locate\u3001find \u547d\u4ee4\u7684\u533a\u522b<\/a><\/p>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7ecf\u5e38\u7528Linux\u7cfb\u7edf\u7684\u4eba\u5e94\u8be5\u90fd\u63a5\u89e6\u8fc7which\u548cwhereis\u547d\u4ee4\uff0c\u5e76\u4f1a\u7528\u8fd9\u4e9b\u547d\u4ee4\u8fdb\u884c\u6587\u4ef6\u67e5\u627e [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11],"tags":[30,593,594],"class_list":["post-2648","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","tag-linux","tag-whereis","tag-which"],"views":3213,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2648","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2648"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2648\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}