5

如何像专业人士一样找到Linux命令的所有信息

 2 years ago
source link: https://www.myfreax.com/finding-a-path-of-a-linux-command-like-a-pro/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

新手遇到的最常见问题之一是如何使用包管理器(例如apt/dnf命令)找到刚刚安装在Linux上的Linux命令路径。有许多来自Windows世界的开发人员。其中许多是首次使用 Linux的用户。有些使用WSL的Linux,有些则直接通过ssh处理云服务器。让我们看看一些常见的命令来列出或查找Linux命令的路径。

什么是Linux命令?

在Windows上,默认用户界面是 GUI。但是,服务器端的 Linux 默认设置为文本界面。因此经常使用诸如 Bashshell、控制台、命令提示符、终端和许多其他名称之类的术语。它们都指的是文本界面。

同样,在终端上,您键入称为 Linux 命令的指令。例如,您可以按Ctrl– Alt–T启动 Linux 终端。然后您可以键入一个简单的命令,例如date命令查看今天的日期和时间。在按Enter键运行它

date
2021年 11月 05日 星期五 15:47:49 CST

date、pwd、hello 和ping都是Linux 命令。

不同类型的Linux命令

您在终端中键入的 Linux 命令有不同的类型:

  1. 内部或内置shell命令 builtin
  2. 外部命令/可执行文件/文件 file
  3. 一个shell函数 function
  4. 一个别名 alias
  5. 一个关键字 keyword

如何找出Linux命令类型

我们需要使用type命令来显示Linux命令的路径。它还会判断该命令是内置shell、别名、函数还是外部命令。语法是

type command
type -t command
type -a command

例如,让我们找出pwd命令类型:

type pwd
type date
type hello
type ping
# Display a single word which is one of 'alias', 'keyword', 'function', 'builtin', 
# 'file or '' , if command is an alias, shell reserved word, shell function, shell builtin, 
# disk file, or not found, respectively
type -t ping
type -t if
type -t vi
type -t nano
 
# The '-a' option shows all locations containing an executable named ping
type -a ping

变量$PATH是什么?

PATH变量包含一组目录,可执行程序(例如 ping、date、vi、docker 等)存储在 Linux或类Unix系统上。要查看当前PATH,请使用echo命令/printf 命令

echo  " $PATH " 
 
# OR 
# 
# 更友好的阅读格式
# 
echo  " ${PATH//:/$'\n'} "

这是我看到的:

 /media/common/linux/gradle-7.2/bin$'
'/home/myfreax/.pulumi/bin$'
'/media/common/linux/go/bin/bin$'
'/media/common/linux/go/bin$'
'/media/common/linux/Android/sdk/cmdline-tools/latest/bin$'
'/media/common/linux/node-v14.17.3-linux-x64/bin$'
'/media/common/yarn/bin$'
'/media/common/linux/flluter/flutter/bin$'
'/home/myfreax/.deno/bin$'
'/home/myfreax/.cargo/bin$'
'/usr/local/sbin$'
'/usr/local/bin$'
'/usr/sbin$'
'/usr/bin$'
'/sbin$'
'/bin$'
'/usr/games$'
'/usr/local/games$'
'/snap/bin$'
'/home/myfreax/.pub-cache/bin$'
'/home/myfreax/.pulumi/bin 

要查看存储在/bin/目录中的所有可执行文件,请运行 ls 命令,如下所示:

ls  / bin / 
ls  -l  / bin /

如何显示有关命令的信息

使用command命令如下列出Linux命令的路径:

command -v date
command -v pwd
command -v ping
command -v docker

如果你要shell脚本检查一个命令是否存在,请使用command检查。

如何定位Linux命令

我们还可以使用which命令轻松获取Linux命令的路径。例如: 要打印所有可能的匹配路径,请按如下方式传递:

which gcc
which nano

which -a ls

获取Linux命令或手册页的路径

使用whereis命令在磁盘上查找指定程序或命令的二进制文件、源代码和手册页。语法是:

whereis command
whereis gcc
whereis docker
whereis lxc
whereis vim

如何只搜索手册和信息页面?

whereis -m date
whereis -m gcc

查找和定位命令

我们还可以按名称查找文件。例如,搜索名为 'date' 类型的文件:

locate -b '\date'
## OR ##
find / -name "date" -ls
# sudo for all files 
sudo find / -name "date" -ls

定位命令的输出:

/snap/core/11316/bin/date
/snap/core/11420/bin/date
/snap/core18/2066/bin/date
/snap/core18/2074/bin/date
/snap/core20/1026/usr/bin/date
/snap/core20/1081/usr/bin/date
/usr/bin/date
/usr/lib/byobu/date
输出

显示有关Linux命令的帮助

我们可以使用whatis命令、help命令和man命令或info命令。

每个 Linux 命令都带有一个手册页(描述用法和语法的帮助页)。此外,它还包含一个简短的描述。例如,whatis命令搜索手册页名称。它以简短形式显示任何匹配名称的手册页描述:

whatis ls
whatis clear
whatis date
whatis gcc

对于所有外部命令,我们使用如下man命令或info命令获取详细手册:

man date
man ls
man gcc
man bash
info ls
info bash
man which

Linux 上的示例手册页

对于所有Bash关键字和内置函数,我们使用help 命令:

help if
help exit
help logout
help type
help command

一个小练习

Linux新用户的主要障碍是定位命令。但是在这个简单页面的帮助下,您现在可以找到命令路径,甚至可以使用手册页获得有关它们的帮助。假设您在 Ubuntu 服务器上安装了Docker。然后您可以使用以下命令查找路径并获取帮助:

whatis docker
whereis docker
type -a docker
which docker
find / -iname "docker"
locate -b '\docker'
man docker

我希望新的 Linux 用户和开发人员会发现这些命令很有用。快乐编码。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK