如何找到命令行操作的来源?

Whi*_*uit 18 command-line documentation

假设我能够abc在命令行中输入并且它会运行(所以 shell 不会说“abc: command not found”)。

我怎样才能知道abc是什么或做什么?是脚本吗?程序?别名?

ste*_*ver 26

您可以使用type命令,例如。type abc. 例如,在 bash shell 中:

$ type while cd ls gcc apt
while is a shell keyword
cd is a shell builtin
ls is aliased to `ls --color=auto'
gcc is /usr/bin/gcc
apt is hashed (/usr/bin/apt)
Run Code Online (Sandbox Code Playgroud)

普通type命令只显示第一个结果。如果abc在您的上的不同位置有多个版本PATH,或者abc作为 shell 关键字和外部可执行文件提供,或者要查看命令的别名和非别名版本,您可以使用type -a列出所有这些,例如:

$ type -a time
time is a shell keyword
time is /usr/bin/time

$ type -a ls
ls is aliased to `ls --color=auto'
ls is /bin/ls

$ type -a datamash
datamash is /usr/local/bin/datamash
datamash is /usr/bin/datamash
Run Code Online (Sandbox Code Playgroud)

在 bash 中,type它本身是一个内置的 shell。其他 shell,例如zshandkshdash/bin/sh在 Ubuntu 中提供)提供类似的功能(尽管dash目前不提供type -a)。在 中tcsh,最接近的等效项是内置which命令 - 不要与外部which命令混淆- 请参阅为什么不使用“which”?那用什么?

对于标识为外部程序的命令(即具有路径,例如/usr/bin/gcc),您可以使用该file命令找出程序类型:

$ file /bin/ls /usr/bin/gcc /usr/sbin/adduser
/bin/ls:           ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=2f15ad836be3339dec0e2e6a3c637e08e48aacbd, for GNU/Linux 3.2.0, stripped
/usr/bin/gcc:      symbolic link to gcc-9
/usr/sbin/adduser: Perl script text executable
Run Code Online (Sandbox Code Playgroud)


N0r*_*ert 6

对于已安装的命令,请使用 steeldriver 的答案。

对于未安装的命令,请阅读以下内容。

有一个名为command-not-found. 它的目的是

建议在交互式 bash 会话中安装软件包

一旦安装,这个包就会完成它的工作,并建议你安装具有已知可执行名称的 deb-package。


如果您知道可执行文件名称和/或其文件路径的一部分,那么您可以使用以下两个选项之一找到它的包: