如何获取 Ubuntu 中程序的位置?例如,我有Oracle
,如何获取 的文件夹 racine(位置)Oracle
?
答案1
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
答案2
你也可以使用whereis
。它将显示二进制文件的路径以及一些相关文件,如文档:
whereis program
答案3
您可以使用它which
来确定正在运行哪个二进制文件。
which ssh
which Oracle
这些是示例,将返回二进制文件的完整路径。
您也可以使用它whereis
来查找其他信息,但在这种情况下它可能会让您感到困惑。
答案4
Shell脚本what-about
我有一个 bash shellscript,它尝试
- 找到程序所在的位置,
- 找到相应的包并
- 告诉它是什么类型的程序
也许使用名称what-about
,使其可执行,并将其放入 PATH,
#!/bin/bash
LANG=C
inversvid="\0033[7m"
resetvid="\0033[0m"
if [ $# -ne 1 ]
then
echo "Usage: ${0##*/} <program-name>"
echo "Will try to find corresponding package"
echo "and tell what kind of program it is"
exit 1
fi
command="$1"
str=;for ((i=1;i<=$(tput cols);i++)) do str="-$str";done
tmp="$command"
first=true
curdir="$(pwd)"
tmq=$(which "$command")
tdr="${tmq%/*}"
tex="${tmq##*/}"
if test -d "$tdr"; then cd "$tdr"; fi
#echo "cwd='$(pwd)' ################# d"
while $first || [ "${tmp:0:1}" == "l" ]
do
first=false
tmp=${tmp##*\ }
tmq="$tmp"
tmp=$(ls -l "$(which "$tmp")" 2>/dev/null)
tdr="${tmq%/*}"
tex="${tmq##*/}"
if test -d "$tdr"; then cd "$tdr"; fi
# echo "cwd='$(pwd)' ################# d"
if [ "$tmp" == "" ]
then
tmp=$(ls -l "$tex" 2>/dev/null)
tmp=${tmp##*\ }
if [ "$tmp" == "" ]
then
echo "$command is not in PATH"
# package=$(bash -ic "$command -v 2>&1")
# echo "package=$package XXXXX 0"
bash -ic "alias '$command' > /dev/null 2>&1" > /dev/null 2>&1
if [ $? -ne 0 ]
then
echo 'looking for package ...'
package=$(bash -ic "$command -v 2>&1"| sed -e '0,/with:/d'| grep -v '^$')
else
echo 'alias, hence not looking for package'
fi
# echo "package=$package XXXXX 1"
if [ "$package" != "" ]
then
echo "$str"
echo "package: [to get command '$1']"
echo -e "${inversvid}${package}${resetvid}"
fi
else
echo "$tmp"
fi
else
echo "$tmp"
fi
done
tmp=${tmp##*\ }
if [ "$tmp" != "" ]
then
echo "$str"
program="$tex"
program="$(pwd)/$tex"
file "$program"
if [ "$program" == "/usr/bin/snap" ]
then
echo "$str"
echo "/usr/bin/snap run $command # run $command "
sprog=$(find /snap/"$command" -type f -iname "$command" \
-exec file {} \; 2>/dev/null | sort | tail -n1)
echo -e "${inversvid}file: $sprog$resetvid"
echo "/usr/bin/snap list $command # list $command"
slist="$(/usr/bin/snap list "$command")"
echo -e "${inversvid}$slist$resetvid"
else
package=$(dpkg -S "$program")
if [ "$package" == "" ]
then
package=$(dpkg -S "$tex" | grep -e " /bin/$tex$" -e " /sbin/$tex$")
if [ "$package" != "" ]
then
ls -l /bin /sbin
fi
fi
if [ "$package" != "" ]
then
echo "$str"
echo " package: /path/program [for command '$1']"
echo -e "${inversvid} $package ${resetvid}"
fi
fi
fi
echo "$str"
#alias=$(grep "alias $command=" "$HOME/.bashrc")
alias=$(bash -ic "alias '$command' 2>/dev/null"| grep "$command")
if [ "$alias" != "" ]
then
echo "$alias"
fi
type=$(type "$command" 2>/dev/null)
if [ "$type" != "" ]
then
echo "type: $type"
elif [ "$alias" == "" ]
then
echo "type: $command: not found"
fi
cd "$curdir"
演示示例
有时有两种选择,例如,对于echo
,既有单独的编译程序,也有 shell 内置命令。除非您使用单独程序的完整路径,否则 shell 内置命令将获得优先级并被使用,
echo
在 18.04.6 LTS 中
$ what-about echo
-rwxr-xr-x 1 root root 35000 jan 18 2018 /bin/echo
----------------------------------------------------------------------------------
/bin/echo: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically
linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0,
BuildID[sha1]=057373f1356c861e0ec5b52c72804c86c6842cd5, stripped
----------------------------------------------------------------------------------
package: /path/program [for command 'echo']
coreutils: /bin/echo
----------------------------------------------------------------------------------
type: echo is a shell builtin
echo
在 22.04 LTS 中
$ what-about echo
-rwxr-xr-x 1 root root 35120 Feb 7 17:03 /usr/bin/echo
----------------------------------------------------------------------------------
/usr/bin/echo: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically
linked, interpreter /lib64/ld-linux-x86-64.so.2,
BuildID[sha1]=b56baf290970e98b8056b1a8114a4404b8186689, for GNU/Linux 3.2.0, stripped
dpkg-query: no path found matching pattern /usr/bin/echo
lrwxrwxrwx 1 root root 7 Apr 27 10:02 /bin -> usr/bin
lrwxrwxrwx 1 root root 8 Apr 27 10:02 /sbin -> usr/sbin
----------------------------------------------------------------------------------
package: /path/program [for command 'echo']
coreutils: /bin/echo
----------------------------------------------------------------------------------
type: echo is a shell builtin
rename
在 18.04.6 LTS 中
有时命令链接到程序,可能是隐藏的,例如rename
我使用的版本,
$ what-about rename
lrwxrwxrwx 1 root root 24 maj 12 2018 /usr/bin/rename -> /etc/alternatives/rename
lrwxrwxrwx 1 root root 20 maj 12 2018 /etc/alternatives/rename -> /usr/bin/file-rename
-rwxr-xr-x 1 root root 3085 feb 20 2018 /usr/bin/file-rename
----------------------------------------------------------------------------------
/usr/bin/file-rename: Perl script text executable
----------------------------------------------------------------------------------
package: /path/program [for command 'rename']
rename: /usr/bin/file-rename
----------------------------------------------------------------------------------
type: rename is /usr/bin/rename
rm
在 18.04.6 LTS 中
rm
为了避免错误,我为 指定了一个别名,并且该别名的优先级高于 中的程序PATH
。您可以在 前面加上反斜杠 ,\rm
以跳过别名并直接运行程序。(请记住,别名仅适用于特定用户,而不适用于sudo
其他用户,除非他们定义了类似的别名。)
$ what-about rm
-rwxr-xr-x 1 root root 63704 jan 18 2018 /bin/rm
---------------------------------------------------------------------------
/bin/rm: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV),
dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for
GNU/Linux 3.2.0, uildID[sha1]=864c9bbef111ce358b3452cf7ea457d292ba93f0,
stripped
---------------------------------------------------------------------------
package: /path/program [for command 'rm']
coreutils: /bin/rm
---------------------------------------------------------------------------
alias rm='rm -i'
type: rm is /bin/rm
firefox
在 18.04.6 LTS 中
$ what-about firefox
lrwxrwxrwx 1 root root 25 jun 8 23:23 /usr/bin/firefox -> ../lib/firefox/firefox.sh
-rwxr-xr-x 1 root root 2667 jun 8 23:23 ../lib/firefox/firefox.sh
----------------------------------------------------------------------------------
/usr/lib/firefox/firefox.sh: POSIX shell script, ASCII text executable
----------------------------------------------------------------------------------
package: /path/program [for command 'firefox']
firefox: /usr/lib/firefox/firefox.sh
----------------------------------------------------------------------------------
type: firefox is /usr/bin/firefox
firefox
在 22.04 LTS 中
$ what-about firefox
lrwxrwxrwx 1 root root 13 Jun 14 10:17 /snap/bin/firefox -> /usr/bin/snap
-rwxr-xr-x 1 root root 15815640 Apr 21 10:50 /usr/bin/snap
----------------------------------------------------------------------------------
/usr/bin/snap: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically
linked, interpreter /lib64/ld-linux-x86-64.so.2, Go
BuildID=_CKxPPsUzOWnuqfKlk4n/eiKeSmbgVb16CaXm3O1l/-ofceI8uzhaCgwaTtgJt/YygjnGD2ASByLbaEMRR4,
stripped
----------------------------------------------------------------------------------
/usr/bin/snap run firefox # run firefox
/usr/bin/snap list firefox # list firefox
Name Version Rev Tracking Publisher Notes
firefox 101.0.1-1 1443 latest/stable/… mozilla** -
----------------------------------------------------------------------------------
type: firefox is /snap/bin/firefox