下载APP

7.第二章 Linux基础入门和帮助 -- Linux 基础和帮助(三)

“开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 4 天,点击查看活动详情

1.8 字符集和编码

许多场合下,字符集与编码这两个概念常被混为一谈,但两者是有差别的。字符集与字符集编码是两个不同层面的概念

charset是character set的简写,即字符集,即二进制和字符的对应关系,不关注最终的存储形式encoding是charset encoding的简写,即字符集编码,简称编码,实现如何将字符转化为实际的二进制进行存储或相反,编码决定了空间的使用的大小 在这里插入图片描述

1.8.1 ASCII码

计算机内部,所有信息最终都是一个二进制值。上个世纪60年代,美国制定了一套字符编码,对英语字符与二进制位之间的关系,做了统一规定,即ASCII(American Standard Code for Information Interchange) 码

ASCII 码一共规定了128个字符的编码,占用了一个字节的后面7位,最前面的一位统一规定为 0 在这里插入图片描述

范例:查看 ascii 表

[root@rocky8 ~]# dnf -y install man-pages
[root@rocky8 ~]# man ascii

在这里插入图片描述

范例:

[root@rocky8 ~]# cat >test.txt <<EOF
> Aa
> EOF
[root@rocky8 ~]# cat test.txt 
Aa
[root@rocky8 ~]# hexdump -C test.txt 
00000000  41 61 0a                                          |Aa.|
00000003	#16进制的41转换成10进制是65,对应的ASCII码就是A。
			#16进制的61转换成10进制是97,对应的ASCII码就是a。
[root@rocky8 ~]# echo "ibase=16;41"|bc
65
[root@rocky8 ~]# echo "ibase=16;61"|bc
97

1.8.2 Unicode

由于计算机是美国人发明的,因此,最早只有127个字母被编码到计算机里,即ASCII编码,但是要处理中文显然一个字节是不够的,至少需要两个字节,而且还不能和ASCII编码冲突,所以,中国制定了GB2312编码,用来把中文编进去。

全世界有上百种语言,日本把日文编到Shift_JIS里,韩国把韩文编到Euc-kr里,各国有各国的标准,就会不可避免地出现冲突,结果就是,在多语言混合的文本中,显示出来会有乱码

为了表示世界上所有语言中的所有字符。每一个符号都给予一个独一无二的编码数字,Unicode 是一个很大的集合,现在的规模可以容纳100多万个符号。Unicode 仅仅只是一个字符集,规定了每个字符对应的二进制代码,至于这个二进制代码如何存储则没有规定

Unicode编码方案:

  • UTF-8:变长,1到4个字节,当前最主流的方案
  • UTF-16:变长,2或4个字节
  • UTF-32:固定长度,4个字节

在这里插入图片描述

UTF-8 是目前互联网上使用最广泛的一种 Unicode 编码方式,可变长存储。使用 1 - 4 个字节表示一个字符,根据字符的不同变换长度。

编码规则如下: 对于单个字节的字符,第一位设为 0,后面的 7 位对应这个字符的 Unicode 码。因此,对于英文中的 0-127 号字符,与 ASCII 码完全相同。这意味着 ASCII 码的文档可用 UTF-8 编码打开

对于需要使用 N 个字节来表示的字符(N > 1),第一个字节的前 N 位都设为 1,第 N + 1 位设为0,剩余的 N - 1 个字节的前两位都设位 10,剩下的二进制位则使用这个字符的 Unicode 码来填充

编码转换和查询参考链接: http://www.chi2ko.com/tool/CJK.htm

https://www.bejson.com/convert/unicode_chinese/

https://javawind.net/tools/native2ascii.jsp?action=transform

http://tool.oschina.net/encode

http://web.chacuo.net/charsetescape

Unicode和UTF-8

Unicode符号范围(十六进制) UTF-8编码方式二进制)
0000 0000-0000 007F 0xxxxxxx
0000 0080-0000 07FF 110xxxxx 10xxxxxx
0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
0001 0000-0010 FFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx

范例:

“汉”的 Unicode 码 0x6C49(110 110001 001001),需要三个字节存储,格式为: 1110xxxx 10xxxxxx 10xxxxxx,从后向前依次填充对应格式中的 x,多出的 x 用 0 补,得出UTF-8 编码为11100110 10110001 10001001

“马”的 Unicode 码 0x9A6C(1001 101001 101100),需要三个字节存储,格式为: 1110xxxx 10xxxxxx 10xxxxxx,从后向前依次填充对应格式中的 x,多出的 x 用 0 补,得出UTF-8 编码为11101001 10101001 10101100

范例:修改LANG变量实现中文语言提示

[root@rocky8 ~]# echo $LANG
en_US.UTF-8 #默认是英文
[root@rocky8 ~]# raymond
-bash: raymond: command not found #提示信息也是英文

[root@rocky8 ~]# LANG=zh_CN.UTF-8 #改为中文
[root@rocky8 ~]# echo $LANG 
zh_CN.UTF-8
[root@rocky8 ~]# raymond
-bash: raymond: 未找到命令 #提示信息就是中文

1.9 命令行扩展和被括起来的集合

1.9.1 命令行扩展:`` 和 $()

把一个命令的输出打印给另一个命令的参数

$(COMMAND) 或 `COMMAND`

#COMMAND必须是一个有标出信息的命令

范例:比较 “ ” ,‘ ’, ``三者区别

