Linux Shell - 如何查找某些命令的目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2869100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:02:46  来源:igfitidea点击:

Shell - How to find directory of some command?

linuxshellcommand-line

提问by Gabriel L. Oliveira

I know that when you are on shell, the only commands that can be used are the ones that can be found on some directory set on PATH. Even I don't know how to see what dirs are on my PATH variable (and this is another good question that could be answered), what I'd like to know is:

我知道当你在 shell 上时,唯一可以使用的命令是可以在 PATH 上设置的某个目录中找到的命令。即使我不知道如何查看我的 PATH 变量上的目录(这是另一个可以回答的好问题),我想知道的是:

I come to shell and write:

我来shell写:

$ lshw

I want to know a command on shell that can tell me WHERE this command is located. In other words, where this "executable file" is located?

我想知道 shell 上的一个命令,它可以告诉我这个命令所在的位置。换句话说,这个“可执行文件”在哪里?

Something like:

就像是:

$ location lshw
/usr/bin

Anyone?

任何人?

采纳答案by Paused until further notice.

If you're using Bash or zsh, use this:

如果您使用 Bash 或 zsh,请使用以下命令:

type -a lshw

This will show whether the target is a builtin, a function, an alias or an external executable. If the latter, it will show each place it appears in your PATH.

这将显示目标是内置函数、函数、别名还是外部可执行文件。如果是后者,它将显示它出现在您的PATH.

bash$ type -a lshw
lshw is /usr/bin/lshw
bash$ type -a ls
ls is aliased to `ls --color=auto'
ls is /bin/ls
bash$ zsh
zsh% type -a which
which is a shell builtin
which is /usr/bin/which

In Bash, for functions type -awill also display the function definition. You can use declare -f functionnameto do the same thing (you have to use that for zsh, since type -adoesn't).

在 Bash 中,for 函数type -a也会显示函数定义。你可以declare -f functionname用来做同样的事情(你必须将它用于 zsh,因为type -a没有)。

回答by Ignacio Vazquez-Abrams

Like this:

像这样:

which lshw

To see all of the commands that match in your path:

要查看路径中匹配的所有命令:

which -a lshw 

回答by Hyman

~$ echo $PATH
/home/Hyman/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
~$ whereis lshw
lshw: /usr/bin/lshw /usr/share/man/man1/lshw.1.gz

回答by hasen

PATHis an environment variable, and can be displayed with the echo command:

PATH是一个环境变量,可以用 echo 命令显示:

echo $PATH

It's a list of paths separated by the colon character ':'

它是由冒号字符 ' :'分隔的路径列表

The whichcommand tells you which file gets executed when you run a command:

which命令会告诉您运行命令时将执行哪个文件:

which lshw

sometimes what you get is a path to a symlink; if you want to trace that link to where the actual executable lives, you can use readlinkand feed it the output of which:

有时你得到的是一个符号链接的路径;如果您想跟踪该链接到实际可执行文件所在的位置,您可以使用readlink以下输出并将其提供给它which

readlink -f $(which lshw)

The -fparameter instructs readlinkto keep following the symlink recursively.

-f参数指示readlink递归跟踪符号链接。

Here's an example from my machine:

这是我机器上的一个例子:

$ which firefox
/usr/bin/firefox

$ readlink -f $(which firefox)
/usr/lib/firefox-3.6.3/firefox.sh

回答by mpez0

The Korn shell, ksh, offers the whencebuilt-in, which identifies other shell built-ins, macros, etc. The whichcommand is more portable, however.

Korn shellksh提供了whence内置命令,它标识了其他 shell 内置which命令、宏等。但是,该命令更具可移植性。

回答by Pierz

In the TENEX C Shell, tcsh, one can list a command's location(s), or if it is a built-in command, using the wherecommand e.g.:

在 TENEX C Shell 中,tcsh,可以列出命令的位置,或者如果它是内置命令,则使用where命令,例如:

tcsh% where python
/usr/local/bin/python
/usr/bin/python

tcsh% where cd
cd is a shell built-in
/usr/bin/cd

回答by Marinos An

An alternative to type -ais command -V

一个替代方案type -acommand -V

Since most of the times I am interested in the first result only, I also pipe from head. This way the screen will not flood with code in case of a bash function.

因为大多数时候我只对第一个结果感兴趣,所以我也从头开始。这样,在 bash 函数的情况下,屏幕不会充斥着代码。

command -V lshw | head -n1