Linux如何獲取命令使用幫助?

摘要:包含type、file、man、whatis命令的使用等

此爲sunny,王苗苗同學的學習筆記,持續學習,持續分享,日積月累,向着大神之路前進

命令的語法通用格式:

# COMMAND OPTIONS ARGUMENTS

#爲命令提示符,#後面跟着的必須是命令。

COMMAND:發起的命令

發起一個命令:請求內核將某個二進制程序運行爲一個進程。
發生的變化:將程序變爲進程,從靜態變爲動態(有生命週期)

命令本身是一個可執行的程序文件:二進制格式的文件,有可能會調用庫文件。
1、多數系統程序文件都存放在:/bin,/sbin,/usr/bin,/usr/sbin,/usr/local/bin,/usr/local/sbin
bin代表普通命令(普通用戶使用),sbin代表管理命令(管理員使用)
2、共享庫:/lib,/lib64,/usr/lib,/usr/lib64,/usr/local/lib,/usr/local/lib64
lib代表32爲系統使用,lib64代表64位系統使用。

命令必須遵循特定格式規範:exe(Windows),msi,ELF(Linux)
file命令可查看文件類型

[root@redhat haha]# file /usr/bin/ls
/usr/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=0x7dc964034aa8ec7327b3992e3239a0f50789a3dd, stripped
#代表/usr/bin/ls文件是一個64位的可執行文件
[root@redhat haha]# file /haha
/haha: cannot open (No such file or directory)
#查不到文件時顯示找不到
[root@redhat haha]# file ~/haha
/root/haha: directory
#haha是一個文件夾
[root@redhat haha]# file ~/haha/sunny.txt 
/root/haha/sunny.txt: ASCII text
#sunny.txt是一個ASCII 的文本文件。

注意:並非所有的命令都有一個在某目錄與之對應的可執行程序文件,例cd,cd爲linux內置命令

命令分爲兩種:內置命令和外部命令
內置命令:由shell程序自帶的命令
外部命令:獨立的可執行程序文件,文件名即其命令名。

查看命令類型:type COMMAND

# cat 是外部命令,可執行文件路徑是/usr/bin/cat,查看/usr/bin/cat 文件類型,爲可執行文件
[root@redhat bin]# type cat
cat is /usr/bin/cat
[root@redhat bin]# file /usr/bin/cat
/usr/bin/cat: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=0xd879129c87e822daf2e4886b6add48998492b53b, stripped

#cd爲內置命令,但是看到/bin目錄下也有cd文件,查看文件類型,發現並不是可執行文件,只是shell腳本
[root@redhat bin]# type cd
cd is a shell builtin
[root@redhat bin]# file /bin/cd
/bin/cd: POSIX shell script, ASCII text executable

shell程序是獨特的程序,負責解析用戶提供的命令:
環境變量PATH:從哪些路徑中查找用戶鍵入的命令所對應的命令文件

sunny思考:shell是一個解析器,輸入命令之後,shell會解析是什麼命令,有哪些參數,作用於什麼上,就是解析你輸入的命令分別屬於COMMAND OPTIONS ARGUMENTS中的哪部分。執行一個命令首先需要找到這個命令所對應的可執行文件在哪裏,像之前在path中配置jmeter路徑一樣,當你在cmd中輸入jmeter,他就會找到對應路徑下的名字爲jmeter的可執行文件,並執行,也可以試一下將jmeter.bat文件重命名爲jmeter1.bat,當你去輸入jmeter時,就會發現找不到文件了,這是因爲shell是根據文件名稱來找命令的。

[root@redhat haha]# echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin
#查找次序,從左到右
OPTIONS:指定命令運行特性

選項有兩種變現形式:
1、短選項:-c ,例如-l -d
有些命令的選項沒有“-”,如果同一命令同時使用多個短選項,多數可合併:-l -a 可合併爲-la

[root@redhat web-index]# ls -l
total 4
-rw-r--r--. 1 root root 26 Mar  8 12:59 index.html
[root@redhat web-index]# ls -a
.  ..  index.html
[root@redhat web-index]# ls -la
total 4
drwxr-xr-x. 2 root root 23 Mar  8 12:59 .
drwxr-xr-x. 3 root root 40 Mar 14 22:26 ..
-rw-r--r--. 1 root root 26 Mar  8 12:59 index.html

2、長選項:–word 例如–help,–all,長選項不能合併。
在這裏插入圖片描述

ARGUMENTS:命令的作用對象,即命令對什麼生效
#ls命令作用域var目錄,ls命令作用於/etc/httpd目錄,可以看到得到的結果是不一樣的
[root@redhat Desktop]# ls /var
account  cache  db     ftp    gdm     kerberos  local  log   nis  preserve  spool  var  yp
adm      crash  empty  games  gopher  lib       lock   mail  opt  run       tmp    www
[root@redhat Desktop]# ls /etc/httpd/
conf  conf.d  conf.modules.d  logs  modules  run