[root@rocky8 ~]# echo "echo $HOSTNAME"
echo rocky8
[root@rocky8 ~]# echo 'echo $HOSTNAME'
echo $HOSTNAME
[root@rocky8 ~]# echo `echo $HOSTNAME`
rocky8

#结论:
单引号:六亲不认,变量和命令都不识别,都当成了普通的字符串
反向单引号:变量和命令都识别,并且会将反向单引号的内容当成命令进行执行后,再交给调用反向单引号的命令继续
双引号:不能识别命令,可以识别变量

范例:

[root@rocky8 ~]# hostname
rocky8
[root@rocky8 ~]# echo My hostname is rocky8
My hostname is rocky8
[root@rocky8 ~]# echo My hostname is hostname
My hostname is hostname
[root@rocky8 ~]# echo My hostname is `hostname`		#使用``反向单引号执行命令
My hostname is rocky8

[root@rocky8 ~]# echo My hostname is $(hostname)		#$()和``反向单引号功能一样
My hostname is rocky8

#当一个命令需要被另一个命令调用时,就需要使用反向单引号
[root@rocky8 ~]# touch a.txt
[root@rocky8 ~]# ls
anaconda-ks.cfg a.txt
[root@rocky8 ~]# touch b.txt
[root@rocky8 ~]# ls
anaconda-ks.cfg b.txt a.txt
[root@rocky8 ~]# touch `hostname`.log		#创建一个主机名.log的空文件
[root@rocky8 ~]# ls
anaconda-ks.cfg  b.txt a.txt rocky8.log	#rocky8.log就是刚创建的文件

[root@rocky8 ~]# date +%F
2021-10-06
[root@rocky8 ~]# touch `date +%F`.log #创建一个日期.log文件
[root@rocky8 ~]# ls
2021-10-06.log  anaconda-ks.cfg #2020-11-16.log 这个就是刚创建的文件

范例:$( ) 和 ``

[root@rocky8 ~]# touch `date +%F`.txt
[root@rocky8 ~]# ll `echo `date +%F`.txt`
-bash: .txt: command not found
-rw-r--r-- 1 root root 0 Oct  6 14:59 date
-rw-r--r-- 1 root root 0 Oct  6 14:59 +%F

[root@rocky8 ~]# ll $(echo $(date +%F).txt)
-rw-r--r-- 1 root root 0 Oct  6 14:59 2021-10-06.txt

[root@rocky8 ~]# ll `echo $(date +%F).txt`
-rw-r--r-- 1 root root 0 Oct  6 14:59 2021-10-06.txt

[root@rocky8 ~]# ll $(echo `date +%F`.txt)
-rw-r--r-- 1 root root 0 Oct  6 14:59 2021-10-06.txt

范例:$( ) 和 ``的区别

[root@rocky8 ~]# echo `echo \\\\\\\w`
\\w
[root@rocky8 ~]# echo $(echo \\\\\\\w)
\\\w
#为什么输出的不一样?

#这就引申出了另一个问题:反引号与$()有没有区别?
#这是一个非常有意思的问题,但要解释这个问题确比较困难,我来换一个简单一点的例子,通俗的解释一下。
#比如我们有一个shell脚本名为test.sh有一下两行

[root@rocky8 ~]# vim test.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-01
#FileName:      test.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
echo  `echo \$HOSTNAME`
echo $(echo \$HOSTNAME)

[root@rocky8 ~]# bash -x test.sh 
++ echo rocky8
+ echo rocky8
rocky8
++ echo '$HOSTNAME'
+ echo '$HOSTNAME'
$HOSTNAME

#通过上图我们可以得出两个结论:
1.	反引号中\$并没有将$的特殊意义转换 反引号包含的内容 echo \$hostname 仍然被解释为一个echo $HOSTNAME 取到了这个变量的值并输出所以反引号返回的值为 rocky8
2.	$()则正好相反,$明显被\转义成了一个普通字符,所以并没有取到变量值,而是返回了字符串本身的意思,故而返回了$HOSTNAME
  这说明反引号对反斜杠(\)有特殊的转化,至少我们看到当我们直接用1个\时,它本身并不会起到转义的作用。现在我们将脚本的内容做一些修改:
[root@rocky8 ~]# vim test.sh
#!/bin/bash
# 
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-01
#FileName:      test.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
echo  `echo \\$HOSTNAME`
echo $(echo \\$HOSTNAME) 

[root@rocky8 ~]# bash -x test.sh 
++ echo '$HOSTNAME'
+ echo '$HOSTNAME'
$HOSTNAME
++ echo '\rocky8'
+ echo '\rocky8'
\rocky8

这次看起来似乎反转了:
1.	反引号反而输出了$HOSTNAME的字面意思
2.	$()如我们所愿的取到了变量值,并理所应当的输出了一个被转义的字符串\
  我们再来做一个实验,再建立一个脚本名为a.sh:
[root@rocky8 ~]# vim a.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-01
#FileName:      a.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
echo    `echo \\\\ `
echo  $(echo \\\\ )

注意:最后一个反引号和)的前面都有一个空格,否则最后一个反引号和)都将被注释,不会被当做结束的替换符号。

使用bash -x a.sh的结果如下:
[root@rocky8 ~]# bash -x a.sh 
++ echo '\'
+ echo '\'
\
++ echo '\\'
+ echo '\\'
\\

通过我们可以看到:
1.	反引号中4个\被输出为1个\
2.	$()中4个\被输出为2个\
  我们将脚本修改为8个\:
[root@rocky8 ~]# vim a.sh
[root@rocky8 ~]# cat a.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-01
#FileName:      a.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
echo   `echo \\\\\\\\ `
echo  $(echo \\\\\\\\ )

