最常被問到的問題之一是如何使用包管理器(例如 apt/dnf 命令)在 Linux 上找到剛剛安裝的 Linux 命令路徑。 Windows 世界有許多新的開發人員。許多人是 Linux 的新用戶。 一些使用 WSL 的 Linux,而另一些則通過 ssh 直接與雲服務器一起工作。 讓我們看一些常用的命令來列出或搜索 Linux 命令的路徑。

什麼是 Linux 命令?

在 Windows 上,默認用戶界面是 GUI。但是,服務器端 Linux 默認為文本界面。因此,經常使用 Bash、shell、控制台、命令提示符和終端等術語。兩者都指的是文本界面。例如,在 MS Windows 上,[開始]單擊按鈕啟動應用程序。

同樣,在終端中,您輸入稱為 Linux 命令的指令。例如,您可以按 Ctrl–Alt–T 調出 Linux 終端。然後你可以通過輸入像 date 這樣的簡單命令來檢查今天的日期和時間。必須包含所有日期命令 小寫 在按下之前 輸入 運行它的關鍵:

各種Linux命令

可以在終端中輸入許多不同類型的 Linux 命令。

  1. 內部或內置外殼 命令
  2. 外部命令/可執行文件/文件
  3. 一種 外殼函數
  4. 別名
  5. 關鍵詞

如何找出它是什麼類型的 Linux 命令

必須使用 輸入命令 顯示 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

如何像專業人士一樣查找 Linux 命令的路徑

$PATH 變量是什麼?

PATH 變量包含一組目錄,其中可執行程序(ping、date、vi、docker 等)存儲在 Linux 或類 Unix 系統上。要查看您當前的路徑,請使用 迴聲命令/打印命令:

echo "$PATH"
 
# OR
#
# More human readable format 
#
echo "${PATH//:/$'n'}"

這是我看到的:

/home/vivek/google-cloud-sdk/bin
/home/vivek/.local/bin
/home/vivek/bin
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin
/usr/games
/usr/local/games
/snap/bin

要查看存儲在 /bin/ 目錄中的所有可執行文件,請運行 ls 命令,如下所示:

ls /bin/
ls -l /bin/

如何顯示有關命令的信息

使用 命令 命令 Linux命令的路徑是:

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

用於查找 Linux 命令路徑的 Linux 命令實用程序

如何查找 Linux 命令

您還可以使用 which 命令輕鬆獲取 Linux 命令的路徑。例如:
which gcc
which nano

要打印所有可能的匹配路徑,請像這樣傳遞 -a:
which -a ls
使用哪個命令獲取Linux命令的路徑

獲取 Linux 命令或手冊頁的路徑

使用 whereis 命令在磁盤上查找指定程序或命令的二進製文件、源代碼和手冊頁。語法是:

whereis command
whereis gcc
whereis docker
whereis lxc
whereis vim

您只想查看二進製文件嗎?嘗試:
whereis -b ls

為什麼不只搜索手冊頁和信息頁?簡單的:
whereis -m date
whereis -m gcc

在 Linux 上確定二進製文件、源代碼和手冊頁路徑

查找和查找命令

您還可以按名稱搜索文件。例如,要搜索名為“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 命令。 幫助命令人的訂單 或信息命令。

什麼是訂單

所有 Linux 命令都帶有手冊頁(描述用法和語法的幫助頁)。此外,還包括簡要說明。例如,whatis 命令搜索手冊頁名稱。以簡短形式顯示匹配名稱的手冊頁描述。
whatis ls
whatis clear
whatis date
whatis gcc

顯示單行手冊頁描述

對於您使用的所有外部命令 人的訂單 或者運行 info 命令將詳細文檔打印到屏幕上:
man date
man ls
man gcc
man bash
info ls
info bash
man which

Linux 中任何命令的手冊頁

對於所有 Bash 關鍵字和內置函數,
幫助命令
:

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

docker 信息路徑 linux

對於 Debian 或 Ubuntu Linux 服務器,請嘗試以下兩個命令: 使用 dpkg 命令列出 Docker 包安裝在服務器上的所有文件。
dpkg -L docker-ce
dpkg -L docker-ce | more

/.
/etc
/etc/default
/etc/default/docker
/etc/init
/etc/init/docker.conf
/etc/init.d
/etc/init.d/docker
/lib
/lib/systemd
/lib/systemd/system
/lib/systemd/system/docker.service
/lib/systemd/system/docker.socket
/usr
/usr/bin
/usr/bin/docker-init
/usr/bin/docker-proxy
/usr/bin/dockerd
/usr/share
/usr/share/doc
/usr/share/doc/docker-ce
/usr/share/doc/docker-ce/README.md
/usr/share/doc/docker-ce/changelog.Debian.gz

使用 apt 命令顯示 Docker 包信息。
apt show docker-ce

Package: docker-ce
Version: 5:19.03.13~3-0~ubuntu-bionic
Priority: optional
Section: admin
Maintainer: Docker <[email protected]>
Installed-Size: 107 MB
Depends: docker-ce-cli, containerd.io (>= 1.2.2-3), iptables, libseccomp2 (>= 2.3.0), libc6 (>= 2.8), libdevmapper1.02.1 (>= 2:1.02.97), libsystemd0
Recommends: aufs-tools, ca-certificates, cgroupfs-mount | cgroup-lite, git, pigz, xz-utils, libltdl7, apparmor
Conflicts: docker (<< 1.5~), docker-engine, docker-engine-cs, docker.io, lxc-docker, lxc-docker-virtual-package
Replaces: docker-engine
Homepage: https://www.docker.com
Download-Size: 22.5 MB
APT-Manual-Installed: yes
APT-Sources: https://download.docker.com/linux/ubuntu bionic/edge amd64 Packages
Description: Docker: the open-source application container engine
 Docker is a product for you to build, ship and run any application as a
 lightweight container
 .
 Docker containers are both hardware-agnostic and platform-agnostic. This means
 they can run anywhere, from your laptop to the largest cloud compute instance and
 everything in between - and they don't require you to use a particular
 language, framework or packaging system. That makes them great building blocks
 for deploying and scaling web apps, databases, and backend services without
 depending on a particular stack or provider.
 
N: There are 29 additional records. Please use the '-a' switch to see them.

我們希望新的 Linux 用戶和開發人員會發現這些命令很有用。快樂的編碼。