遇到陌生的 Linux 命令怎麼辦

1、whatis

whatis 命令用於描述一個命令執行什麼功能。

whatis <cmd>

示例:

$ whatis bash
bash (1)             - GNU Bourne-Again SHell

2、whereis

whereis 命令用於查找二進制文件、源文件和 man 手冊頁的路徑。

whereis <cmd>

示例:

$ whereis bash
bash: /bin/bash /etc/bash.bashrc /usr/share/man/man1/bash.1.gz

3、which

which 命令用於查看並顯示指定命令的絕對路徑,可用此命令檢查系統是否存在某個命令,以及該命令所在的位置。

which <cmd>

示例:

$ which bash
/bin/bash

4、type

type 命令用於查看命令的類型,比如命令是內建的還是外部的。

type <cmd>

示例

$ type bash
bash 是 /bin/bash

$ type cd
cd 是 shell 內建

$ type ls
ls 是 `ls --color=auto' 的別名

5、command

command 命令用於執行一個簡單命令或者顯示命令的相關信息。

command [-pVv] <cmd>

示例:

$ command -v bash
/bin/bash

6、help 和 --help

  • help 命令只能用於內部命令,不能用於外部命令。
  • --help 用於外部命令,但並非所有命令都支持。
help <cmd>
<cmd> --help

7、man

通常來說,man 得到的信息比 help 要多,且沒有內建和外部命令的區分。因爲 man 工具是顯示系統手冊頁中的內容,也就是一本電子版的字典,其內容大多數是對命令的解釋,另外還有一些相關的描述。

man [1-9] <cmd>

比如執行 man bash,會顯示如下內容:

BASH(1)                 General Commands Manual                  BASH(1)

NAME
       bash - GNU Bourne-Again SHell

SYNOPSIS
       bash [options] [command_string | file]

......

第一行的 “BASH(1)”,“BASH” 表示手冊名稱,“(1)” 表示該手冊位於第一章節。而 man 手冊一共有下面 9 個章節,不同章節包含不同類型的內容。

  1. Standard commands,可執行程序或 shell 命令;
  2. System calls,系統調用(內核提供的函數);
  3. Library functions,庫調用(程序庫中的函數);
  4. Special Devices,特殊文件(通常位於 /dev);
  5. File formats,文件格式和規範,如 /etc/passwd;
  6. Games and toys,遊戲和娛樂;
  7. Miscellaneous,雜項(包括宏包和規範,如 man(7)groff(7));
  8. Adminstrative commands,系統管理命令(通常只針對 root 用戶);
  9. 內核例程。

注意: 如果指定了章節,man 將只在手冊的指定章節搜索。默認將按預定的順序查找所有可用的章節,默認查找順序是 “1 n l 8 3 2 3posix 3pm 3perl 5 4 9 6 7”(除非被 /etc/manpath.config 中的 SECTION 指令覆蓋)。並且 man 只顯示找到的第一個頁,即使多個章節中都有這個頁面。

比如,mkdir 命令,執行 man mkdir 時顯示如下內容:

MKDIR(1)                   User Commands                   MKDIR(1)

NAME
       mkdir - make directories

SYNOPSIS
       mkdir [OPTION]... DIRECTORY...

......

執行 man 2 mkdir 時顯示如下內容:

MKDIR(2)                  Linux Programmer's Manual                 MKDIR(2)

NAME
       mkdir, mkdirat - create a directory

SYNOPSIS
       #include <sys/stat.h>
       #include <sys/types.h>

       int mkdir(const char *pathname, mode_t mode);
......

8、info

man 和 info 就像兩個集合,它們有一個交集部分,但與 man 相比,info 可顯示更完整的 GNU 工具信息。這是因爲,info 來自 GNU 項目,是 GNU 的超文本幫助系統,因此能夠更完整地顯示出 GNU 信息。

info <cmd>

比如,執行 info info,顯示如下內容:

File: dir,      Node: Top,      This is the top of the INFO tree.

This is the Info main menu (aka directory node).
A few useful Info commands:

  'q' quits;
  '?' lists all Info commands;
  'h' starts the Info tutorial;
  'mTexinfo RET' visits the Texinfo manual, etc.

* Menu:

Archiving
* Shar utilities: (sharutils).  Shell archiver, uuencode/uudecode.

Basics
* Common options: (coreutils)Common options.
* Coreutils: (coreutils).       Core GNU (file, text, shell) utilities.
......

與 man 頁面只用一頁來顯示內容的方式不同,info 頁面把內容組織成多個區段(稱爲節點),每個區段也可能包含子區段(稱爲子節點)。因此在瀏覽 info 頁面內容時,還需要學會如何在節點和子節點之間切換。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章