[root@rocky8 ~]# bash -x a.sh 
++ echo '\\'
+ echo '\\'
\\
++ echo '\\\\'
+ echo '\\\\'
\\\\

由此,我们可以得出结论:
1.	反引号齐本身就对\进行了转义,保留了齐本身意思,如果我们想在反引号中起到\的特殊意义,我们必须使用2个\来进行表示。
所以我们可以简单的想象成反引号中: \\ = \
2.	$()中则不需要考虑\的问题,与我们平常使用的一样:\ = \
题外话: 反引号是老的用法,$()是新的用法,不管是在学习测试中,还是在实际工作中,$()的用法都是被推荐的。

[root@rocky8 ~]# C_PS1=`echo "PS1='\[\e[1;31m\][\u@\h \W]\\$ \[\e[0m\]'"`
[root@rocky8 ~]# echo $C_PS1
PS1='\[\e[1;31m\][\u@\h \W]$ \[\e[0m\]'  #使用反向单引号,这里$前面没有\
[root@rocky8 ~]# C_PS1=$(echo "PS1='\[\e[1;31m\][\u@\h \W]\\$ \[\e[0m\]'")
[root@rocky8 ~]# echo $C_PS1
PS1='\[\e[1;31m\][\u@\h \W]\$ \[\e[0m\]' #使用$(),$前面有\

1.9.2 括号扩展:{ }

{} 可以实现打印重复字符串的简化形式

范例:

[root@rocky8 ~]# echo file{1,3,5}
file1 file3 file5

[root@rocky8 ~]# rm -f file{1,3,5}
[root@rocky8 ~]# ls
anaconda-ks.cfg  anaconda-screenshots

[root@rocky8 ~]# echo {1..10}
1 2 3 4 5 6 7 8 9 10

[root@rocky8 ~]# echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z

[root@rocky8 ~]# echo {000..20..2}
000 002 004 006 008 010 012 014 016 018 020

[root@rocky8 ~]# echo {a..z..2}
a c e g i k m o q s u w y

[root@rocky8 ~]# echo {A..z}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [  ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z

1.10 tab 键补全

tab 键可以实现命令及路径等补全,提高输入效率,避免出错

1.10.1 命令补全

  • 内部命令:
  • 外部命令:bash根据PATH环境变量定义的路径,自左而右在每个路径搜寻以给定命令名命名的文件,第一次找到的命令即为要执行的命令
  • 命令的子命令补全,需要安装 bash-completion

注意:用户给定的字符串只有一条惟一对应的命令,直接补全,否则,再次Tab会给出列表

范例:

[root@rocky8 ~]# yum -y install bash-completion
[root@centos7 ~]# exit # 装完退出
logout

[root@rocky8 ~]# nmcli connection 2TAB
add delete edit help load monitor show
clone down export import modify reload up

1.10.2 路径补全

把用户给出的字符串当做路径开头,并在其指定上级目录下搜索以指定的字符串开头的文件名

如果惟一:则直接补全

否则:再次Tab给出列表

1.10.3 双击Tab键

  • command 2Tab 所有子命令或文件补全
  • string2Tab 以string开头命令
  • /2Tab 显示所有根目录下一级目录,包括隐藏目录
  • ./2Tab 当前目录下子目录,包括隐藏目录
  • *2Tab 当前目录下子目录,不包括隐藏目录
  • ~2Tab 所有用户列表
  • $2Tab 所有变量
  • @2Tab /etc/hosts记录 (centos7 不支持)
  • =2Tab 相当于ls –A (centos7不支持)

1.11 命令行历史

当执行命令后,系统默认会在内存记录执行过的命令

当用户正常退出时,会将内存的命令历史存放对应历史文件中,默认是~/.bash_history

登录shell时,会读取命令历史文件中记录下的命令加载到内存中

登录进shell后新执行的命令只会记录在内存的缓存区中;这些命令会用户正常退出时“追加”至命令历史文件中

利用命令历史。可以用它来重复执行命令,提高输入效率

命令:history

history [-c] [-d offset] [n]
history -anrw [filename]
history -ps arg [arg...]
  • -c: 清空命令历史
  • -d offset: 删除历史中指定的第offset个命令
  • n: 显示最近的n条历史
  • -a: 追加本次会话新执行的命令历史列表至历史文件
  • -r: 读历史文件附加到历史列表
  • -w: 保存历史列表到指定的历史文件
  • n: 读历史文件中未读过的行到历史列表
  • -p: 展开历史参数成多行,但不存在历史列表中
  • -s: 展开历史参数成一行,附加在历史列表后

范例:

[root@rocky8 ~]# type history 
history is a shell builtin
[root@rocky8 ~]# history 
    1  shutdown -h now
    2  ip a
    3  pidof tail
    4  ll /proc/`pidof tail`/fd
    5  pidof tail
...
  192  hostname
  193  echo My hostname is rocky8
  194  echo My hostname is hostname
  195  echo My hostname is `hostname`
  196  echo My hostname is $(hostname)
  197  date +%F
  198  touch `date +%F`.log
  199  ls
  200  rm *.log
  201  rm test.conf 
  202  touch `date +%F`.log
  203  ls
  204  ll `echo `date +%F`.txt`
  205  ll $(echo $(date +%F).txt)
  206  ll `echo $(date +%F).txt`
  207  ll $(echo `date +%F`.txt)
  208  ls
  209  touch `echo `date +%F`.txt`
  210  touch `date +%F`.txt
  211  ll `echo `date +%F`.txt`
  212  ll $(echo $(date +%F).txt)
  213  ll `echo $(date +%F).txt`
  214  ll $(echo `date +%F`.txt)
  215  history 