獲取命令的使用幫助

內部命令獲取使用幫助:help COMMAND ,注意help只能看到內置命令的幫助信息,非內置命令會提示沒找到。

[root@redhat Desktop]# type cd
cd is a shell builtin
[root@redhat Desktop]# help cd
cd: cd [-L|[-P [-e]]] [dir]
    Change the shell working directory.
    
    Change the current directory to DIR.  The default DIR is the value of the
    HOME shell variable.
    
    The variable CDPATH defines the search path for the directory containing
    DIR.  Alternative directory names in CDPATH are separated by a colon (:).
    A null directory name is the same as the current directory.  If DIR begins
    with a slash (/), then CDPATH is not used.
    
    If the directory is not found, and the shell option `cdable_vars' is set,
    the word is assumed to be  a variable name.  If that variable has a value,
    its value is used for DIR.
    
    Options:
        -L	force symbolic links to be followed
        -P	use the physical directory structure without following symbolic
    	links
        -e	if the -P option is supplied, and the current working directory
    	cannot be determined successfully, exit with a non-zero status
    
    The default is to follow symbolic links, as if `-L' were specified.
    
    Exit Status:
    Returns 0 if the directory is changed, and if $PWD is set successfully when
    -P is used; non-zero otherwise.
[root@redhat Desktop]# type cat
cat is /usr/bin/cat
[root@redhat Desktop]# help cat
bash: help: no help topics match `cat'.  Try `help help' or `man -k cat' or `info cat'.

外部命令獲取使用幫助
1、使用命令自帶的簡要格式的使用幫助 COMMAND --help
2、使用手冊man COMMAND,後面會介紹man 命令使用說明
3、獲取命令的在線文檔 info COMMAND
4、很多應用程序會自帶幫助文檔,文檔存放路徑/usr/share/doc/APP-VERSION
5、主流發行版的官方文檔 http://www.redhat.com/doc
6、程序官方的文檔,官方站點上的“Document”
7、搜索引擎 google
固定文件格式搜索 :keyword filetype:pdf (搜索pdf文件)
固定網站搜索: keyword site:domain.tld (只需要域名加後綴即可,不需要www)
8、書籍,最好找國外的書籍,另外此類專業書籍比較好的出版社有O’Reiley、Wrox、機械工業、電子工業、人郵、清華大學、水利水電

[root@redhat yum-3.4.3]# pwd
/usr/share/doc/yum-3.4.3
[root@redhat yum-3.4.3]# ls -l
total 564
-rw-r--r--. 1 root root    550 Jun 29  2011 AUTHORS     ##作者
-rw-r--r--. 1 root root 521349 Jun 29  2011 ChangeLog    ##修改日誌
-rw-r--r--. 1 root root  17976 Jun 29  2011 COPYING    ## 版權
-rw-r--r--. 1 root root    149 Jun 29  2011 INSTALL   ## 安裝幫助
-rw-r--r--. 1 root root  12815 Jun 29  2011 PLUGINS
-rw-r--r--. 1 root root   1078 Jun 29  2011 README  ## 程序的相關信息 
-rw-r--r--. 1 root root     42 Jun 29  2011 TODO

man命令使用

man是一個外部命令,可執行文件存放在/usr/bin/man中,man使用手冊文檔存放在/usr/share/man/

#man是一個外部命令
[root@redhat yum-3.4.3]# type man
man is hashed (/usr/bin/man)
#可執行文件存放在/usr/bin/man中
[root@redhat yum-3.4.3]# cd /usr/bin/
[root@redhat bin]# file man
man: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=0xfb91a0e8b53e6336fdd1b9c31d92b88a0beb5b84, stripped

#man使用手冊文檔存放在/usr/share/man/
[root@redhat bin]# whereis man
man: /usr/bin/man /usr/share/man /usr/share/man/man7/man.7.gz /usr/share/man/man1/man.1.gz /usr/share/man/man1p/man.1p.gz
[root@redhat bin]# cd /usr/share/man/
[root@redhat man]# ls
ca  da  en  fr  id  ja  man0p  man1p  man2   man3   man3x  man4x  man5x  man6x  man7x  man8x  man9x  overrides  pt     ru  sv  uk     zh_TW
cs  de  es  hu  it  ko  man1   man1x  man2x  man3p  man4   man5   man6   man7   man8   man9   mann 

#進入其中一個文件夾,裏面全是壓縮文件
[root@redhat man]# cd man1
[root@redhat man1]# ls
:.1.gz                                                       nm-connection-editor.1.gz
[.1.gz                                                       nm-online.1.gz
。。。省略。。。

查看man命令幫助信息,使用man man,使用man man後得到的信息如下,只截取部分,文檔中常見元素說明如下。

1、NAME:功能性說明
2、SYNOPSIS:語法格式

	[]:可選內容
	<>:必須提供的內容
	a|b|c :多選一
	...:同類內容可出現多個

3、DESCRIPTION:描述
4、EXAMPLES:使用示例
5、OPTIONS:選項
6、AUTHIR:作者
7、BUGS:報告程序bug的方式
8、SEE ALSO:參考

MAN(1)                                                     Manual pager utils                                                     MAN(1)

NAME
       man - an interface to the on-line reference manuals

SYNOPSIS
       man  [-C  file]  [-d] [-D] [--warnings[=warnings]] [-R encoding] [-L locale] [-m system[,...]] [-M path] [-S list] [-e extension]
       [-i|-I] [--regex|--wildcard] [--names-only] [-a] [-u] [--no-subpages] [-P pager] [-r prompt]  [-7]  [-E  encoding]  [--no-hyphen‐
       ation] [--no-justification] [-p string] [-t] [-T[device]] [-H[browser]] [-X[dpi]] [-Z] [[section] page ...] ...
       man -k [apropos options] regexp ...
       man -K [-w|-W] [-S list] [-i|-I] [--regex] [section] term ...
       man -f [whatis options] page ...
       man  -l  [-C  file]  [-d]  [-D]  [--warnings[=warnings]]  [-R encoding] [-L locale] [-P pager] [-r prompt] [-7] [-E encoding] [-p
       string] [-t] [-T[device]] [-H[browser]] [-X[dpi]] [-Z] file ...
       man -w|-W [-C file] [-d] [-D] page ...
       man -c [-C file] [-d] [-D] page ...
       man [-?V]

DESCRIPTION
       man is the system's manual pager. Each page argument given to man is normally the name of a program, utility  or  function.   The
       manual  page associated with each of these arguments is then found and displayed. A section, if provided, will direct man to look
       only in that section of the manual.  The default action is to search in all of the available sections,  following  a  pre-defined
       order and to show only the first page found, even if page exists in several sections.

       The table below shows the section numbers of the manual followed by the types of pages they contain.

       1   Executable programs or shell commands
       2   System calls (functions provided by the kernel)
       3   Library calls (functions within program libraries)
       4   Special files (usually found in /dev)
       5   File formats and conventions eg /etc/passwd
       6   Games
       7   Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7)
       8   System administration commands (usually only for root)
 Manual page man(1) line 1 (press h for help or q to quit)
 。。。省略。。。

使用手冊:壓縮格式的文件,有章節之分存放在/usr/share/man路徑下的man1、man2、man3等。
有些命令在多個章節都有,每章節對應不同的內容,對應章節存放內容說明如下
1:用戶命令 /usr/share/man/man1
2:系統調用 /usr/share/man/man2
3、c庫調用
4、設備文件及特殊文件
5、文件格式:(配置文件格式)
6、遊戲使用幫助
7、雜項
8、管理工具及守護進程

sunny記錄:可以把man手冊想象爲一本書,書中有不同的章節,每章有自己的主題
1、怎麼查找一個命令在哪些章節有呢?
2、怎麼知道查看的文檔屬於第幾章節?
3、書可以向前翻、向後翻、根據目錄查等操作,那這個是否也有類似操作呢?

1、怎麼查找一個命令在哪些章節存在?
答:使用whatis COMMAND查看命令存在於哪些章節及命令作用簡要說明,使用whatis CHAPTER COMMAND查看特定章節的命令使用說明。

#使用whatis man 得知man命令在第1、第7和1p都存在
[root@redhat man4]# whatis man
man (7)              - macros to format man pages
man (1)              - an interface to the on-line reference manuals
man (1p)             - display system documentation

# 查看第一章man的說明
[root@redhat man4]# man 1 man
# 查看第7章man的說明
[root@redhat man4]# man 7 man

2、怎麼知道查看的文檔屬於第幾章節?
文檔的左上角有章節提示,例如查看man man ,左上角會顯示man(1),代表正在查看第一章節。

3、書可以向前翻、向後翻、根據目錄查等操作,那這個是否也有類似操作呢?
翻屏:
空格鍵:向後翻屏
b:向前翻屏
ctrl+d:向後翻半屏
ctrl+u:向前翻半屏
回車鍵:向後翻一行
k:向前翻一行
G:跳轉至最後一行
1G:跳轉至第一行
#G:跳轉至指定行,例10G指跳轉至第10行

文件搜索:
/keyword:從文件首部向文件尾部一次查找,不區分大小寫
?keyword:從文件尾部向文件首部查找
查找到關鍵字後,有時顯示多個,那怎麼上下切換呢?
n:與查找命令方向相同
N:與查找命令方向相反

sunny:如果使用 /keyword就是從頭到尾查,當查找出關鍵字後一般會上一個下一個,這時候使用n、N就能上下切換;?keyword同理,只是查找方向相反。

退出:
q:quit

如果有人的man手冊路徑不是存放在默認路徑下,則可以使用 -M /PATH/TO/SOMEDIR:到指定目錄下查找命令手冊並打開

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