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

一、簡單命令

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行

三、文件系統分層結構

  • 目錄結構


    Paste_Image.png
  • 文件類型-:普通文件
    ?d: 目錄文件
    ?b: 塊設備
    ?c: 字符設備
    ?l: 符號鏈接文件
    ?p: 管道文件pipe
    ?s: 套接字文件socket
  • 如何觸發新增加的磁盤
    執行以下命令
    echo '- - -' /sys/class/scsi_host/host2/scan

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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,797評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,179評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,628評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,642評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,444評論 6 405
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,948評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,040評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,185評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,717評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,602評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,794評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,316評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,045評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,418評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,671評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,414評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,750評論 2 370

推薦閱讀更多精彩內容