7月17日上課 簡單命令、命令歷史(history)、如何獲取幫助、檔案管理

weixin_34007886發表於2017-07-17

一、簡單命令

1、date命令

  • 顯示和修改時間
[root@centos6 ~]#date                     ---顯示時間
Mon Jul 17 20:23:54 CST 2017                 
[root@centos6 ~]#date 071720242017.30     ---修改時間,時間格式為月日小時分年.秒
Mon Jul 17 20:24:30 CST 2017
[root@centos6 ~]#date "+%F %T"             ---顯示目前時間格式
2017-07-17 20:47:36
[root@centos6 ~]#date -d -10day +%A        ---顯示10天前是星期幾
Friday
  • 和伺服器172.18.0.1時間同步
[root@centos6 ~]#ntpdate 172.18.0.1
17 Jul 20:31:33 ntpdate[7744]: step time server 172.18.0.1 offset 37.359560 sec
  • 顯示硬體時間
[root@centos6 ~]#clock
Mon 17 Jul 2017 08:33:35 PM CST  -0.799711 seconds

選項

-w 以系統時間為準
-s 以硬體時間為準
  • 顯示日曆
[root@centos6 ~]#cal
      July 2017     
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
[root@centos6 ~]#cal 08 2008---顯示某年某月日曆
     August 2008    
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
  • 調整時區
[root@centos7 ~]#date
Mon Jul 17 20:57:21 CST 2017  ---目前時區
[root@centos7 ~]#timedatectl list-timezones ---檢視時區
[root@centos7 ~]#timedatectl set-timezone Africa/Abidjan---修改時區
[root@centos7 ~]#date
Mon Jul 17 12:55:19 GMT 2017---時區改變

2、shutdown命令

用法:shutdown [OPTION]... TIME [MESSAGE]
-r: reboot
-h: halt
-c:cancel
TIME:無指定,預設相當於+1
now: 立刻,相當於+0
+m: 相對時間表示法,幾分鐘之後;例如+3
hh:mm: 絕對時間表示,指明具體時間

  • 設定3分鐘之後關機
[root@centos7 ~]#shutdown +3 systerm will shutdown  ---關機
Shutdown scheduled for Mon 2017-07-17 21:06:37 CST, use 'shutdown -c' to cancel.
[root@centos7 ~]#
Broadcast message from root@centos7.magedu.com (Mon 2017-07-17 21:03:37 CST):
systerm will shutdown
The system is going down for power-off at Mon 2017-07-17 21:06:37 CST!
shutdown -c ---取消關機
Broadcast message from root@centos7.magedu.com (Mon 2017-07-17 21:04:01 CST):
The system shutdown has been cancelled at Mon 2017-07-17 21:05:01 CST!

3、screen命令

建立新screen會話
screen –S [SESSION]
加入screen會話
screen –x [SESSION]
退出並關閉screen會話
exit
剝離當前screen會話
Ctrl+a,d
顯示所有已經開啟的screen會話
screen -ls
恢復某screen會話
screen -r [SESSION]

  • yes 命令如果在執行的過程中斷網,怎麼恢復
[root@centos6 ~]#screen
[root@centos6 ~]# yes
斷網-重現連線
[root@centos6 ~]#screen -ls
[root@centos6 ~]#screen -r

4、echo命令

語法:echo [-neE][字串]
說明:echo會將輸入的字串送往標準輸出。輸出的字串間以空白字元隔開, 並在最後加上換行號
選項:
-E (預設)不支援\解釋功能
-n 不自動換行
-e 啟用\字元的解釋功能

  • 常用選項