[root@rocky8 ~]# !197 #!数字 执行历史命令
date +%F
2021-10-06

[root@rocky8 ~]# history 
    1  shutdown -h now
    2  ip a
    3  pidof tail
    4  ll /proc/`pidof tail`/fd
...
  216  date +%F
  217  touch `echo `date +%F`.txt`
  218  history 
[root@rocky8 ~]# !-3 #!-数字  执行倒数第几个命令
date +%F
2021-10-06

[root@rocky8 ~]# mysql -uroot -p123456
bash: mysql: command not found...
Packages providing this file are:
'mariadb'
'mysql'
[root@rocky8 ~]# history
    1  tty
    2  who
    3  whoami
…
  230  mysql -uroot -p123456
  231  history
[root@rocky8 ~]#  history -c	# history -c 清空历史命令
[root@rocky8 ~]# history 
1	history

[root@rocky8 ~]# exit
logout
[root@centos8 ~]# history 
    1  tty
    2  who
    3  whoami
…
  187  tty
  188  history 
  189  exit
  190  history
#退出重新登陆,还有历史命令
[root@rocky8 ~]# ls -a .bash_history 
.bash_history		#因为之前的历史命令存在了这个文件里,清空命令只是清空了内存中的记录

命令历史相关环境变量

HISTSIZE:命令历史记录的条数

HISTFILE:指定历史文件,默认为~/.bash_history

HISTFILESIZE:命令历史文件记录历史的条数

HISTTIMEFORMAT="%F %T `whoami` " 显示时间和用户

HISTIGNORE="str1:str2*:…" 忽略str1命令,str2开头的历史

HISTCONTROL:控制命令历史的记录方式
    ignoredups 是默认值,可忽略重复的命令,连续且相同为“重复”
    ignorespace 忽略所有以空白开头的命令
    ignoreboth 相当于ignoredups, ignorespace的组合
    erasedups 删除重复命令

范例:

[root@rocky8 ~]# HISTTIMEFORMAT="%F %T `whoami` " #设置历史命令格式
[root@rocky8 ~]# history 
    1  2021-10-06 14:50:23 root shutdown -h now
    2  2021-10-06 14:50:23 root ip a
    3  2021-10-06 14:50:23 root pidof tail
    4  2021-10-06 14:50:23 root ll /proc/`pidof tail`/fd
    5  2021-10-06 14:50:23 root pidof tail
...
  217  2021-10-06 15:03:38 root touch `echo `date +%F`.txt`
  218  2021-10-06 15:03:47 root history 
  219  2021-10-06 15:03:57 root date +%F
  220  2021-10-06 15:05:24 root HISTTIMEFORMAT="%F %T `whoami` "
  221  2021-10-06 15:05:27 root history 

持久保存变量 以上变量可以 export 变量名="值" 形式存放在 /etc/profile 或 ~/.bash_profile

范例:

[root@rocky8 ~]# echo "export EDITOR=vim" >> /etc/profile.d/env.sh 
[root@rocky8 ~]# echo 'export HISTTIMEFORMAT="%F %T "' >> /etc/profile.d/env.sh 
[root@rocky8 ~]# cat /etc/profile.d/env.sh 
PS1='\[\e[1;31m\][\u@\h \W]\$ \[\e[0m\]'
export EDITOR=vim
export HISTTIMEFORMAT="%F %T " 

[root@rocky8 ~]# exit
logout

[root@rocky8 ~]# history 
    1  2021-10-01 07:08:55 shutdown -h now
    2  2021-10-01 07:08:55 ip a
    3  2021-10-01 07:08:55 tty
    4  2021-10-01 07:08:55 w
    5  2021-10-01 07:08:55 echo ${SHELL}

1.12 调用命令行历史

#重复前一个命令方法
重复前一个命令使用上方向键,并回车执行
按 !! 并回车执行
输入 !-1 并回车执行
按 Ctrl+p 并回车执行

