RHCE9学习指南 第4章 获取帮助

前面已经讲了很多命令的使用,但是系统还有很多我们没见过的命令,每个命令中还有很多选项。我们不可能把每个命令、命令的每个选项都记住,那么如果遇到不认识的命令或不认识的选项,那么我们就需要通过获取帮助来解决问题。
这里介绍获取帮助的命令:whatis命令、--help选项、man命令。

4.1 whatis

whatis命令可以帮助查看到某个命令的作用,用法如下。

whatis 命令

例如,要查看ls的作用,命令如下。

[root@server ~]# whatis ls
ls: 没有合适的结果。
[root@server ~]#

如果遇到这个问题,只要使用root用户执行mandb命令。

[root@server ~]# mandb
正在删除 /usr/share/man/overrides 中的旧数据库条目...
    ...大量输出...
0 stray cats were added.
19 old database entries were purged.
[root@server ~]#

然后再次执行whatis命令。

[root@server ~]# whatis ls
ls (1)               - list directory contents
ls (1p)              - list directory contents
[root@server ~]#

在结果的第二列中,我们只要关注小括号中是纯数字的那行就可以了。从上面的结果中可以看到,ls的作用是列出目录中的内容的。
查看date命令的作用,代码如下。

[root@server ~]# whatis date
date (1)             - print or set the system date and time
date (1p)            - write the date and time
[root@server ~]# 

这里可以看到,date命令是用于显示或设置系统日期和时间的。
执行下面的命令,可以看到cal命令是用于显示日历的。

[root@server ~]# whatis cal
cal (1)              - display a calendar
cal (1p)             - print a calendar
[root@server ~]# 

whatis的输出其实是从man中截取出来的,关于man后面会讲。
通过whatis我们知道了命令有什么作用,但并没有告诉我们这个命令怎么使用及有哪些可用选项。下面介绍命令的--help选项。

4.2 --help选项

--help选项的用法如下。

命令 --help

我们先查看命令ls的具体用法。

[root@server ~]# ls --help
用法:ls [选项]... [文件]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

必选参数对长短选项同时适用。
  -a, --all             不隐藏任何以.开始的项目
  -A, --almost-all      列出除.和 ..外的任何项目
    ...大量输出...

[root@server ~]#

这里显示了ls的用法及ls所能用到的选项。例如,-a等同于--all,是列出包括隐藏文件在内的所有文件。

[root@server ~]# ls -a
.   aa.txt          .bash_logout  .cache   .dbus     .tcshrc      yy
..  anaconda-ks.cfg  .bash_profile  .config  hosts      .viminfo
11  .bash_history   .bashrc      .cshrc   initial-setup-ks.cfg  .Xauthority
[root@server ~]#

这里有很多以点开头的文件表示隐藏文件,也包括了表示当前目录的.和表示上一层目录的..。
ls -A 等同于 ls --almost-all,意思是列出除了表示当前目录.和上一层目录之外其他的所有的文件。

[root@server ~]# ls -A
11            .bash_history  .bashrc  .cshrc  initial-setup-ks.cfg  .Xauthority
aa.txt          .bash_logout  .cache   .dbus   .tcshrc     yy
anaconda-ks.cfg  .bash_profile  .config  hosts   .viminfo
[root@server ~]#

如果想目录优先排在前面,通过查看帮助可以知道应该使用选项--group-directories-first。

[root@server ~]# ls --group-directories-first
11  yy  aa.txt  anaconda-ks.cfg  hosts  initial-setup-ks.cfg
[root@server ~]#

这里11和yy是目录,如果想以时间先后顺序进行排序,可以用-t选项。

[root@server ~]# ls -t
aa.txt  hosts  11  yy  initial-setup-ks.cfg  anaconda-ks.cfg
[root@server ~]# 

这里文件越新越排在前面,读者练习时可以用ls -lt进行查看。
如果想以时间的倒叙排序可以加上-r选项。

[root@server ~]# ls -t -r
anaconda-ks.cfg  initial-setup-ks.cfg  yy  11  hosts  aa.txt
[root@server ~]#

提示:大家练习时可以用ls -ltr进行查看。关于ls其他选项,读者可以根据需要自行查看和练习。
下面看一下date的用法。
如果直接输入date命令,会显示当前的日期和时间。

[root@server ~]# date
2023年 12月 19日 星期二 15:34:57 CST
[root@server ~]#