[root@centos7 ~]#echo -e "\a"---發出聲音
[root@centos7 ~]#echo -e "abca\bef"---退格鍵
abcef
[root@centos7 ~]#echo -e "abc\cdf"---啟用不自動換行
abc[root@centos7 ~]#echo -e "abc\ndf"---啟用自動換行
abc
df
[root@centos7 ~]#echo -e "abe\rd"---不換行游標移至行首
dbe
[root@centos7 ~]#echo -e "aa\tbb\tcc\ndd\tee\tff"---加入製表符
aa      bb      cc
dd      ee      ff
[root@centos7 ~]#echo -e "\0101"---八進位制所代表的ASCII字元
A
[root@centos7 ~]#echo -e "\x61"---十六進位制所代表的ASCII字元
a
  • 命令列擴充套件$( ) 或把一個命令的輸出列印給另一個命令的引數
[root@centos7 ~]#echo "echo $UID"  
echo 0
[root@centos7 ~]#echo 'echo $UID'
echo $UID---當成字串
[root@centos7 ~]#echo  `echo $UID` 
0---命令呼叫命令即命令列擴充套件

總結:單引號是最傻的符號,反向單引號最聰明,雙引號介於兩者之間。

  • 括號擴充套件:{ }
    列印重複字串的簡化形式
[root@centos7 ~]#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@centos7 ~]#echo {20..10}
20 19 18 17 16 15 14 13 12 11 10
[root@centos7 ~]#echo {2,5,6}
2 5 6
[root@centos7 ~]#echo {a,b,c}.{txt,log}
a.txt a.log b.txt b.log c.txt c.log
[root@centos7 ~]#echo {1..100..3}
1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70 73 76 79 82 85 88 91 94 97 100
[root@centos7 ~]#echo {000..100..3}
000 003 006 009 012 015 018 021 024 027 030 033 036 039 042 045 048 051 054 057 060 063 066 069 072 075 078 081 084 087 090 093 096 099

二、命令歷史(history)

history是一個內部命令,每個會話都有自己的命令歷史,命令歷史先儲存在記憶體中,當退出重新登入後會把記憶體中的歷史命令寫入歷史檔案中( .bash_history)

1、呼叫歷史

  • 重複執行上一條命令:使用上方向鍵,並回車執行
  • !string 重複前一個以“string”開頭的命令
  • !?string 重複前一個包含string的命令
  • string1string2將上一條命令中的第一個string1替換為string2
  • command !^ : 利用上一個命令的第一個引數做cmd的引數
  • command !$ : 利用上一個命令的最後一個引數做cmd的引數
  • command !* : 利用上一個命令的全部引數做cmd的引數
  • !$ 表示:Esc, .(點選Esc鍵後鬆開,然後點選. 鍵)

2、命令history

history [-c] [-d offset] [n]
history -anrw[filename]
history -psarg[arg...]
-c: 清空命令歷史
-d: 刪除歷史中指定的命令 如history -d 2 表示刪除第二條歷史
n: 顯示最近的n條歷史
-a: 追加本次會話新執行的命令歷史列表至歷史檔案---注意是新執行的命令
-n: 讀歷史檔案中未讀過的行到歷史列表---** 比如新開的會話,歷史不多,會把歷史檔案中未讀過的行到歷史列表中,不重複**
-r: 讀歷史檔案附加到歷史列表,---輸入一次就會讀一次,會重複
-w: 儲存歷史列表到指定的歷史檔案---重新登入也會儲存到歷史檔案中
-p: 展開歷史引數成多行,但不存在歷史列表中---隱藏曆史
-s: 展開歷史引數成一行,附加在歷史列表後---偽造歷史

  • 常用選項
[root@centos7 ~]#history -c ---刪除記憶體中的歷史,退出重新登入後會從歷史檔案中把歷史讀到記憶體中
[root@centos7 ~]#history
    1  history
[root@centos7 ~]#history -p `hostname`---執行命令但不記錄歷史
centos7.magedu.com
[root@centos7 ~]#history
    1  history
[root@centos7 ~]#history -p `rm -f /app/*`---但遇到刪除命令等敏感詞彙時還是會記錄歷史
[root@centos7 ~]#history
    1  history
    2  history -p `rm -f /app/*`---記錄歷史
    3  history