!:0 执行前一条命令(去除参数)
!n 执行history命令输出对应序号n的命令
!-n 执行history历史中倒数第n个命令
!string 重复前一个以“string”开头的命令
!?string 重复前一个包含string的命令
!string:p 仅打印命令历史,而不执行
!$:p 打印输出 !$ (上一条命令的最后一个参数)的内容
!*:p 打印输出 !*(上一条命令的所有参数)的内容
^string 删除上一条命令中的第一个string
^string1^string2 将上一条命令中的第一个string1替换为string2
!:gs/string1/string2 将上一条命令中所有的string1都替换为 string2
使用up(向上)和down(向下)键来上下浏览从前输入的命令
ctrl-r来在命令历史中搜索命令
(reverse-i-search)`’:
Ctrl+g:从历史搜索模式退出

#要重新调用前一个命令中最后一个参数
!$ 表示
Esc, . 点击Esc键后松开,然后点击 . 键
Alt+ . 按住Alt键的同时点击 . 键

command !^ 利用上一个命令的第一个参数做command的参数
command !$ 利用上一个命令的最后一个参数做command的参数
command !* 利用上一个命令的全部参数做command的参数
command !:n 利用上一个命令的第n个参数做command的参数
command !n:^ 调用第n条命令的第一个参数
command !n:$ 调用第n条命令的最后一个参数
command !n:m 调用第n条命令的第m个参数
command !n:* 调用第n条命令的所有参数
command !string:^ 从命令历史中搜索以 string 开头的命令,并获取它的第一个参数
command !string:$ 从命令历史中搜索以 string 开头的命令,并获取它的最后一个参数
command !string:n 从命令历史中搜索以 string 开头的命令,并获取它的第n个参数
command !string:* 从命令历史中搜索以 string 开头的命令,并获取它的所有参数

范例:

[root@rocky8 ~]# cat > a.txt <<EOF
> a
> b
> c
> EOF

[root@rocky8 ~]# cat > b.txt <<EOF
> hello
> EOF

[root@rocky8 ~]# ls a.txt b.txt
a.txt  b.txt
[root@rocky8 ~]# ls !* # !* 上一个命令的所有参数
ls a.txt b.txt
a.txt  b.txt

[root@rocky8 ~]# ls a.txt b.txt
a.txt  b.txt
[root@rocky8 ~]# ls b.txt  # Esc, . 点击Esc键后松开,然后点击 . 键,表示上一个命令的最后一个参数
b.txt

[root@rocky8 ~]# ls a.txt b.txt
a.txt  b.txt
[root@rocky8 ~]# ls !$ # !$ 表示上一个命令的最后一个参数
ls b.txt
b.txt

1.13 bash的快捷键

Ctrl + l 清屏,相当于clear命令
Ctrl + o 执行当前命令,并重新显示本命令
Ctrl + s 阻止屏幕输出,锁定
Ctrl + q 允许屏幕输出,解锁
Ctrl + c 终止命令
Ctrl + z 挂起命令
Ctrl + a 光标移到命令行首,相当于Home
Ctrl + e 光标移到命令行尾,相当于End
Ctrl + f 光标向右移动一个字符
Ctrl + b 光标向左移动一个字符
Ctrl + xx 光标在命令行首和光标之间移动

Alt + f 光标向右移动一个单词尾
Alt + b 光标向左移动一个单词首
Ctrl + u 从光标处删除至命令行首
Ctrl + k 从光标处删除至命令行尾
Alt + r 删除当前整行
Ctrl + w 从光标处向左删除至单词首
Alt + d 从光标处向右删除至单词尾
Alt + Backspace 删除左边单词
Ctrl + d 删除光标处的一个字符

Ctrl + h 删除光标前的一个字符
Ctrl + y 将删除的字符粘贴至光标后
Alt + c 从光标处开始向右更改为首字母大写的单词
Alt + u 从光标处开始,将右边一个单词更改为大写
Alt + l 从光标处开始,将右边一个单词更改为小写
Ctrl + t 交换光标处和之前的字符位置
Alt + t 交换光标处和之前的单词位置
Alt + # 提示输入指定字符后,重复显示该字符#次

注意:Alt组合快捷键经常和其它软件冲突

范例:xshell中启动 alt 键

在这里插入图片描述

2.获得帮助

获取帮助的能力决定了技术的能力!

多层次的帮助

  • whatis
  • command --help
  • man and info
  • /usr/share/doc/
  • Red Hat documentation 、Ubuntu documentation
  • 软件项目网站
  • 其它网站
  • 搜索

2.1 whatis

whatis 使用数据库来显示命令的简短描述

此工具在系统刚安装后,不可立即使用,需要生成数据库后才可使用

#CentOS 7 版本以后
mandb
#CentOS 6 版本之前
makewhatis

范例:

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

[root@rocky8 ~]# man -f cal
cal (1)              - display a calendar
cal (1p)             - print a calendar

范例:

[root@rocky8 ~]# whatis ls
ls: nothing appropriate.

#生成man相关数据库
[root@rocky8 ~]# mandb
Purging old database entries in /usr/share/man/overrides...
Processing manual pages under /usr/share/man/overrides...
Purging old database entries in /usr/share/man...
Processing manual pages under /usr/share/man...
Purging old database entries in /usr/share/man/ru...
Processing manual pages under /usr/share/man/ru...
Purging old database entries in /usr/share/man/fr...
Processing manual pages under /usr/share/man/fr...
Purging old database entries in /usr/share/man/ja...
Processing manual pages under /usr/share/man/ja...
Purging old database entries in /usr/share/man/ko...
Processing manual pages under /usr/share/man/ko...
Purging old database entries in /usr/share/man/pl...
Processing manual pages under /usr/share/man/pl...
Purging old database entries in /usr/share/man/sk...
Processing manual pages under /usr/share/man/sk...
Purging old database entries in /usr/share/man/cs...
Processing manual pages under /usr/share/man/cs...
Purging old database entries in /usr/share/man/da...
Processing manual pages under /usr/share/man/da...
Purging old database entries in /usr/share/man/de...
Processing manual pages under /usr/share/man/de...
Purging old database entries in /usr/share/man/hu...
Processing manual pages under /usr/share/man/hu...
Purging old database entries in /usr/share/man/id...
Processing manual pages under /usr/share/man/id...
Purging old database entries in /usr/share/man/it...
Processing manual pages under /usr/share/man/it...
Purging old database entries in /usr/share/man/pt_BR...
Processing manual pages under /usr/share/man/pt_BR...
Purging old database entries in /usr/share/man/sv...
Processing manual pages under /usr/share/man/sv...
Purging old database entries in /usr/share/man/tr...
Processing manual pages under /usr/share/man/tr...
Purging old database entries in /usr/share/man/zh_CN...
Processing manual pages under /usr/share/man/zh_CN...
Purging old database entries in /usr/share/man/zh_TW...
Processing manual pages under /usr/share/man/zh_TW...
Purging old database entries in /usr/share/man/es...
Processing manual pages under /usr/share/man/es...
Purging old database entries in /usr/share/man/nl...
Processing manual pages under /usr/share/man/nl...
Purging old database entries in /usr/share/man/uk...
Processing manual pages under /usr/share/man/uk...
Purging old database entries in /usr/share/man/ca...
Processing manual pages under /usr/share/man/ca...
Purging old database entries in /usr/share/man/overrides...
Processing manual pages under /usr/share/man/overrides...
Purging old database entries in /usr/local/share/man...
Processing manual pages under /usr/local/share/man...
0 man subdirectories contained newer manual pages.
0 manual pages were added.
0 stray cats were added.
0 old database entries were purged.

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

[root@centos6 ~]# makewhatis

2.2 查看命令的帮助

2.2.1 内部命令帮助

  • help COMMAND
  • man bash

范例:

[root@rocky8 ~]# type enable
enable is a shell builtin
[root@rocky8 ~]# help enable
enable: enable [-a] [-dnps] [-f filename] [name ...]
    Enable and disable shell builtins.
    
    Enables and disables builtin shell commands.  Disabling allows you to
    execute a disk command which has the same name as a shell builtin
    without using a full pathname.
    
    Options:
      -a	print a list of builtins showing whether or not each is enabled
      -n	disable each NAME or display a list of disabled builtins
      -p	print the list of builtins in a reusable format
      -s	print only the names of Posix `special' builtins
    
    Options controlling dynamic loading:
      -f	Load builtin NAME from shared object FILENAME
      -d	Remove a builtin loaded with -f
    
    Without options, each NAME is enabled.
    
    To use the `test' found in $PATH instead of the shell builtin
    version, type `enable -n test'.
    
    Exit Status:
