目录
1、Linux 的命令行模式
2、help 命令
3、man 命令
4、man 命令输出文档汉化
注:本文档使用 Linux 版本为 CentOS 7.9
[swadian@localhost ~]$ cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)
1、Linux 的命令行模式
一般情况下,我们使用 Linux,都是通过特定的程序跟 Linux 系统进行沟通的,这个特定的程序就被称为壳程序(Shell),Linux 的壳程序就是 BASH。
Linux 的命令行格式:
[swadian@localhost ~]$ command [-options] parameterl parameter2
命令 选项 参数1 参数2
上述命令详细说明如下:
- 一行命令中第一个输入的部分绝对是命令 (command ) 或可执行文件 (例如 shell 脚本),command 为命令的名称,例如变换工作目录的命令为 cd 等。
- 中扩号 [] 并不存在于实际的命令中,表示是可选的,而加入选项设置时,通常选项前会带 - 号,例如 -h;有时候会使用选项的完整全名,则选项前带有 -- 符号,例如 --help。
- parameter1 parameter2 为依附在选项后面的参数,或是 command 的参数。
命令、选项、参数等这几个东西中间以空格来区分,不论空几格 shell 都视为一格,所以空格是很重要的特殊字符。
Linux 系统中有非常多的命令,而且不同版本的 Linux 命令也稍有不同,那,这么多的命令都需要我们去死记硬背吗?
答案是:当然不需要,我们只需要知道知道怎样使用联机文档就可以了。
2、help 命令
首先,如果你知道一个命令,但是不清楚它的具体用法,你只要使用 [--help] 这个选项,就能够对该命令的用法有一个大致的理解,例如,下边是 date 这个命令的基本用法与选项参数的介绍:
[swadian@localhost ~]$ date --help
Usage: date [OPTION]... [+FORMAT]
or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.
Mandatory arguments to long options are mandatory for short options too.
-d, --date=STRING display time described by STRING, not 'now'
-f, --file=DATEFILE like --date once for each line of DATEFILE
-I[TIMESPEC], --iso-8601[=TIMESPEC] output date/time in ISO 8601 format.
TIMESPEC='date' for date only (the default),
'hours', 'minutes', 'seconds', or 'ns' for date
and time to the indicated precision.
-r, --reference=FILE display the last modification time of FILE
-R, --rfc-2822 output date and time in RFC 2822 format.
Example: Mon, 07 Aug 2006 12:34:56 -0600
--rfc-3339=TIMESPEC output date and time in RFC 3339 format.
TIMESPEC='date', 'seconds', or 'ns' for
date and time to the indicated precision.
Date and time components are separated by
a single space: 2006-08-07 12:34:56-06:00
-s, --set=STRING set time described by STRING
-u, --utc, --universal print or set Coordinated Universal Time (UTC)
--help display this help and exit
--version output version information and exit
FORMAT controls the output. Interpreted sequences are:
%% a literal %
%a locale's abbreviated weekday name (e.g., Sun)
%A locale's full weekday name (e.g., Sunday)
%b locale's abbreviated month name (e.g., Jan)
%B locale's full month name (e.g., January)
%c locale's date and time (e.g., Thu Mar 3 23:05:25 2005)
%C century; like %Y, except omit last two digits (e.g., 20)
%d day of month (e.g., 01)
%D date; same as %m/%d/%y
%e day of month, space padded; same as %_d
%F full date; same as %Y-%m-%d
%g last two digits of year of ISO week number (see %G)
%G year of ISO week number (see %V); normally useful only with %V
%h same as %b
%H hour (00..23)
%I hour (01..12)
%j day of year (001..366)
%k hour, space padded ( 0..23); same as %_H
%l hour, space padded ( 1..12); same as %_I
%m month (01..12)
%M minute (00..59)
%n a newline
%N nanoseconds (000000000..999999999)
%p locale's equivalent of either AM or PM; blank if not known
%P like %p, but lower case
%r locale's 12-hour clock time (e.g., 11:11:04 PM)
%R 24-hour hour and minute; same as %H:%M
%s seconds since 1970-01-01 00:00:00 UTC
%S second (00..60)
%t a tab
%T time; same as %H:%M:%S
%u day of week (1..7); 1 is Monday
%U week number of year, with Sunday as first day of week (00..53)
%V ISO week number, with Monday as first day of week (01..53)
%w day of week (0..6); 0 is Sunday
%W week number of year, with Monday as first day of week (00..53)
%x locale's date representation (e.g., 12/31/99)
%X locale's time representation (e.g., 23:13:48)
%y last two digits of year (00..99)
%Y year
%z +hhmm numeric time zone (e.g., -0400)
%:z +hh:mm numeric time zone (e.g., -04:00)
%::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)
%Z alphabetic time zone abbreviation (e.g., EDT)
By default, date pads numeric fields with zeroes.
The following optional flags may follow '%':
- (hyphen) do not pad the field
_ (underscore) pad with spaces
0 (zero) pad with zeros
^ use upper case if possible
# use opposite case if possible
After any flags comes an optional field width, as a decimal number;
then an optional modifier, which is either
E to use the locale's alternate representations if available, or
O to use the locale's alternate numeric symbols if available.
Examples:
Convert seconds since the epoch (1970-01-01 UTC) to a date
$ date --date='@2147483647'
Show the time on the west coast of the US (use tzselect(1) to find TZ)
$ TZ='America/Los_Angeles' date
Show the local time for 9AM next Friday on the west coast of the US
$ date --date='TZ="America/Los_Angeles" 09:00 next Fri'
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'date invocation'
在上边的显示文档中,首先一开始是执行命令的语法 (Usage):
Usage: date [OPTION]... [+FORMAT] #1、基本语法
or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]] #2、设置事件的语法
Display the current time in the given FORMAT, or set the system date.
#显示指定格式的当前时间,或者设置系统日期。
Usage 中说明,这个 date 有两种基本语法,一种是直接执行并且取得日期返回值,且可以 +FORAMAT 的方式来显示。至于另一种方式,则是加上 MMDDhhmmCCYY 的方式来设置日期时间,它的格式是 [月月日日时时分分公元年] 的格式。
再往下看,会列举一些主要的选项,例如 -d,-f,-r 的意义, +FORMAT 的用法等:
# 主要的选项说明
Mandatory arguments to long options are mandatory for short options too.
-d, --date=STRING display time described by STRING, not 'now'
-f, --file=DATEFILE like --date once for each line of DATEFILE
-I[TIMESPEC], --iso-8601[=TIMESPEC] output date/time in ISO 8601 format.
TIMESPEC='date' for date only (the default),
'hours', 'minutes', 'seconds', or 'ns' for date
and time to the indicated precision.
-r, --reference=FILE display the last modification time of FILE
#省略.....
#下面则是重要的格式 (FORMAT)的主要项目
FORMAT controls the output. Interpreted sequences are:
%% a literal %
%a locale's abbreviated weekday name (e.g., Sun)
%A locale's full weekday name (e.g., Sunday)
#省略.....
然后再接下来,就是该命令的几个使用示例:
# 使用示例
Examples:
Convert seconds since the epoch (1970-01-01 UTC) to a date
$ date --date='@2147483647'
Show the time on the west coast of the US (use tzselect(1) to find TZ)
$ TZ='America/Los_Angeles' date
#省略.....
基本上如果是命令,那么通过这个简单的 [--help] 就可以很快速地取得你所需要的选项、参数的说明。这里说明一下,尽管我们不需要硬背命令参数,但是常用的命令还是得记忆一下,而选项就通过 [--help] 来快速查询即可。
但是,如果你要使用的是从来没有用过得命令,或是你要查询的根本就不是命令,而是文件的格式时,那又该怎么办呢?那么,你就需要使用 man page 命令了。
3、man 命令
这个 man 是 manual (操作说明) 的简写。该命令提供比 help 更加详细的文档说明。以下是执行:[man date] 的示例:
[swadian@localhost ~]$ man date
DATE(1) User Commands DATE(1)
NAME #简短的命令,数据名称说明
date - print or set the system date and time
SYNOPSIS #简短的命令语法简介
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
DESCRIPTION #较为完整的说明(重点)
Display the current time in the given FORMAT, or set the system date.
Mandatory arguments to long options are mandatory for short options too.
-d, --date=STRING
display time described by STRING, not 'now'
-f, --file=DATEFILE
like --date once for each line of DATEFILE
-I[TIMESPEC], --iso-8601[=TIMESPEC]
output date/time in ISO 8601 format. TIMESPEC='date' for date only (the default), 'hours', 'minutes', 'seconds', or 'ns' for date and time
to the indicated precision.
-r, --reference=FILE
display the last modification time of FILE
-R, --rfc-2822
output date and time in RFC 2822 format. Example: Mon, 07 Aug 2006 12:34:56 -0600
--rfc-3339=TIMESPEC
output date and time in RFC 3339 format. TIMESPEC='date', 'seconds', or 'ns' for date and time to the indicated precision. Date and time
components are separated by a single space: 2006-08-07 12:34:56-06:00
-s, --set=STRING
set time described by STRING
-u, --utc, --universal
print or set Coordinated Universal Time (UTC)
--help display this help and exit
--version
output version information and exit
FORMAT controls the output. Interpreted sequences are:
%% a literal %
%a locale's abbreviated weekday name (e.g., Sun)
%A locale's full weekday name (e.g., Sunday)
%b locale's abbreviated month name (e.g., Jan)
%B locale's full month name (e.g., January)
%c locale's date and time (e.g., Thu Mar 3 23:05:25 2005)
%C century; like %Y, except omit last two digits (e.g., 20)
%d day of month (e.g., 01)
%D date; same as %m/%d/%y
%e day of month, space padded; same as %_d
%F full date; same as %Y-%m-%d
%g last two digits of year of ISO week number (see %G)
%G year of ISO week number (see %V); normally useful only with %V
%h same as %b
%H hour (00..23)
%I hour (01..12)
%j day of year (001..366)
%k hour, space padded ( 0..23); same as %_H
%l hour, space padded ( 1..12); same as %_I
%m month (01..12)
%M minute (00..59)
%n a newline
%N nanoseconds (000000000..999999999)
%p locale's equivalent of either AM or PM; blank if not known
%P like %p, but lower case
%r locale's 12-hour clock time (e.g., 11:11:04 PM)
%R 24-hour hour and minute; same as %H:%M
%s seconds since 1970-01-01 00:00:00 UTC
%S second (00..60)
%t a tab
%T time; same as %H:%M:%S
%u day of week (1..7); 1 is Monday
%U week number of year, with Sunday as first day of week (00..53)
%V ISO week number, with Monday as first day of week (01..53)
%w day of week (0..6); 0 is Sunday
%W week number of year, with Monday as first day of week (00..53)
%x locale's date representation (e.g., 12/31/99)
%X locale's time representation (e.g., 23:13:48)
%y last two digits of year (00..99)
%Y year
%z +hhmm numeric time zone (e.g., -0400)
%:z +hh:mm numeric time zone (e.g., -04:00)
%::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)
%Z alphabetic time zone abbreviation (e.g., EDT)
By default, date pads numeric fields with zeroes. The following optional flags may follow '%':
- (hyphen) do not pad the field
_ (underscore) pad with spaces
0 (zero) pad with zeros
^ use upper case if possible
# use opposite case if possible
After any flags comes an optional field width, as a decimal number; then an optional modifier, which is either E to use the locale's alternate rep‐
resentations if available, or O to use the locale's alternate numeric symbols if available.
ENVIRONMENT
TZ Specifies the timezone, unless overridden by command line parameters. If neither is specified, the setting from /etc/localtime is used.
EXAMPLES #参考范例
Convert seconds since the epoch (1970-01-01 UTC) to a date
$ date --date='@2147483647'
Show the time on the west coast of the US (use tzselect(1) to find TZ)
$ TZ='America/Los_Angeles' date
Show the local time for 9AM next Friday on the west coast of the US
$ date --date='TZ="America/Los_Angeles" 09:00 next Fri'
GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Report date translation bugs to <http://translationproject.org/team/>
DATE STRING
The --date=STRING is a mostly free format human readable date string such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29 16:21:42" or even
"next Thursday". A date string may contain items indicating calendar date, time of day, time zone, day of week, relative time, relative date, and
numbers. An empty string indicates the beginning of the day. The date string format is more complex than is easily documented here but is fully
described in the info documentation.
AUTHOR
Written by David MacKenzie.
COPYRIGHT
Copyright © 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
SEE ALSO
The full documentation for date is maintained as a Texinfo manual. If the info and date programs are properly installed at your site, the command
info coreutils 'date invocation'
should give you access to the complete manual.
首先,在上个表格的第一行,你可以看到的是:[DATE(1)],DATE 我们知道是命令的名称,那么 (1) 代表什么?它代表的是“一般用户可使用的命令”的意思。以下是常见的几个数字的意义:
注:上表中的 1、5、8 这三个号码特别重要,最好是将这三个数字所代表的意义背下来。
接下来,就是 man page 的内容,基本上 man page 的内容大致分成下面这几个部分:
有些时候,你只记得该命令的部分关键词,但是偏偏忘记了该命令的完整名称,这个时候你要如何查出来你所想要知道的 man page 呢?
man -f 命令:
使用 -f 这个选项就可以取得更多与命令相关的信息,比如,执行 man -f man:
[swadian@localhost ~]$ man -f man
man (1) - an interface to the on-line reference manuals
man (1p) - display system documentation
man (7) - macros to format man pages
上边内容中,第三行的 [man(7)] 表示有个 man(7) 的说明文件存在,但是也有个 man(1) 存在。那当我们执行 [man man] 的时候,到底是指向哪一个说明文件?其实,你可以指定不同的文件,举例来说,上表当中的两个 man 你可以这样将它的文件显示出来:
[swadian@localhost ~]$ man 1 man #展示man(1)的说明文件
[swadian@localhost ~]$ man 7 man #展示man(7)的说明文件
当使用 [ man -f ] 命令] 时,man 只会找数据中的左边那个命令的完整名称,有一点不同都不行。
但如果我想要找的是关键词呢?
man -k 命令:
比如,执行 man -k grep 命令,你会发现具有 'grep ' 关键词的命令都被打印出来了:
[swadian@localhost ~]$ man -k grep
bzgrep (1) - search possibly bzip2 compressed files for a regular expression
egrep (1) - print lines matching a pattern
fgrep (1) - print lines matching a pattern
grep (1) - print lines matching a pattern
grep (1p) - search a file for a pattern
#省略...
事实上,还有两个命令与 man page 有关,而这两个命令是 man 的简略写法,这两个命令如下:
[swadian@localhost ~]$ whatis grep #相当于man -f
[swadian@localhost ~]$ apropos grep #相当于man -k
4、man 命令输出文档汉化
一般来说,我们看 man 命令的输出文档默认都是英语文档,但对于初学者来说,可能会有些不习惯,那么,此时使用中文文档就显得简单直观多了。
(1)首先,我们要从网上下载 man 的汉化包:
[root@localhost ~]# wget https://src.fedoraproject.org/repo/pkgs/man-pages-zh-CN/manpages-zh-1.5.1.tar.gz/13275fd039de8788b15151c896150bc4/manpages-zh-1.5.1.tar.gz
(2)下载完成后,解压 man 汉化包:
[root@localhost ~]# tar -xvf manpages-zh-1.5.1.tar.gz
(3)然后,进如解压后的目录,在安装之前进行一些配置:
[root@localhost ~]# cd manpages-zh-1.5.1/
[root@localhost manpages-zh-1.5.1]# ./configure --disable-zhtw --prefix=/usr/local/zhman
- --disable-zhtw:表示禁用繁体中文支持。软件构建时,相关的繁体中文的功能将被禁用。
- --prefix=/usr/local/zhman:指定软件安装的根目录。在这个例子中,软件将被安装到 /usr/local/zhman 目录下。prefix 选项通常用于指定软件的安装路径。
(4)执行编译:
[root@localhost manpages-zh-1.5.1]# make && make install
make 和 make install 是用于构建和安装软件的两个命令。
make 命令用于调用 Makefile 文件,执行其中定义的构建规则,编译源代码并生成可执行文件,一旦通过 make 构建成功,make install 用于将生成的可执行文件和相关文件安装到系统中。
(5)为了防止 man 中文版和英文版冲突,给 man 中文版起一个别名,并重新加载配置:
[root@localhost manpages-zh-1.5.1]# echo "alias cman='man -M /usr/local/zhman/share/man/zh_CN' " >>.bash_profile
[root@localhost manpages-zh-1.5.1]# source .bash_profile
接下来,就可以使用 cman 命令实现 man 的功能了:
如上我们就对 man 命令进行了汉化。
至此,全文结束。