找出与给定命令对应的可执行文件的 unix 命令是什么?

hug*_*omg 23 linux unix command-line

例如,如果ls作为输入传递,它应该告诉我/bin/ls如果ls在命令行上运行,它将运行。

Jde*_*eBP 26

要使用的命令因外壳而异。

只有内置的 shell 才能正确地告诉人们 shell 将对给定的命令名称执行什么操作,因为只有内置函数才能完全了解别名、shell 函数、其他内置函数等。请记住:并非所有命令首先都对应于可执行文件。

进一步阅读


NPE*_*NPE 14

您可以which为此使用:

aix@aix:~$ which ls
/bin/ls
Run Code Online (Sandbox Code Playgroud)

它通过搜索PATH与参数名称匹配的可执行文件来工作。请注意,它不适用于 shell 别名:

aix@aix:~$ alias listdir=/bin/ls
aix@aix:~$ listdir /
bin    dev   initrd.img      lib32   media  proc  selinux  tmp  vmlinuz
...
aix@aix:~$ which listdir
aix@aix:~$
Run Code Online (Sandbox Code Playgroud)

type,但是,确实有效:

aix@aix:~$ type listdir
listdir is aliased to `/bin/ls'
Run Code Online (Sandbox Code Playgroud)

  • 小心:如果 ls 是一个函数或别名,你需要'type ls'而不是'which ls' (3认同)

Pet*_*r.O 8

which(一定)返回的可执行文件。它返回它在 $PATH 中找到的第一个匹配文件(或使用时多个类似的命名文件which -a)...实际的可执行文件可能与多个链接相距甚远。

  • which locate
    /usr/bin/locate
    `
  • file $(which locate)
    /usr/bin/locate: symbolic link to /etc/alternatives/locate'

查找实际可执行文件的命令是readlink -e,
(与 结合使用which

  • readlink -e $(which locate)
    /usr/bin/mlocate

查看所有中间链接

f="$(which locate)"             # find name in $PATH
printf "# %s\n" "$f"
while f="$(readlink "$f")" ;do  # follow links to executable
    printf "# %s\n" "$f"
done

# /usr/bin/locate
# /etc/alternatives/locate
# /usr/bin/mlocate
Run Code Online (Sandbox Code Playgroud)