找回密码
 立即注册
搜索
查看: 370|回复: 0

Linux 命令学习之路:man

[复制链接]

266

主题

0

回帖

1119

积分

管理员

积分
1119
发表于 2024-1-7 00:22:26 | 显示全部楼层 |阅读模式

我认为学习 Linux 命令,第一个要学的就是 man 命令。

man 命令是 Linux 系统中用于查看系统文档的重要命令,可以用来查看命令的说明、系统调用、库函数等帮助信息。man 命令的全称是 "manual",它是单词 "manual" 的缩写,意思是使用手册。也许有人一开始的时候认为它是“人”的意思,我认为也说得过去,服务于人,人性化,不都是说一个好的系统首先应该是以人为本的吗。

man 命令格式:

man [OPTION...] [SECTION] PAGE...

man 命令常用参数:
-h   显示帮助信息,我认为这是学习每一个命令时首先要会用的参数
-a   显示所有匹配项
-d   输出调试信息,包括查找时的搜索路径等,例如:

$ man -d ls
ruid=1000, euid=1000
rgid=1000, egid=1000
++priv_drop_count = 1
From the config file /etc/manpath.config:
  Mandatory mandir `/usr/man'.
  Mandatory mandir `/usr/share/man'.
  Mandatory mandir `/usr/local/share/man'.
  Path `/bin' mapped to mandir `/usr/share/man'.
  Path `/usr/bin' mapped to mandir `/usr/share/man'.
  Path `/sbin' mapped to mandir `/usr/share/man'.
  Path `/usr/sbin' mapped to mandir `/usr/share/man'.
  Path `/usr/local/bin' mapped to mandir `/usr/local/man'.
  Path `/usr/local/bin' mapped to mandir `/usr/local/share/man'.
  Path `/usr/local/sbin' mapped to mandir `/usr/local/man'.
  Path `/usr/local/sbin' mapped to mandir `/usr/local/share/man'.
  Path `/usr/X11R6/bin' mapped to mandir `/usr/X11R6/man'.
  Path `/usr/bin/X11' mapped to mandir `/usr/X11R6/man'.
  Path `/usr/games' mapped to mandir `/usr/share/man'.
  Path `/opt/bin' mapped to mandir `/opt/man'.
  Path `/opt/sbin' mapped to mandir `/opt/man'.
  Global mandir `/usr/man', catdir `/var/cache/man/fsstnd'.
  Global mandir `/usr/share/man', catdir `/var/cache/man'.
  Global mandir `/usr/local/man', catdir `/var/cache/man/oldlocal'.
  Global mandir `/usr/local/share/man', catdir `/var/cache/man/local'.
  Global mandir `/usr/X11R6/man', catdir `/var/cache/man/X11R6'.
  Global mandir `/opt/man', catdir `/var/cache/man/opt'.
  Global mandir `/snap/man', catdir `/var/cache/man/snap'.
  Added sections: `1', `n', `l', `8', `3', `0', `2', `3posix', `3pm', `3perl', `3am', `5', `4', `9', `6', `7'.
is a tty
using pager as pager
path directory /usr/local/sbin is in the config file
  adding /usr/local/man to manpath
  adding /usr/local/share/man to manpath
path directory /usr/local/bin is in the config file
...

-f  等价于 whatis 命令,用于显示给定关键字的简短描述信息。使用 -f 参数时,man 命令会在关键字数据库中搜索与给定关键字相关的命令,并显示该命令的简短描述。

$ man -f ls
ls (1)               - list directory contents
$ whatis ls
ls (1)               - list directory contents

-k   与 apropos 命令等价,用于执行关键字搜索。该参数允许用户在手册页中搜索与给定关键字相关的条目。需要注意的是,由于这是一个模糊搜索,它可能会返回一些并不是你直接想要的结果。在这种情况下,你可以尝试使用更具体的关键字,或者结合其他 man 命令的参数和特性来进一步筛选结果。

$ man -k ls
add-shell (8)        - add shells to the list of valid login shells
arm-linux-gnueabi-c++filt (1) - demangle C++ and Java symbols
arm-linux-gnueabi-nm (1) - list symbols from object files
arm-linux-gnueabi-strip (1) - discard symbols and other data from object files
arm-linux-gnueabihf-c++filt (1) - demangle C++ and Java symbols
arm-linux-gnueabihf-nm (1) - list symbols from object files
arm-linux-gnueabihf-strip (1) - discard symbols and other data from object files
blockdev (8)         - call block device ioctls from the command line
c++filt (1)          - demangle C++ and Java symbols
credentials (7)      - process identifiers
dircolors (1)        - color setup for ls
eatmydata (1)        - transparently disable fsync() and other data-to-disk synchronization calls
EVP_KDF-TLS13_KDF (7ssl) - The TLS 1.3 EVP_KDF implementation
EVP_KDF-TLS1_PRF (7ssl) - The TLS1 PRF EVP_KDF implementation
false (1)            - do nothing, unsuccessfully
git-credential (1)   - Retrieve and store user credentials
git-credential-cache--daemon (1) - Temporarily store user credentials in memory
git-credential-store (1) - Helper to store credentials on disk
git-difftool (1)     - Show changes using common diff tools
git-ls-files (1)     - Show information about files in the index and the working t

-s/-S  用于指定要查看的手册页的章节,也就是命令格式中的 [SECTION]。通过指定章节,您可以限制 man 命令只搜索和显示特定章节的手册页。Linux 的手册页分为多个章节,每个章节包含不同类型的信息:

  • 第1章(1)包含了用户命令和程序,例如 ls、cp 等。
  • 第2章(2)包含了系统调用,例如 open、read 等。
  • 第3章(3)包含了库函数,例如 C 语言的标准库函数。
  • 第4章(4)包含了设备文件和特殊文件。
  • 第5章(5)包含了文件格式和约定,例如 /etc/passwd。
  • 第6章(6)包含了游戏。
  • 第7章(7)包含了惯例和约定。
  • 第8章(8)包含了系统管理命令和守护进程。
  • 第9章(9)包含了内核的函数和宏。

括号中的数字可以帮助用户快速识别手册页的类型和内容。

例如,如果您只想模糊搜索用户命令和程序相关的手册页,可以输入以下命令:

$ man -s 1 -k ls(关键字)
arm-linux-gnueabi-c++filt (1) - demangle C++ and Java symbols
arm-linux-gnueabi-nm (1) - list symbols from object files
arm-linux-gnueabi-strip (1) - discard symbols and other data from object files
arm-linux-gnueabihf-c++filt (1) - demangle C++ and Java symbols
arm-linux-gnueabihf-nm (1) - list symbols from object files
arm-linux-gnueabihf-strip (1) - discard symbols and other data from object files
c++filt (1)          - demangle C++ and Java symbols
dircolors (1)        - color setup for ls
eatmydata (1)        - transparently disable fsync() and other data-to-disk synchronization calls
false (1)            - do nothing, unsuccessfully
git-credential (1)   - Retrieve and store user credentials
git-credential-cache--daemon (1) - Temporarily store user credentials in memory
git-credential-store (1) - Helper to store credentials on disk
git-difftool (1)     - Show changes using common diff tools

与上边的实例相比,只输出了第一章节的条目。

-t   用于将手册页内容输出到终端,并使用 troff 命令格式化格式进行显示(默认用的是 groff 输出格式页)。这通常用于在终端中以美观的格式查看手册页,特别是当手册页包含复杂的格式化文本或表格时。可以提供更好的可读性和视觉效果。不过,有可能你使用的终端不支持相应的格式化功能,那就看不到预期的结果了。此外也可以使用其他格式化手段,例如简单地格式化和着色:man ls |col |more

-w   显示命令或函数的手册页文件路径

$ man -w ls
/usr/share/man/man1/ls.1.gz

man 命令的使用非常方便,用户可以通过简单的输入命令和参数来获取需要的帮助信息。在 man 页面中,用户可以使用各种快捷键来快速浏览和查找信息,例如 q 键用于退出,Enter 键按行下翻,Space 键按页下翻,b 键上翻一页等。

man 命令还可以按照默认的搜索路径和顺序去搜索文档,也可以指定 section 来只显示制定 section 的文档页内容。例如,man 5 passwdman passwd 会显示不同的帮助页。

man 命令的文档页文件存放在 /usr/share/man 目录下。


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|棱讯科技 ( 粤ICP备2024228160号-2|粤公网安备44030002003510号 )

GMT+8, 2024-7-27 13:06 , Processed in 0.016025 second(s), 4 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表