如何在Ubuntu终端获取程序的位置?

如何在Ubuntu中获取程序的位置? 例如我有Oracle ,如何获取Oracle的文件夹racine(location)?

你也可以使用whereis 。 它将显示二进制文件的路径,但也会显示一些相关文件,如文档:

whereis program

如果命令引用可执行文件,Bash和Dash具有command内置命令,可以使用-v开关向您显示命令的位置。 对于内置命令和别名,结果是不同的。 例子:

 $ command -v java /usr/bin/java $ echo $? 0 $ command -v echo echo $ command -v ls alias ls='ls -h --color=auto' $ command -v non-existing_command; echo $? 1 

另外,从Sh派生的所有shell都知道type命令,它告诉你任何命令的性质。

 $ type java java is /usr/bin/java $ type ls ls is aliased to `ls -h --color=auto' $ type echo echo is a shell builtin $ type non-existing_command bash: type: non-existing_command: not found 

如果您的shell(例如Bash)支持它,请type -a列出命令可能引用的所有内容:

 $ type -a ls ls is aliased to `ls -h --color=auto' ls is /bin/ls $ type -a echo echo is a shell builtin echo is /bin/echo $ type -a touch touch is /usr/bin/touch touch is /bin/touch 

您可以使用which来确定正在运行的二进制文件。

  • which ssh
  • which Oracle

这些是示例,将返回二进制文件的完整路径。

您也可以使用whereis来查找其他信息,但在这种情况下可能会让您感到困惑。

正如David Foerster已经提到的,你可以使用type -a来显示在活动的$PATH可以找到给定可执行文件的所有位置:

 $ type -a now now is /home/rick/bin/now now is /mnt/e/bin/now 

type -a还将标识命令是否为内置shell。 例如:

 $ type -a test test is a shell builtin test is /usr/bin/test 

type -a还将标识该命令是否为shell关键字。 例如:

 $ type -a if if is a shell keyword 

type a按照分层顺序列出程序,命令,shell内置函数和shell关键字,它们将根据$PATH环境变量调用它们。 通过将PATH=更改为不同的顺序,它会更改调用哪个版本的程序。 当您在同一台机器上拥有生产,开发和测试程序版本时,这很方便。

程序不在$ PATH中

如果程序不在您的路径中怎么办? 找到它的最快方法是使用locate命令:

 $ locate .bashrc /etc/bash.bashrc /etc/skel/.bashrc /home/rick/.bashrc /home/rick/.bashrc~ /mnt/e/.bashrc /mnt/e/Temporary Work/.bashrc /usr/share/base-files/dot.bashrc /usr/share/doc/adduser/examples/adduser.local.conf.examples/bash.bashrc /usr/share/doc/adduser/examples/adduser.local.conf.examples/skel/dot.bashrc 

我在这里受骗是因为.bashrc不是真正的可执行文件,它是一个“源”文件,包含在一个可执行文件的bash脚本中。 然而,它适当地说明。