Returns success unless NAME is not a shell builtin or an error occurs.


[root@rocky8 ~]# type hostname
hostname is /usr/bin/hostname
[root@rocky8 ~]# man hostname
HOSTNAME(1)                               Linux Programmer's Manual                               HOSTNAME(1)

NAME
       hostname - show or set the system's host name
       domainname - show or set the system's NIS/YP domain name
       ypdomainname - show or set the system's NIS/YP domain name
       nisdomainname - show or set the system's NIS/YP domain name
       dnsdomainname - show the system's DNS domain name

SYNOPSIS
       hostname  [-a|--alias]  [-d|--domain] [-f|--fqdn|--long] [-A|--all-fqdns] [-i|--ip-address] [-I|--all-
       ip-addresses] [-s|--short] [-y|--yp|--nis]
       hostname [-b|--boot] [-F|--file filename] [hostname]
       hostname [-h|--help] [-V|--version]

       domainname [nisdomain] [-F file]
       ypdomainname [nisdomain] [-F file]
       nisdomainname [nisdomain] [-F file]

       dnsdomainname


[root@rocky8 ~]# whatis passwd
openssl-passwd (1ssl) - compute password hashes
passwd (1)           - update user's authentication tokens
passwd (5)           - password file
[root@rocky8 ~]# man 5 passwd
PASSWD(5)                                 Linux Programmer's Manual                                 PASSWD(5)

NAME
       passwd - password file

DESCRIPTION
       The /etc/passwd file is a text file that describes user login accounts for the system.  It should have
       read permission allowed for all users (many utilities, like ls(1) use it to  map  user  IDs  to  user‐
       names), but write access only for the superuser.

       In  the  good  old days there was no great problem with this general read permission.  Everybody could
       read the encrypted passwords, but the hardware was too slow to crack a well-chosen password, and more‐
       over  the  basic  assumption used to be that of a friendly user-community.  These days many people run
       some version of the shadow password suite, where /etc/passwd has an  'x'  character  in  the  password
       field, and the encrypted passwords are in /etc/shadow, which is readable by the superuser only.

       If  the  encrypted  password,  whether  in /etc/passwd or in /etc/shadow, is an empty string, login is
       allowed without even asking for a password.  Note that this functionality may  be  intentionally  dis‐
       abled  in  applications,  or  configurable  (for  example  using the "nullok" or "nonull" arguments to
       pam_unix.so).

[root@rocky8 ~]# man bash

2.2.2 外部命令和软件帮助

  • COMMAND --help 或 COMMAND -h
  • 使用 man 手册(manual): man COMMAND
  • 信息页:info COMMAND
  • 程序自身的帮助文档:README、INSTALL、ChangeLog
  • 程序官方文档
  • 相关网站
  • 搜索引擎

2.3 --help 或 -h 选项

显示用法总结和参数列表,大多数命令使用,但并非所有的

范例:

[root@rocky8 ~]# 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'
      --debug                annotate the parsed date,
                              and warn about questionable usage to stderr
  -f, --file=DATEFILE        like --date; once for each line of DATEFILE
  -I[FMT], --iso-8601[=FMT]  output date/time in ISO 8601 format.
                               FMT='date' for date only (the default),
                               'hours', 'minutes', 'seconds', or 'ns'
                               for date and time to the indicated precision.
                               Example: 2006-08-14T02:34:56-06:00
  -R, --rfc-email            output date and time in RFC 5322 format.
                               Example: Mon, 14 Aug 2006 02:34:56 -0600
      --rfc-3339=FMT         output date/time in RFC 3339 format.
                               FMT='date', 'seconds', or 'ns'
                               for date and time to the indicated precision.
                               Example: 2006-08-14 02:34:56-06:00
  -r, --reference=FILE       display the last modification time of FILE
  -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
  %q   quarter of year (1..4)
  %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: <https://www.gnu.org/software/coreutils/>
