2019-04-21 11:50:48 +08:00

27 lines
1.8 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h1 id="which">which</h1>
<p>查找并显示给定命令的绝对路径</p>
<h2 id="补充说明">补充说明</h2>
<p><strong>which命令</strong> 用于查找并显示给定命令的绝对路径环境变量PATH中保存了查找命令时需要遍历的目录。which指令会在环境变量$PATH设置的目录里查找符合条件的文件。也就是说使用which命令就可以看到某个系统命令是否存在以及执行的到底是哪一个位置的命令。</p>
<h3 id="语法">语法</h3>
<pre><code class="language-bash">which(选项)(参数)</code></pre>
<h3 id="选项">选项</h3>
<pre><code class="language-bash">-n&lt;文件名长度&gt;:制定文件名长度,指定的长度必须大于或等于所有文件中最长的文件名;
-p&lt;文件名长度&gt;:与-n参数相同但此处的&lt;文件名长度&gt;包含了文件的路径;
-w指定输出时栏位的宽度
-V显示版本信息。</code></pre>
<h3 id="参数">参数</h3>
<p>指令名:指令名列表。</p>
<h3 id="实例">实例</h3>
<p>查找文件、显示命令路径:</p>
<pre><code class="language-bash">[root@localhost ~]# which pwd
/bin/pwd
[root@localhost ~]# which adduser
/usr/sbin/adduser</code></pre>
<p>说明which是根据使用者所配置的 PATH 变量内的目录去搜寻可运行档的所以不同的 PATH 配置内容所找到的命令当然不一样的</p>
<p>用 which 去找出 cd</p>
<pre><code class="language-bash">[root@localhost ~]# which cd
cd: shell built-in command</code></pre>
<p>cd 这个常用的命令竟然找不到啊!为什么呢?这是因为 cd 是bash 内建的命令! 但是 which 默认是找 PATH 内所规范的目录,所以当然一定找不到的!</p>
<!-- Linux命令行搜索引擎https://jaywcjlove.github.io/linux-command/ -->