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 以來統計人數
計數器
其他連結
環境工程模式篇
鳥園討論區
鳥哥舊站

今日 人數統計
昨日 人數統計
本月 人數統計
上月 人數統計