[root@centos7 ~]#history -s "rm -rf /"--- 命令不執行,但記錄到歷史中
[root@centos7 ~]#history 
    1  history
    2  history -n
    3  history
    4  ls
    5  pwd
    6  ll
    7  history
    8  rm -rf /---記錄到歷史中,可以偽造歷史
    9  history 

總結:如果要想做壞事不被別人發現,可以先刪除歷史檔案、再刪除記憶體中的歷史,然後exit。

3、命令歷史相關環境變數

HISTSIZE:命令歷史記錄的條數
HISTFILE:指定歷史檔案,預設為~/.bash_history
HISTFILESIZE:命令歷史檔案記錄歷史的條數
HISTTIMEFORMAT=“%F %T “ 顯示時間
HISTIGNORE=“str1:str2*:… “ 忽略str1命令,str2開頭的歷史
控制命令歷史的記錄方式:
環境變數:HISTCONTROL
ignoredups預設,忽略重複的命令,連續且相同為“重複”
ignorespace忽略所有以空白開頭的命令
ignoreboth相當於ignoredups, ignorespace的組合
erasedups刪除重複命令
export 變數名="值“
存放在/etc/profile 或~/.bash_profile,建議存放在自己的家目錄裡,然後.或者source讓檔案生效即可。

  • 舉例
[root@centos7 ~]#HISTTIMEFORMAT="%F %T "---加時間
[root@centos7 ~]#history
    1  2017-07-18 10:27:18 history
    2  2017-07-18 10:27:20 l
    3  2017-07-18 10:27:21 ll
    4  2017-07-18 10:27:23 pwd
    5  2017-07-18 10:27:24 cd
    6  2017-07-18 10:31:09 HISTTIMEFORMAT="%F %T "
    7  2017-07-18 10:31:15 history
[root@centos7 ~]#HISTIGNORE="ll*"---忽略以ll開頭的命令
[root@centos7 ~]#ll
total 8
-rw-------. 1 root root 1884 Jul 14 11:24 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Jul 14 11:54 Desktop
drwxr-xr-x. 2 root root    6 Jul 14 11:54 Documents
drwxr-xr-x. 2 root root    6 Jul 14 11:54 Downloads
-rw-r--r--. 1 root root 1915 Jul 14 11:54 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 Jul 14 11:54 Music
drwxr-xr-x. 2 root root    6 Jul 14 11:54 Pictures
drwxr-xr-x. 2 root root    6 Jul 14 11:54 Public
drwxr-xr-x. 2 root root    6 Jul 14 11:54 Templates
drwxr-xr-x. 2 root root    6 Jul 14 11:54 Videos
[root@centos7 ~]#history
    1  2017-07-18 10:27:18 history
    2  2017-07-18 10:27:20 ls
    3  2017-07-18 10:27:21 ll
    4  2017-07-18 10:27:23 pwd
    5  2017-07-18 10:27:24 cd
    6  2017-07-18 10:31:09 HISTTIMEFORMAT="%F %T "
    7  2017-07-18 10:31:15 history
    8  2017-07-18 10:32:56 HISTIGNORE="passwd*"
    9  2017-07-18 10:33:38 HISTIGNORE="ll*"
   10  2017-07-18 10:33:49 history
[root@centos7 ~]#HISTCONTROL=ignoreboth---忽略重複和以空白開頭的命令
[root@centos7 ~]#pwd
/root
[root@centos7 ~]#pwd
/root
[root@centos7 ~]# ls
anaconda-ks.cfg  Desktop  Documents  Downloads  initial-setup-ks.cfg  Music  Pictures  Public  Templates  Videos
[root@centos7 ~]#history 
    1  2017-07-18 10:27:18 history
    2  2017-07-18 10:27:20 ls
    3  2017-07-18 10:27:21 ll
    4  2017-07-18 10:27:23 pwd
    5  2017-07-18 10:27:24 cd
    6  2017-07-18 10:31:09 HISTTIMEFORMAT="%F %T "
    7  2017-07-18 10:31:15 history
    8  2017-07-18 10:32:56 HISTIGNORE="passwd*"
    9  2017-07-18 10:33:38 HISTIGNORE="ll*"
   10  2017-07-18 10:33:49 history
   11  2017-07-18 10:35:42 pwd
   12  2017-07-18 10:35:50 history
   13  2017-07-18 10:36:54 HISTCONTROL=ignoreboth
   14  2017-07-18 10:36:58 pwd
   15  2017-07-18 10:37:19 history 