下面查看date的更多用法。

[root@server ~]# date --help
用法:date [选项]... [+格式]
 或:date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
    ...大量输出...
或者在本地使用:info '(coreutils) date invocation'
[root@server ~]#

这里显示date有两个用法:第一个是“date [选项]... [+格式]”用于查看日期或时间。如果使用格式,格式前面要有一个加号“+”,在date --help的结果中列出了支持的所有格式,大家可以自行查看和练习,下面我们演示一些。
%Y表示年份。

[root@server ~]# date +%Y
2023
[root@server ~]#
%F表示年月日。
[root@server ~]# date +%F
2021-12-29
[root@server ~]# 
%R表示“时:分”。
[root@server ~]# date +%R
15:47
[root@server ~]#

%T表示“时:分:秒”。

[root@server ~]# date +%T
15:47:24
[root@server ~]#

我们也可以把多个格式组合一起使用,用法是date +"格式1 格式2",格式1和格式2之间用什么分隔符是可以随便指定的。
如果以“年-月-日 时:分:秒”的格式显示,命令如下。

[root@server ~]# date +"%F %T"
2021-12-19 15:47:53
[root@server ~]# 

date还有另外一种用法,就是用于设置时间的,用法如下。
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
这里格式是“月日时秒年.秒”,格式中用中括号括起来的,表示可以不写。这里“年”可以不写,不写表示今年,可以写成四位数年如2021,也可以写两位数年如21。“秒”位置可以不写,如果写,前面需要加点“.”。不过需要注意的是,如果是个位数则前面补0,如1要写成01。
假设把系统时间设置为“2012-12-21 10:00:08”,命令如下。

[root@server ~]# date 122110002012.08
2012年 12月 21日 星期五 10:00:08 CST
[root@server ~]# 
[root@server ~]# date
2012年 12月 21日 星期五 10:00:09 CST
[root@server ~]#

因为通过date改时间只是改了系统时间,并没有改变BIOS上的时间。通过hwclock -s 把系统改为和BIOS上的时间一致也就是当前时间。

[root@server ~]# hwclock -s
[root@server ~]#

如果通过date修改了系统时间之后也想写入BIOS中,那么执行命令hwclock -w即可。
刚才这种设置时间的方法看起来并不符合我们的习惯,通过查看帮助可以看到有一个-s选项。

格式为 date -s "年-月-日 时:分:秒"。

下面再次把时间设置为“2012-12-21 10:00:08”,可以使用下面的命令。

[root@server ~]# date -s "2012-12-21 10:00:08"
2012年 12月 21日 星期五 10:00:08 CST
[root@server ~]# date
2012年 12月 21日 星期五 10:00:09 CST
[root@server ~]#

这样就方便了许多。
通过hwclock -s 把系统改为当前。

[root@server ~]# hwclock -s
[root@server ~]#

关于hwclock命令的使用,请大家自行查阅帮助。

4.3 man的使用

man是manual的简写,是手册的意思,一般称之为manpage。同一个命令如果有多个意思,man放在不同“章节”中来显示,下面通过whatis查看passwd结果。

[root@server ~]# whatis passwd
openssl-passwd (1ssl) - compute password hashes
passwd (1)           - update user's authentication tokens
passwd (5)           - password file
[root@server ~]#

第二列小括号中的数字指的是在man的第几章中有详细解释。passwd作为一个修改用户密码的命令,在man的第一章中有详细解释,passwd作为一个密码文件在man的第5章中有解释。
如果要查看passwd作为密码文件的解释,可以用man 5 passwd进行查看,如下图所示。
file
按q退出。
如果要查看passwd作为一个命令的解释,可以用man 1 passwd进行查看,如下图所示。
file
按q退出,这里可以写成man passwd。

作业

  1. 现在想把系统时间设置为 2022年12月23日 上午10点23分27秒,下面哪个命令不对?
    a. date 12231023202227
    b. date -s "2022-12-23 10:23:27"
    c. date 20221223102327
    d. timedatectrl set-time "2022-12-23 10:23:27"

  2. 下面哪个命令可以列出当前目录中所有的文件,并以时间倒序排列即最先创建的文件,排在最后?
    a. ls -lRt
    b. ls -lrt
    c. ls -alrt
    d. ls -alr

相关新闻

发表回复

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

                                                                                                                                    RHCE9学习指南连载,点击阅读