Full documentation at: <https://www.gnu.org/software/coreutils/date>
or available locally via: info '(coreutils) date invocation'

[root@rocky8 ~]# date -d yesterday
Thu Sep 30 17:04:45 CST 2021

[root@rocky8 ~]# date -d yesterday +%F
2021-09-30

[root@rocky8 ~]# date -d "-1 day" +%F
2021-09-30

[root@rocky8 ~]# date -d "2 day" +%F
2021-10-03

[root@rocky8 ~]# cal -h

Usage:
 cal [options] [[[day] month] year]
 cal [options] <timestamp|monthname>

Display a calendar, or some part of it.
Without any arguments, display the current month.

Options:
 -1, --one             show only a single month (default)
 -3, --three           show three months spanning the date
 -n, --months <num>    show num months starting with date's month
 -S, --span            span the date when displaying multiple months
 -s, --sunday          Sunday as first day of week
 -m, --monday          Monday as first day of week
 -j, --julian          use day-of-year for all calendars
     --reform <val>    Gregorian reform date (1752|gregorian|iso|julian)
     --iso             alias for --reform=iso
 -y, --year            show the whole year
 -Y, --twelve          show the next twelve months
 -w, --week[=<num>]    show US or ISO-8601 week numbers
     --color[=<when>]  colorize messages (auto, always or never)
                         colors are enabled by default

 -h, --help            display this help
 -V, --version         display version

For more details see cal(1).

[root@rocky8 ~]# openssl --help
Invalid command '--help'; type "help" for a list.


[root@rocky8 ~]# date -h
date: invalid option -- 'h'
Try 'date --help' for more information.

[root@rocky8 ~]# shutdown -h
Shutdown scheduled for Wed 2021-10-06 15:09:04 CST, use 'shutdown -c' to cancel.

格式说明:

  • [] 表示可选项
  • CAPS或 <> 表示变化的数据
  • ... 表示一个列表
  • x |y| z 的意思是“ x 或 y 或 z ”
  • -abc的 意思是 -a -b –c
  • { } 表示分组

2.3.1 练习:

1、显示当前时间,格式:2016-06-18 10:20:30

2、显示前天是星期几

3、设置当前日期为2019-08-07 06:05:10

2.4 man 命令

man 提供命令帮助的文件,手册页存放在/usr/share/man

几乎每个命令都有man的“页面”

中文man需安装包

  • man-pages
  • man-pages-zh-CN

man 章节

man 页面分组为不同的“章节”,统称为Linux手册,man 1 man

  • 1:用户命令
  • 2:系统调用
  • 3:C库调用
  • 4:设备文件及特殊文件
  • 5:配置文件格式
  • 6:游戏
  • 7:杂项
  • 8:管理类的命令
  • 9:Linux 内核API

man命令的配置文件:

#CentOS 6 之前版 man 的配置文件
/etc/man.config
#CentOS 7 之后版 man 的配置文件
/etc/man_db.conf
#ubuntu man 的配置文件
/etc/manpath.config

格式:

MANPATH /PATH/TO/SOMEWHERE #指明man文件搜索位置

也可以指定位置下搜索COMMAND命令的手册页并显示

man -M /PATH/TO/SOMEWHERE COMMAND

查看man手册页

man [章节] keyword

man 帮助段落说明

  • NAME 名称及简要说明
  • SYNOPSIS 用法格式说明
  • [] 可选内容
  • <> 必选内容
  • a|b 二选一
  • { } 分组
  • ... 同一内容可出现多次
  • DESCRIPTION 详细说明
  • OPTIONS 选项说明
  • EXAMPLES 示例
  • FILES 相关文件
  • AUTHOR 作者
  • COPYRIGHT 版本信息
  • REPORTING BUGS bug信息
  • SEE ALSO 其它帮助参考

man命令的操作方法:使用less命令实现

  • space, ^v, ^f, ^F: 向文件尾翻屏
  • b, ^b: 向文件首部翻屏
  • d, ^d: 向文件尾部翻半屏
  • u, ^u: 向文件首部翻半屏
  • RETURN, ^N, e, ^E or j or ^J: 向文件尾部翻一行
  • y or ^Y or ^P or k or ^K:向文件首部翻一行
  • q: 退出
  • #:跳转至第#行
  • 1G: 回到文件首部
  • G:翻至文件尾部
  • /KEYWORD 以KEYWORD指定的字符串为关键字,从当前位置向文件尾部搜索;不区分字符大小写 n:下一个 N:上一个
  • ?KEYWORD 以KEYWORD指定的字符串为关键字,从当前位置向文件首部搜索;不区分字符大小写 n:跟搜索命令同方向,下一个 N:跟搜索命令反方向,上一个

man 常用选项

  • 列出所有帮助
man -a keyword
  • 搜索man手册
#列出所有匹配的页面,使用 whatis 数据库
man -k keyword
  • 相当于 whatis
man -f keyword
  • 打印man帮助文件的路径
man -w [章节] keyword

范例:最小化安装时需要安装 man 包

[root@rocky8 ~]# yum install man-pages

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

[root@rocky8 ~]# man 5 passwd

[root@rocky8 ~]# man -w 1 passwd
/usr/share/man/man1/passwd.1.gz

[root@rocky8 ~]# man 1ssl openssl-passwd

