第3章 学习命令

所属:第一部分 shell入门 来源:Linux命令速查手册

Linux命令成百上千,不可能全部记住。幸运的是,Linux提供了丰富的帮助工具,让你随时可以学习和查阅命令用法。


3.1 使用man来查看命令的用法

man command
man ls

查看命令的手册页(manual page),是最权威的命令参考。

man 中的导航快捷键

按键功能
空格 / PageDown / f向下翻页
b / PageUp向上翻页
q退出
/关键词向下搜索
?关键词向上搜索
n下一个搜索结果
N上一个搜索结果
g跳到开头
G跳到结尾
h显示帮助

man page 的标准结构

  1. NAME - 命令名称和简短描述
  2. SYNOPSIS - 语法概要
  3. DESCRIPTION - 详细描述
  4. OPTIONS - 选项说明
  5. EXAMPLES - 示例(不一定都有)
  6. SEE ALSO - 相关命令
  7. BUGS - 已知问题
  8. AUTHOR - 作者

3.2 基于命令的功能来搜索命令

man -k keyword
apropos keyword

按关键词搜索相关命令,当你想做某件事但不知道用什么命令时非常有用。

示例:

$ apropos search
apropos (1)          - search the manual page names and descriptions
find (1)             - search for files in a directory hierarchy
grep (1)             - print lines matching a pattern
locate (1)           - find files by name

3.3 根据命令的名称快速查找命令的功能

whatis command
whatis ls

显示命令的一行简短描述,比man快得多。

示例:

$ whatis ls
ls (1)          - list directory contents

3.4 重建命令的man数据库

sudo mandb

更新man手册数据库。新装软件后可以运行一下,确保 aproposwhatis 能搜到新命令。


3.5 读取命令的特定man page

man手册分为多个章节,同一个关键词可能在不同章节有不同含义:

章节内容
1用户命令(User Commands)
2系统调用(System Calls)
3库函数(Library Functions)
4设备文件(Special Files)
5文件格式(File Formats)
6游戏(Games)
7杂项(Miscellaneous)
8系统管理命令(System Administration)
man 5 passwd    # 查看passwd文件格式说明(第5章)
man 1 passwd    # 查看passwd命令说明(第1章,默认)

3.6 打印man page

man -t command | lpr

将man手册页格式化后打印。


3.7 学习info命令

info command

查看 info 格式的文档。GNU项目的软件通常提供info文档,内容比man更详细,结构更像超文本。


3.8 在Info页面中导航

Info 导航快捷键

按键功能
?显示所有快捷键帮助
q退出
n下一节点(Next)
p上一节点(Previous)
u上一级(Up)
Enter进入光标所在的链接
l返回上次访问的节点(Last)
d回到Info主目录(Directory)
空格 / PageDown向下翻页
b / PageUp向上翻页
s搜索
Tab跳到下一个链接

info 就像一个纯文本的网页浏览器,有节点、链接、目录树。


3.9 查找命令的可执行文件、源文件和man page的路径

whereis command
whereis ls

查找命令的:

  • 二进制可执行文件路径
  • 源代码路径
  • man手册页路径

示例:

$ whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz

3.10 读取命令的描述

which command
which ls

显示命令的可执行文件完整路径

当系统中有多个同名版本的命令时,which 会告诉你实际运行的是哪一个。

示例:

$ which python
/usr/bin/python

3.11 基于功能查找命令

apropos keyword
man -k keyword

按功能描述搜索命令。和3.2节相同,这是查找未知命令的重要手段。

使用场景

  • 想压缩文件但记不清命令名 → apropos compress
  • 想查看进程 → apropos process
  • 想编辑文本 → apropos editor

3.12 找出将要运行的命令的版本

type command
command -v command

显示命令的类型,告诉你它是:

  • 别名(alias)
  • shell内置命令(builtin)
  • 外部可执行文件(file)
  • 关键字(keyword)

示例:

$ type ls
ls is aliased to `ls --color=auto'
 
$ type cd
cd is a shell builtin
 
$ type grep
grep is /usr/bin/grep

3.13 小结

帮助工具速查表

命令用途
man command查看命令详细手册
info command查看GNU info文档(更详细)
whatis command命令的一行简短描述
apropos keyword按关键词搜索命令
whereis command查找二进制、源码、man页路径
which command显示可执行文件路径
type command显示命令类型(别名/内置/外部)

掌握如何查找和学习命令是Linux自学的关键。遇到不会的命令,先 man 一下!


Linux Shell man help apropos which whereis