3、如何實現螢幕錄影

[root@centos7 ~]#script -t 2> /app/time.log -a /app/cmd.log---準備錄屏的time.log和cmd.log兩個檔案,一個用來存時間,一個用來存執行的命令,並開始錄屏
Script started, file is /app/cmd.log
---執行一些命令
[root@centos7 ~]#hostname
centos7.magedu.com
[root@centos7 ~]#ls
anaconda-ks.cfg  Desktop  Documents  Downloads  initial-setup-ks.cfg  Music  Pictures  Public  Templates  Videos
---執行退出命令
[root@centos7 ~]#exit
exit
Script done, file is /app/cmd.log
---執行以下命令開始播放錄屏
[root@centos7 ~]#scriptreplay /app/time.log /app/cmd.log 

三、如何獲取幫助

  • 內部命令獲取幫助
    help command
    man bash
  • 外部命令獲取幫助
    man command
    command -- help
  • 獲取幫助的步驟
[root@centos7 ~]#type passwd---判讀以下是內部命令還是外部命令
passwd is /usr/bin/passwd
[root@centos7 ~]#whatis passwd---檢視章節,並顯示每個章節的資訊
passwd (1)           - update user's authentication tokens
sslpasswd (1ssl)     - compute password hashes
passwd (5)           - password file
[root@centos7 ~]#man 1 passwd ---最後用man來查幫助資訊
  • 一些命令
[root@centos7 ~]#whereis passwd---顯示路徑和man幫助文件路徑
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz
[root@centos7 ~]#man -a passwd---顯示全部章節資訊,按q後進入下一個章節
[root@centos7 ~]#man -k passwd 相當於搜尋,比如不知道幹什麼,可以搜尋的關鍵字
chpasswd (8)         - update passwords in batch mode
fgetpwent_r (3)      - get passwd file entry reentrantly
getpwent_r (3)       - get passwd file entry reentrantly
gpasswd (1)          - administer /etc/group and /etc/gshadow
grub2-mkpasswd-pbkdf2 (1) - Generate a PBKDF2 password hash.
lpasswd (1)          - Change group or user password
lppasswd (1)         - add, change, or delete digest passwords.
pam_localuser (8)    - require users to be listed in /etc/passwd
passwd (1)           - update user's authentication tokens
sslpasswd (1ssl)     - compute password hashes
passwd (5)           - password file
passwd2des (3)       - RFS password encryption
pwhistory_helper (8) - Helper binary that transfers password hashes from passwd or shadow to opasswd
saslpasswd2 (8)      - set a user's sasl password
smbpasswd (5)        - The Samba encrypted password file
vncpasswd (1)        - change the VNC password
  • man幫助文件行間跳轉
    gg/1G 跳至第一行
    G 跳至最後一行
    100G 跳至第100行

三、檔案系統分層結構

  • 目錄結構


    6854348-866ed7c6f9191070.png
    Paste_Image.png
  • 檔案型別-:普通檔案
    d: 目錄檔案
    b: 塊裝置
    c: 字元裝置
    l: 符號連結檔案
    p: 管道檔案pipe
    s: 套接字檔案socket
  • 如何觸發新增加的磁碟
    執行以下命令
    echo '- - -' /sys/class/scsi_host/host2/scan

說明:這個命令只是在實驗環境使用,生產環境不會隨意增加磁碟的。

相關文章