[root@rocky8 ~]# man 7 ascii
[root@rocky8 ~]# man 7 utf8

2.4.1 练习:

1、在本机字符终端登录时,除显示原有信息外,再显示当前登录终端号,主机名和当前时间

2、今天18:30自动关机,并提示用户

2.5 info

man常用于命令参考 ,GNU工具 info 适合通用文档参考

没有参数,列出所有的页面

info 页面的结构就像一个网站

每一页分为“节点”

链接节点之前 *

info 命令格式

info [ 命令 ]

导航info页

  • 方向键,PgUp,PgDn 导航
  • Tab键 移动到下一个链接
  • d 显示主题目录
  • Home 显示主题首部
  • Enter进入 选定链接
  • n/p/u/l 进入下/前/上一层/最后一个链接
  • s 文字 文本搜索
  • q 退出 info

2.6 命令自身提供的官方使用指南

/usr/share/doc 目录

多数安装了的软件包的子目录,包括了这些软件的相关原理说明

常见文档:README INSTALL CHANGES

不适合其它地方的文档的位置

配置文件范例

HTML/PDF/PS 格式的文档

授权书详情

2.7 系统及第三方应用官方文档

2.7.1 通过在线文档获取帮助

http://www.github.com https://www.kernel.org/doc/html/latest/ http://httpd.apache.org http://www.nginx.org https://mariadb.com/kb/en https://dev.mysql.com/doc/ http://tomcat.apache.org https://jenkins.io/zh/doc/ https://kubernetes.io/docs/home/ https://docs.openstack.org/train/ http://www.python.org http://php.net

2.7.2 Linux官方在线文档和知识库

通过发行版官方的文档光盘或网站可以获得安装指南、部署指南、虚拟化指南等

https://help.ubuntu.com/ http://kbase.redhat.com http://www.redhat.com/docs http://access.redhat.com https://help.ubuntu.com/lts/serverguide/index.html http://tldp.org

2.7.3 红帽全球技术支持服务

rhn.redhat.com或者本地卫星服务器/代理服务器

RHN账户为及其注册和基于网络管理的RHN用户

sosreport 收集所有系统上的日志信息的工具,并自动打成压缩包,方便技术支持人员和红帽全球支持

提供分析问题依据

范例:

[root@rocky8 ~]# dnf -y install sos
[root@rocky8 ~]# sosreport
Please note the 'sosreport' command has been deprecated in favor of the new 'sos' command, E.G. 'sos report'.
Redirecting to 'sos report '

sosreport (version 4.0)

This command will collect diagnostic and configuration information from
this Rocky system and installed applications.

An archive containing the collected information will be generated in
/var/tmp/sos.37730i48 and may be provided to a Rocky support
representative.

Any information provided to Rocky will be treated in accordance with the
published support policies at:

  https://rockylinux.org/

The generated archive may contain data considered sensitive and its
content should be reviewed by the originating organization before being
passed to any third party.

No changes will be made to system configuration.

Press ENTER to continue, or CTRL-C to quit.

Please enter the case id that you are generating this report for []: 2

 Setting up archive ...
 Setting up plugins ...
[plugin:firewalld] skipped command 'nft list ruleset': required kmods missing: nf_tables, nfnetlink.  Use '--allow-system-changes' to enable collection.
[plugin:networking] skipped command 'ip6tables -t nat -nvL': required kmods missing: nf_tables, ip6table_nat. 
[plugin:networking] skipped command 'ip6tables -t mangle -nvL': required kmods missing: ip6table_mangle, nf_tables. 
[plugin:networking] skipped command 'ip6tables -t filter -nvL': required kmods missing: nf_tables, ip6table_filter. 
[plugin:networking] skipped command 'nft list ruleset': required kmods missing: nf_tables. 
[plugin:networking] skipped command 'ip -s macsec show': required kmods missing: macsec.  Use '--allow-system-changes' to enable collection.
[plugin:networking] skipped command 'ss -peaonmi': required kmods missing: udp_diag, af_packet_diag, netlink_diag, inet_diag, tcp_diag, unix_diag.  Use '--allow-system-changes' to enable collection.
[plugin:networking] skipped command 'iptables -vnxL': required kmods missing: iptable_filter, nf_tables. 
[plugin:networking] skipped command 'ip6tables -vnxL': required kmods missing: nf_tables, ip6table_filter. 
[plugin:systemd] skipped command 'systemd-resolve --status': required services missing: systemd-resolved. 
[plugin:systemd] skipped command 'systemd-resolve --statistics': required services missing: systemd-resolved. 
 Running plugins. Please wait ...

  Finishing plugins              [Running: processor]                                     
  Finished running plugins                                                               
Creating compressed archive...

Your sosreport has been generated and saved in:
	/var/tmp/sosreport-rocky8-2-2021-10-01-qjcljmg.tar.xz

 Size	11.56MiB
 Owner	root
 md5	77bd1bbd4fc642a4a01abca70ce59ac6

Please send this file to your support representative.
[root@rocky8 ~]# ll /var/tmp/sosreport-rocky8-2-2021-10-01-qjcljmg.tar.xz
-rw------- 1 root root 12122240 Oct  1 17:27 /var/tmp/sosreport-rocky8-2-2021-10-01-qjcljmg.tar.xz

2.8 网站和搜索

http://www.google.com

Openstack filetype:pdf
rhca site:redhat.com/docs

http://bing.com http://www.baidu.com https://www.dogedoge.com/ http://www.slideshare.net

在线举报