Linux 基础学习训练教材 - CentOS 8.x

课程问题参考解答

单纯提供一个相对的解答,并不是标准答案!

最近更新时间: 2020/06/11

单纯就是个解答的参考,写完之后再来这边查查看答案跟你想的一样不一样!?

第 02 堂课

  • 例题 2.1.1-1:
    1. 通过 date --help 这个指令,可以查到类似底下的结果:
      [student@station10-101 ~]$ 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.
      ......
      FORMAT controls the output.  Interpreted sequences are:
      ......
        %H   hour (00..23)  <==应该是指 24 小时制
        %I   hour (01..12)  <==应该是指 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   minute (00..59) <==分钟的设置
      ......
        %s   seconds since 1970-01-01 00:00:00 UTC
        %S   second (00..60)
      ......
      [student@station10-101 ~]$ date +%H:%M
      21:52
      
      要注意的是,我们要输出的信息有『小时』与『分钟』,所以需要 %H 与 %M,但是两者中间还有冒号,因此,就得要在两者间加上冒号链接。 否则会出错。
    2. 对照上一题的输出,有个『%s seconds since 1970-01-01 00:00:00 UTC』,代表 %s 是从 1970 年 1 月 1 日到目前为止的累积秒数。 因此『 date +%s 』会一直变化。
      [student@station10-101 ~]$ date +%s
      1583243769
      [student@station10-101 ~]$ date +%s
      1583243786  <==间隔数秒重复运行,会有不同的秒数累积,会持续长大这个数值!
      
    3. 毕竟指令是外国人写的,所以我们得知道两天前的英文是『 2 days ago 』!搭配之前看过得 -d "日期" 格式,所以可以:
      [student@station10-101 ~]$ date
      二  3月  3 21:59:17 CST 2020  <==先显示今天
      [student@station10-101 ~]$ date -d "2 days ago" +%Y/%m/%d
      2020/03/01  <==果然是两天前!
      [student@station10-101 ~]$ date --date="2 days ago" +%Y/%m/%d
      2020/03/01
      
      使用最后一个长选项的指令 (--date="xxx") 也是可以的!
    4. 如同第 A 题,+FORMAT 必须要连续,不能是 %H %M 这样~会失败,如下:
      [student@station10-101 ~]$ date +%H %M
      date: 出现多余的参数 ‘%M’
      Try 'date --help' for more information.
      
      所以,我们得要将『西元年-日-月 小时:分钟』中间的空格去除。如何去除?其实是『让空白字符变一般字符』即可,可以这样: 『西元年-日-月' '小时:分钟』,用单引号将两边黏在一起。因为『 '空白' 』也是『一串字符』,因此就可以将两者连一起。
      [student@station10-101 ~]$ date +%Y-%d-%m' '%H:%M
      2020-03-03 22:04
      
  • 例题 2.1.1-2:
    1. 通过 cal --help 得到如下的说明:
      [student@station10-101 ~]$ cal --help
      
      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.
      ......
      
      意思是说,显示月历 (calendar),若不加参数,则会显示目前这个月的月历。
    2. 直接下达指令即可:
      [student@station10-101 ~]$ cal
            三月 2020
      日 一 二 三 四 五 六
       1  2  3  4  5  6  7
       8  9 10 11 12 13 14
      15 16 17 18 19 20 21
      22 23 24 25 26 27 28
      29 30 31
      
      你会发现到,目前的日期还会反白!
    3. 比较有趣的地方是语法,如果后面要加 day, month, year 的话,年一定,然后是月,然后是日。所以,今年 (2020) 一整年的年历:
      [student@station10-101 ~]$ cal 2020
                                     2020
      
              一月                   二月                   三月
      日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
                1  2  3  4                      1    1  2  3  4  5  6  7
       5  6  7  8  9 10 11    2  3  4  5  6  7  8    8  9 10 11 12 13 14
      12 13 14 15 16 17 18    9 10 11 12 13 14 15   15 16 17 18 19 20 21
      19 20 21 22 23 24 25   16 17 18 19 20 21 22   22 23 24 25 26 27 28
      26 27 28 29 30 31      23 24 25 26 27 28 29   29 30 31
      ......
              十月                  十一月                 十二月
      日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
                   1  2  3    1  2  3  4  5  6  7          1  2  3  4  5
       4  5  6  7  8  9 10    8  9 10 11 12 13 14    6  7  8  9 10 11 12
      11 12 13 14 15 16 17   15 16 17 18 19 20 21   13 14 15 16 17 18 19
      18 19 20 21 22 23 24   22 23 24 25 26 27 28   20 21 22 23 24 25 26
      25 26 27 28 29 30 31   29 30                  27 28 29 30 31
      
    4. 查找 cal --help 的参数部份,会看到这句话:
      [student@station10-101 ~]$ cal --help
      ......
      选项:
       -1, --one             show only a single month (default)
       -3, --three           show three months spanning the date
      ......
      
      [student@station10-101 ~]$ cal -3
            二月 2020             三月 2020             四月 2020
      日 一 二 三 四 五 六  日 一 二 三 四 五 六  日 一 二 三 四 五 六
                         1   1  2  3  4  5  6  7            1  2  3  4
       2  3  4  5  6  7  8   8  9 10 11 12 13 14   5  6  7  8  9 10 11
       9 10 11 12 13 14 15  15 16 17 18 19 20 21  12 13 14 15 16 17 18
      16 17 18 19 20 21 22  22 23 24 25 26 27 28  19 20 21 22 23 24 25
      23 24 25 26 27 28 29  29 30 31              26 27 28 29 30
      
      这样就能显示出前/目前/后一个月的月历了!
  • 例题 2.1.2-1:
    1. 因为使用『 su - 』时,你会取得全新的 root 身份,与你原本的 student 并不相同,因此,history 的历史命令,也会跟改成为 root 的历史命令。 所以,此时方向键与 history 看到的,就会是 root 的历史命令,而不是 student 的历史命令了。
    2. 就是注销!可以是 exit, logout, [ctrl]+d 都可以。
  • 例题 2.1.4-1:
    1. 可以直接输入 if 或 ls 后面加上两次 [tab] 按钮即可!这个称为命令补齐功能,如下所示:
      # 底下指令中的 [tab] 代表键盘左侧的 Tab 按钮,光标请连着 f 不要有空白键,按下两次
      [student@station10-101 ~]$ if[tab][tab]
      if         ifcfg      ifconfig   ifdown     ifenslave  ifstat     ifup
      [student@station10-101 ~]$ if
      # 如上,过程中都没有按下 [enter],屏幕会输出很多以 if 为开头的『指令名称』
      
      [student@station10-101 ~]$ ls[tab][tab]
      ls        lsblk     lsgpio    lsiio     lsipc     lslogins  lsmd      lsmod     lsof      lsscsi    lsusb.py
      lsattr    lscpu     lshw      lsinitrd  lslocks   lsmcli    lsmem     lsns      lspci     lsusb
      [student@station10-101 ~]$ ls[tab][tab]
      
    2. 如上题的情境,查看一下,由于 ifco 只有一个指令名称的开头是这样,因此,按下一次 [tab] 就补齐了指令名称,如下:
      [student@station10-101 ~]$ ifco[tab]
      [student@station10-101 ~]$ ifconfig
      
      上面的动作都没有按下 [enter] 按键,第一行按下 [tab] 之后,会立刻变成第二行的型态!这就是命令补齐
    3. 参考热键组合的清单,有个 [ctrl]+c 的中断功能,底下的指令过程中,你会发现屏幕真的很乱~没关系,在很乱的显示过程中, 你随时可以按下 [ctrl]+c 来中断它即可!
      [student@station10-101 ~]$ find /
      ......(很乱很乱,一直输出文件名)
      /proc/2434/net/^C
      [student@station10-101 ~]$ ^C
      # [ctrl]+c 的代表符号就是『^C』,我按了两次,所以有一次就会出现在指令列上面!
      
    4. 因为单引号必需要成对,才会是『一组文本字符串数据』的型态,如果只有单一一个单引号,他就会让你持续不断的填写该段文本字符串的内容。 所以,无论你按下多少次 [enter],该指令都不会运行或中断。此时,你依旧可以按下 [enter] 来中断这次的指令输入,该指令并不会被运行!
      [student@station10-101 ~]$ ls ' /
      > /home
      > /var
      > ^C  <==最后按下 [ctrl]+c 就中断这次的输入!
      
    5. 当 [tab] 按钮不是接在指令的位置上,而是接在类似文件名的位置上,那就可以变成文件名补齐!如下所示:
      [student@station10-101 ~]$ ll -d /etc/se[tab][tab]
      security/       selinux/        services        sestatus.conf   setroubleshoot/
      # 你会发现到, /etc 底下以 se 开头的有上述几个文件名。
      # 现在假设我需要 security 这个文件名,所以多加一个 c 再 [tab]
      
      [student@station10-101 ~]$ ll -d /etc/sec[tab]   <==多加一个 c 再 [tab],就会补齐成底下这样
      [student@station10-101 ~]$ ll -d /etc/security/
      drwxr-xr-x. 7 root root 4096  2月 26 09:03 /etc/security/
      
    6. 有趣的是, [tab] 也能够作为变量名称的补齐喔!如题目所示,我们需要知道 H 开头的变量有多少,再加上前面提到过,调用变量内容要加上钱字号, 所以就会变成如下的模样:
      [student@station10-101 ~]$ echo ${H[tab][tab]
      ${HISTCMD}       ${HISTCONTROL}   ${HISTFILE}      ${HISTFILESIZE}  
      ${HISTSIZE}      ${HOME}          ${HOSTNAME}      ${HOSTTYPE}
      
      上述过程中都没有按下 [enter] 喔!所以 H 开头的变量有上面这 8 个呢!再也不会打错字了!
  • 例题 2.1.5-1:
    1. 由刚刚的 man page 里面,我们知道可以使用 scale = NN 来设置小数点位数,题目要求需要 4 位数,因此可以这样做:
      [student@station10-101 ~]$ bc
      bc 1.07.1
      Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
      This is free software with ABSOLUTELY NO WARRANTY.
      For details type `warranty'.
      1/3
      0
      scale=4
      1/3
      .3333
      quite
      
      这样就取得了 4 位数小数点的输出了。
    2. 直接 man bc 之后,使用 /pi= 去找到底下的信息:
      [student@station10-101 ~]$ man bc
      
         EXAMPLES
             In  /bin/sh,   the following will assign the value of "pi" to the shell variable
             pi.
                     pi=$(echo "scale=10; 4*a(1)" | bc -l)
      
             The following is the definition of the exponential function  used  in  the  math
             library.  This function is written in POSIX bc.
                    scale = 20
      ......
      
      /pi=  <==直接按下斜线 / 之后,光标就会主动跑到最左下角了!
      
      你可以找到上面这行奇怪的东西,其中那个 scale 缺省使用了 10 位数,我们需要的是 50 位数,所以运行方法改成这样:
      [student@station10-101 ~]$ echo "scale=50; 4*a(1)" | bc -l
      3.14159265358979323846264338327950288419716939937508
      [student@station10-101 ~]$
      
      上面就可以算出圆周率 pi 啰!因为我们要直接运算,不需要设置变量,所以直接处理成上面这样啰!
    3. 余数的英文是 remainder,直接 man bc 找 remainder 关键字,可以发现这样:
      [student@station10-101 ~]$ man bc
      ......
             expr % expr
                    The result of the expression is the "remainder" and it is computed in the
                    following way.  To compute a%b, first a/b is computed  to  scale  digits.
                    That  result  is used to compute a-(a/b)*b to the scale of the maximum of
                    scale+scale(b) and scale(a).  If scale is set to zero  and  both  expres‐
                    sions are integers this expression is the integer remainder function.
      
             expr ^ expr
                    The result of the expression is the value of the first raised to the sec‐
                    ond. The second expression must be an integer.  (If the second expression
      ......
      /remainder  <==输入 / 找寻,找到之后按下方向键↑几次,就会发现如上模样
      
      我们可以发现到,要算余数,就用百分比,也就是题目中的,用 1000 % 17 来算!
      [student@station10-101 ~]$ bc
      bc 1.07.1
      Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
      This is free software with ABSOLUTELY NO WARRANTY.
      For details type `warranty'.
      1000 / 17
      58
      1000 % 17
      14
      58*17+14
      1000
      quit
      
      得到商数为 58,得到余数为 14,亦即答案应该是:『 1000 = 58*17 + 14 』啰!
    4. 要找到 date 的说明文档范本,使用『 man date 』之后,出入『 /examples 』就能找到!如下:
      [student@station10-101 ~]$ man date
      ......
      EXAMPLES
             Convert seconds since the epoch (1970-01-01 UTC) to a date
      
                    $ date --date='@2147483647'
      ......
      
      /examples
      
      意思是,由 +%s 得到的总秒数,将他转回日期的方法!例如底下的实验:
      # 先找出目前的总秒数
      [student@station10-101 ~]$ date +%s
      1583289427
      
      # 将上述总秒数改换成为人类的日期,就是这样:
      [student@station10-101 ~]$ date --date="@1583289427"
      三  3月  4 10:37:07 CST 2020
      
      这两者之间的切换这样!这在许多脚本要确认日期时,是很有帮助的功能!
  • 例题 2.1.6-1:
    1. 先来看一下,如果没有加上 more 与 less 的情况下,列出 ll /etc 的数据是怎样?
      [student@station10-101 ~]$ ll /etc
      ......
      drwxr-xr-x.  3 root root        36  2月 26 09:03 xml
      drwxr-xr-x.  2 root root        57  2月 26 09:06 yum
      lrwxrwxrwx.  1 root root        12 11月 12 23:35 yum.conf -> dnf/dnf.conf
      drwxr-xr-x.  2 root root      4096  2月 26 09:02 yum.repos.d
      
      实在很多数据,得用 [shift]+[pageup] 去查阅。现在通过 more 来翻动看看:
      [student@station10-101 ~]$ ll /etc | more
      总计 1360
      -rw-r--r--.  1 root root        44  3月  4 00:06 adjtime
      -rw-r--r--.  1 root root      1518  9月 10  2018 aliases
      drwxr-xr-x.  3 root root        65  2月 26 09:06 alsa
      drwxr-xr-x.  2 root root      4096  2月 26 09:08 alternatives
      -rw-r--r--.  1 root root       541 11月  9 00:47 anacrontab
      -rw-r--r--.  1 root root        55 11月  9 00:21 asound.conf
      -rw-r--r--.  1 root root         1  5月 11  2019 at.deny
      drwxr-x---.  4 root root       100  2月 26 09:15 audit
      drwxr-xr-x.  3 root root       228  2月 26 09:10 authselect
      drwxr-xr-x.  4 root root        71  2月 26 09:06 avahi
      drwxr-xr-x.  2 root root       136  2月 26 09:06 bash_completion.d
      -rw-r--r--.  1 root root      3001  9月 10  2018 bashrc
      -rw-r--r--.  1 root root       429 11月  9 04:35 bindresvport.blacklist
      drwxr-xr-x.  2 root root         6 11月  9 07:14 binfmt.d
      drwxr-xr-x.  2 root root        23  2月 26 09:03 bluetooth
      -rw-r-----.  1 root brlapi      33  2月 26 09:05 brlapi.key
      --更多--
      # 此时,你可以按下 [enter] 或空白按键,或者是 q 离开,也能按下 / 搜索关键字!
      
      more 的功能是一页一页翻动,由于不是文件,因此你无法使用方向键向上查找,只能持续往下找寻数据而已。 最终数据显示完毕后,more 也就运行结束。那如果使用 less 呢?
      [student@station10-101 ~]$ ll /etc | less
      总计 1360
      -rw-r--r--.  1 root root        44  3月  4 00:06 adjtime
      -rw-r--r--.  1 root root      1518  9月 10  2018 aliases
      drwxr-xr-x.  3 root root        65  2月 26 09:06 alsa
      drwxr-xr-x.  2 root root      4096  2月 26 09:08 alternatives
      -rw-r--r--.  1 root root       541 11月  9 00:47 anacrontab
      -rw-r--r--.  1 root root        55 11月  9 00:21 asound.conf
      -rw-r--r--.  1 root root         1  5月 11  2019 at.deny
      drwxr-x---.  4 root root       100  2月 26 09:15 audit
      drwxr-xr-x.  3 root root       228  2月 26 09:10 authselect
      drwxr-xr-x.  4 root root        71  2月 26 09:06 avahi
      drwxr-xr-x.  2 root root       136  2月 26 09:06 bash_completion.d
      -rw-r--r--.  1 root root      3001  9月 10  2018 bashrc
      -rw-r--r--.  1 root root       429 11月  9 04:35 bindresvport.blacklist
      drwxr-xr-x.  2 root root         6 11月  9 07:14 binfmt.d
      drwxr-xr-x.  2 root root        23  2月 26 09:03 bluetooth
      -rw-r-----.  1 root brlapi      33  2月 26 09:05 brlapi.key
      :    <==光标停在这里,几乎与 man page 相同!
      
      事实上, man page 的环境,就是调用 less 来达成的!所以,你也可以在 less 底下使用 man page 相同的指令来翻动与查找关键字!
    2. 若要查找数据内的关键字,可以直接使用 /keyword 查找!我们只用 less 来示意:
      [student@station10-101 ~]$ ll /etc | less
      -rw-r--r--.  1 root root      2433  2月 26 09:10 passwd
      -rw-r--r--.  1 root root      2381  2月 26 09:06 passwd-
      -rw-r--r--.  1 root root      1362  5月 14  2019 pbm2ppa.conf
      -rw-r--r--.  1 root root      2872  5月 14  2019 pinforc
      drwxr-xr-x.  2 root root        27  2月 26 09:05 pipewire
      drwxr-xr-x.  3 root root        21  2月 26 09:02 pkcs11
      drwxr-xr-x. 10 root root       123  2月 26 09:06 pki
      drwxr-xr-x.  2 root root        28  2月 26 09:05 plymouth
      ......
      /passwd
      
    3. 使用 find 可以打印文件名,在找寻文件时,很有用!但是就是数据量太大~查找起来不容易。此时,就可以通过 less 的协助:
      [student@station10-101 ~]$ find /etc | less
      /etc/dnf/vars/infra
      /etc/dnf/aliases.d
      /etc/dnf/dnf.conf
      /etc/dnf/modules.defaults.d
      /etc/dnf/plugins
      /etc/dnf/plugins/copr.conf
      /etc/dnf/plugins/copr.d
      ......
      find: ‘/etc/audit’: 拒绝不符权限的操作
      find: ‘/etc/libvirt’: 拒绝不符权限的操作
      find: ‘/etc/firewalld’: 拒绝不符权限的操作
      find: ‘/etc/sudoers.d’: 拒绝不符权限的操作
      :  
      
    4. 因为我们用 student 一般帐号操作系统,因此有些在 /etc 底下的特权文件我们就没有权限可以查阅,因此出现错误消息也是合理的! 按下任意按钮,该错误消息就会被洗掉了!
    5. 我们知道可以通过 bc 来运算加减乘除,此时通过 echo "365*24*60*60" 这个算式,来提供给 bc 计算:
      [student@station10-101 ~]$ echo "365*24*60*60" | bc
      31536000
      
      一年共有 31,536,000 秒喔!
  • 例题 2.1.6-2:
    1. 简单输入 ifconfig 指令即可:
      [student@station10-101 ~]$ ifconfig
      ens3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
              inet 172.16.10.101  netmask 255.255.0.0  broadcast 172.16.255.255
              inet6 fe80::5718:f7d5:ab79:9643  prefixlen 64  scopeid 0x20<link>
              ether 52:54:00:85:b1:54  txqueuelen 1000  (Ethernet)
              RX packets 9354  bytes 755581 (737.8 KiB)
              RX errors 0  dropped 0  overruns 0  frame 0
              TX packets 16824  bytes 4387726 (4.1 MiB)
              TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
      
      lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
              inet 127.0.0.1  netmask 255.0.0.0
              inet6 ::1  prefixlen 128  scopeid 0x10
              loop  txqueuelen 1000  (Local Loopback)
              RX packets 86  bytes 7046 (6.8 KiB)
              RX errors 0  dropped 0  overruns 0  frame 0
              TX packets 86  bytes 7046 (6.8 KiB)
              TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
      ......
      
    2. 如上所示,我们其实比较需要了解的仅有『 inet ... 』那一行特殊关键字就是 inet 啰!所以可以这样做:
      [student@station10-101 ~]$ ifconfig | grep 'inet '
              inet 172.16.10.101  netmask 255.255.0.0  broadcast 172.16.255.255
              inet 127.0.0.1  netmask 255.0.0.0
              inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
      
      这样就仅列出 IP 而已!画面清爽非常多!
  • 例题 2.2.1-1:
    1. 直接使用 ll / 来看看:
      [student@station10-101 ~]$ ll /
      总计 24
      lrwxrwxrwx.   1 root root    7  5月 11  2019 bin -> usr/bin
      dr-xr-xr-x.   6 root root 4096  2月 26 09:15 boot
      drwxr-xr-x.  20 root root 3260  3月  3 15:47 dev
      ......
      
      最左侧的第一个字符,就是这个文件名的类型,基本上有三种类型一定要先知道:
      • - :一般文件
      • d :目录文件
      • l :链接文件
    2. 同样直接查一下数据:
      [student@station10-101 ~]$ ll -d /sys /proc
      dr-xr-xr-x. 233 root root 0  3月  3 15:47 /proc
      dr-xr-xr-x.  13 root root 0  3月  3 15:47 /sys
      
      容量竟然是 0 !这是因为这两个目录是从内存里面挂载出来的,所以没有用到实体硬盘容量,而是占用了内存容量喔! 因此硬盘使用量就是 0。
    3. 直接使用 ll 去寻找一下,同时搭配通配符星号 (*) 来检查文件名:
      [student@station10-101 ~]$ ll -h /boot/vmlinuz*
      -rwxr-xr-x. 1 root root 7.8M  2月 26 09:08 /boot/vmlinuz-0-rescue-502dbaaf2a074134909a59ef9ab651c1
      -rwxr-xr-x. 1 root root 7.8M 12月  5 05:58 /boot/vmlinuz-4.18.0-147.el8.x86_64
      
      ll 加上 -h 可以直接用人类易读的单位来处理,所以可以看到 vmlinuz-4.xxx 为 7.8M,这就是操作系统内核文件,容量相当小! 这个文件就可以驱动整个 x86 的硬件设备喔!
    4. 直接用 man 来看看:
      [student@station10-101 ~]$ man ls
      LS(1)                                User Commands                                LS(1)
      
      NAME
             ls - list directory contents
      ......
      
      [student@station10-101 ~]$ man ifconfig
      IFCONFIG(8)               Linux System Administrator's Manual               IFCONFIG(8)
      
      NAME
             ifconfig - configure a network interface
      ......
      
      一个是 ls(1),一个是 ifconfig(8),所以,我们猜文件名应该就是 /usr/bin/ls 以及 /usr/sbin/ifconfig,来检查看看:
      [student@station10-101 ~]$ ll /usr/bin/ls /usr/sbin/ifconfig
      -rwxr-xr-x. 1 root root 166448  5月 12  2019 /usr/bin/ls
      -rwxr-xr-x. 1 root root  97920  5月 11  2019 /usr/sbin/ifconfig
      
    5. 你可以将文件放置到 /dev/shm 底下去喔!该目录是内存仿真出来的文件系统,直接将文件放置到内存的意思,所以速度飞快! 但是一关机就不见了!要很注意!
      [student@station10-101 ~]$ df
      文件系统                 1K-区段    已用    可用 已用% 挂载点
      devtmpfs                  918108       0  918108    0% /dev
      tmpfs                     936140       0  936140    0% /dev/shm
      tmpfs                     936140    9388  926752    2% /run
      tmpfs                     936140       0  936140    0% /sys/fs/cgroup
      /dev/mapper/centos-root 10475520 4359596 6115924   42% /
      /dev/vda2                1998672  149448 1727984    8% /boot
      /dev/mapper/centos-home  3135488   70240 3065248    3% /home
      tmpfs                     187228      16  187212    1% /run/user/42
      tmpfs                     187228       4  187224    1% /run/user/0
      tmpfs                     187228       4  187224    1% /run/user/1000
      
      [student@station10-101 ~]$ ll /dev/shm
      总计 0
      
  • 例题 2.2.2-1:
    1. 直接使用 cd 搭配绝对路径去切换工作目录即可:
      [student@station10-101 ~]$ cd /var/spool/mail
      [student@station10-101 mail]$ pwd
      /var/spool/mail
      # 工作目录确实是切换到我们需要的目录了!
      
    2. 分析一下, /var/spool/mail 的上层就是 /var/spool ,但是我们已经在 /var/spool/mail 里面, 所以,可以有两个方式来达成上述的功能,底下分别运行以绝对路径、相对路径文件名来展示:
      [student@station10-101 mail]$ ll /var/spool/
      总计 0
      drwxr-xr-x. 2 root root 63  2月 26 09:04 anacron
      drwx------. 3 root root 31  2月 26 09:06 at
      drwx------. 2 root root  6 11月  9 00:47 cron
      drwx--x---. 3 root lp   17  2月 26 09:05 cups
      drwxr-xr-x. 2 root root  6  5月 11  2019 lpd
      drwxrwxr-x. 2 root mail 32  2月 26 09:10 mail
      drwxr-xr-x. 2 root root  6 11月 22 01:36 plymouth
      drwx------. 2 root root  6 11月 14 12:16 up2date
      
      [student@station10-101 mail]$ ll ..
      总计 0
      drwxr-xr-x. 2 root root 63  2月 26 09:04 anacron
      drwx------. 3 root root 31  2月 26 09:06 at
      drwx------. 2 root root  6 11月  9 00:47 cron
      drwx--x---. 3 root lp   17  2月 26 09:05 cups
      drwxr-xr-x. 2 root root  6  5月 11  2019 lpd
      drwxrwxr-x. 2 root mail 32  2月 26 09:10 mail
      drwxr-xr-x. 2 root root  6 11月 22 01:36 plymouth
      drwx------. 2 root root  6 11月 14 12:16 up2date
      # 输出的结果会是一模一样的!
      
      [student@station10-101 mail]$ ll -d ../ana[tab]
      [student@station10-101 mail]$ ll -d ../anacron/
      drwxr-xr-x. 2 root root 63  2月 26 09:04 ../anacron/
      # 当然使用相对路径要打字的情况会减少,同时再以 [tab] 去补齐文件名的结果!!
      
    3. 当然使用相对路径切换会比较少打字!
      [student@station10-101 mail]$ cd ../anacron/
      [student@station10-101 anacron]$ pwd
      /var/spool/anacron
      
    4. 我们目前在 /var/spool/anacron 底下,与需要切换的目录互相参考相对位置,会是这样:
      • /var/spool/anacron/
      • /var/log/
      所以可以这样运行看看:
      [student@station10-101 anacron]$ ll /var/log | head -n 5
      总计 2892
      drwxr-xr-x. 2 root   root       280  2月 26 09:10 anaconda
      drwx------. 2 root   root        23  2月 26 09:15 audit
      -rw-------. 1 root   root         0  3月  3 16:35 boot.log
      -rw-------. 1 root   root      1920  3月  1 16:14 boot.log-20200301
      
      [student@station10-101 anacron]$ ll ../../log | head -n 5
      总计 2892
      drwxr-xr-x. 2 root   root       280  2月 26 09:10 anaconda
      drwx------. 2 root   root        23  2月 26 09:15 audit
      -rw-------. 1 root   root         0  3月  3 16:35 boot.log
      -rw-------. 1 root   root      1920  3月  1 16:14 boot.log-20200301
      # 这个案例中,从根目录写起的绝对路径文件名,又比相对路径文件名来的简单好写!
      
    5. 回家目录有两种写法,基本上,直接按下 cd 后面不加任何参数,就可以回到自己的家目录了!
      [student@station10-101 anacron]$ cd
      [student@station10-101 ~]$ pwd
      /home/student
      [student@station10-101 ~]$ cd ~
      
    6. 要运行一个指令有非常多的方法,如果是正规的指令,可以直接输入指令名称,即可开始运行。 如果是非正规的指令,可能得要使用绝对路径文件名,或相对路径文件名的方式,才能够运行的。假设我们已经知道 ifconfig 指令, 完整的文件名为 /usr/sbin/ifconfig,那么接下来可以这样做:

      # 使用缺省的方式来运行正规指令:
      [student@station10-101 ~]$ ifconfig
      ens3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
              inet 172.16.10.101  netmask 255.255.0.0  broadcast 172.16.255.255
      ......
      
      # 使用绝对路径的方式来运行指令:
      [student@station10-101 ~]$ /usr/sbin/ifconfig
      ens3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
              inet 172.16.10.101  netmask 255.255.0.0  broadcast 172.16.255.255
      
      # 由于我们在 /home/student 底下,所以,直接使用相对路径就成为:
      [student@station10-101 ~]$ ../../usr/sbin/ifconfig
      ens3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
              inet 172.16.10.101  netmask 255.255.0.0  broadcast 172.16.255.255
      
      # 切换工作目录到 /usr/sbin 底下去,就变这样:
      [student@station10-101 ~]$ cd /usr/sbin
      [student@station10-101 sbin]$ pwd
      /usr/sbin
      [student@station10-101 sbin]$ ./ifconfig
      
      最后一个工作目录底下运行该指令的方法,未来在你出外工作后,会是很常看到的运行非正规指令的方法喔!
  • 例题 2.2.3-1:
    1. 使用 ll 来查看文件名即可:
      [student@station10-101 backup]$ ll /dev/shm/backup/etc/passwd*
      -rw-r--r--. 1 student student 2433  3月  4 13:22 /dev/shm/backup/etc/passwd
      -rw-r--r--. 1 student student 2381  3月  4 13:22 /dev/shm/backup/etc/passwd-
      
      确实有存在两个 passwd 为开头的文件名!
    2. 使用 rm --help 可以查看用法,然后尝试删除一下:
      [student@station10-101 backup]$ rm --help
      用法:rm [选项]... [文件]...
      Remove (unlink) the FILE(s).
      
      [student@station10-101 backup]$ rm /dev/shm/backup/etc/passwd*
      # 有点可怕的是,一般身份用户删除文件时,系统不会提醒你!直接就删除了!
      
      [student@station10-101 backup]$ ll /dev/shm/backup/etc/passwd*
      ls: 无法访问 '/dev/shm/backup/etc/passwd*': 没有此一文件或目录
      
    3. 可以使用 ll -d 去查阅即可!
      [student@station10-101 backup]$ ll -d /dev/shm/backup/etc/X11
      drwxr-xr-x. 7 student student 180  3月  4 13:21 /dev/shm/backup/etc/X11
      # 最左边是 d ,所以是个目录文件
      
    4. 删除直接使用 rm 吧!但是可能删除不掉,需要考虑 rm --help 找寻问题!
      [student@station10-101 backup]$ rm /dev/shm/backup/etc/X11
      rm: 无法移除 '/dev/shm/backup/etc/X11': 是个目录
      
      [student@station10-101 backup]$ man rm
      ......
             -r, -R, --recursive
                    remove directories and their contents recursively
      ......
      
      [student@station10-101 backup]$ rm -r /dev/shm/backup/etc/X11
      [student@station10-101 backup]$ ll -d /dev/shm/backup/etc/X11
      ls: 无法访问 '/dev/shm/backup/etc/X11': 没有此一文件或目录
      
    5. 不询问直接删除,确实很可怕!如果我需要交互 (interactive) 来询问,让我确认才删除,要怎么处理?
      [student@station10-101 backup]$ man rm
             -i     prompt before every removal
      
             -I     prompt once before removing more  than  three  files,  or  when  removing
                    recursively;  less  intrusive  than  -i,  while  still  giving protection
                    against most mistakes
      
             --interactive[=WHEN]
                    prompt according to WHEN: never, once (-I), or always (-i); without WHEN,
                    prompt always
      
      由上面可以知道:
      • -i 每个文件移除前都询问
      • -I 只询问第一次,之后依据询问的回应持续进行动作
      • --interactive=[never|once|always] 不问、问一次、每次问
      目前想要每次都询问,所以可以这样做:
      [student@station10-101 backup]$ rm -ri /dev/shm/backup/etc/xdg/
      rm: descend into directory '/dev/shm/backup/etc/xdg/'? y
      rm: descend into directory '/dev/shm/backup/etc/xdg/menus'? y
      rm:是否移除普通文件'/dev/shm/backup/etc/xdg/menus/gnome-applications.menu'? y
      ^C  <==每次都要回答 y,很烦~所以直接 [ctrl]+c 中断
      
      [student@station10-101 backup]$ rm -rI /dev/shm/backup/etc/xdg/
      rm: remove 1 argument recursively? y
      # 只问第一次,确定就全部都删除了!
      
  • 例题 2.3 课后作业:
    1. 直接查看
      [student@station10-101 ~]$ locale
      LANG=zh_TW.utf8     <==是这一个
      LC_CTYPE="zh_TW.utf8"
      LC_NUMERIC="zh_TW.utf8"
      LC_TIME="zh_TW.utf8"
      LC_COLLATE="zh_TW.utf8"
      LC_MONETARY="zh_TW.utf8"
      LC_MESSAGES="zh_TW.utf8"
      LC_PAPER="zh_TW.utf8"
      LC_NAME="zh_TW.utf8"
      LC_ADDRESS="zh_TW.utf8"
      LC_TELEPHONE="zh_TW.utf8"
      LC_MEASUREMENT="zh_TW.utf8"
      LC_IDENTIFICATION="zh_TW.utf8"
      LC_ALL=
      
      [student@station10-101 ~]$ LANG=C
      [student@station10-101 ~]$ locale
      LANG=C              <==变成这个
      LC_CTYPE="C"
      LC_NUMERIC="C"
      LC_TIME="C"
      ......
      
    2. 由于时间的展示,可以是这样:『2020/02/09 00:00 GMT』,所以可以变成这样显示秒数累积:
      [student@station10-101 ~]$ date -d "2020/02/09 00:00 GMT" +%s
      1581206400
      
    3. 上面显示的是秒数,这一题要求的是日数,一天总共有 24*60*60 秒,因为 1970/1/1 是第一天,所以要加上 1 天的日数!如果通过 bc 来处理, 基本上应该是这样的算式:
      [student@station10-101 ~]$ echo "1581206400/(24*60*60)+1" | bc
      18302
      
    4. 现在看到的日期累加总数竟然是 18310 的话,简单的来说,先计算出总秒数 (记得天数要先 -1),然后再带入 date 去计算吧!
      [student@station10-101 ~]$ echo "(18310-1)*24*60*60" | bc
      1581897600
      [student@station10-101 ~]$ date -d "@1581897600"
      Mon Feb 17 08:00:00 CST 2020
      
    5. 星期几可以通过 cal 简单的判断,所以这样进行即可:
      [student@station10-101 ~]$ cal 10 10 2020
          October 2020
      Su Mo Tu We Th Fr Sa
                   1  2  3
       4  5  6  7  8  9 10
      11 12 13 14 15 16 17
      18 19 20 21 22 23 24
      25 26 27 28 29 30 31
      
    6. 其实可以通过 echo 加上 [tab] 来测试调用看看:
      [student@station10-101 ~]$ echo ${HIST[tab][tab]
      ${HISTCMD}       ${HISTCONTROL}   ${HISTFILESIZE}  ${HISTFILE}      ${HISTSIZE}
      
      [student@station10-101 ~]$ echo ${HISTSIZE}
      1000
      
      所以得到可以记忆 1000 笔纪录的设置值
    7. 底下将直接进行所有的任务:
      # 变换工作目录
      [student@station10-101 ~]$ cd /dev/shm
      [student@station10-101 shm]$ pwd
      /dev/shm
      
      # 创建目录
      [student@station10-101 shm]$ mkdir check
      [student@station10-101 shm]$ ll
      total 0
      drwxrwxr-x. 3 student student 60 Mar  4 13:21 backup
      drwxrwxr-x. 2 student student 40 Mar  4 15:23 check
      
      # 切换工作目录
      [student@station10-101 shm]$ cd check
      
      # 拷贝目录的方法
      [student@station10-101 check]$ cp /etc/X11 .
      cp: -r not specified; omitting directory '/etc/X11'
      [student@station10-101 check]$ cp -r /etc/X11 .
      [student@station10-101 check]$ ll
      total 0
      drwxr-xr-x. 7 student student 180 Mar  4 15:24 X11
      
      # 删除目录的方法
      [student@station10-101 check]$ ll -d ./X11/xinit
      drwxr-xr-x. 5 student student 200 Mar  4 15:24 ./X11/xinit
      [student@station10-101 check]$ rm ./X11/xinit
      rm: cannot remove './X11/xinit': Is a directory
      [student@station10-101 check]$ rm -r ./X11/xinit
      
      # 配合管线,抓到关键字文件名,然后进一步拷贝
      [student@station10-101 check]$ find /etc | grep group 2> /dev/null
      /etc/security/group.conf
      /etc/group
      /etc/group-
      /etc/iproute2/group
      [student@station10-101 check]$ cp /etc/security/group.conf /etc/group /etc/group- /etc/iproute2/group .
      cp: will not overwrite just-created './group' with '/etc/iproute2/group'
      
      [student@station10-101 check]$ ll
      total 12
      drwxr-xr-x. 6 student student  160 Mar  4 15:24 X11
      -rw-r--r--. 1 student student  936 Mar  4 15:25 group
      -rw-r--r--. 1 student student  920 Mar  4 15:25 group-
      -rw-r--r--. 1 student student 3635 Mar  4 15:25 group.conf
      
      [student@station10-101 check]$ ll -R
      # 这样才能够看到 X11 底下的子目录!
      
修改历史:
  • 2020/02/25:尝试直接在课程中加入解答,让大家有个参考依据。不过,没事不要来看啊!
  • 2020/03/10:加入了第三章的后课练习部份。
2020/03/02 以来统计人数
计数器
其他链接
环境工程模式篇
鸟园讨论区
鸟哥旧站

今日 人数统计
昨日 人数统计
本月 人数统计
上月 人数统计