這份整理把入門階段的四個單元串成一條線:桌面與指令基本功、目錄結構與檔案管理、man 手冊、以及輸入輸出的重定向與管道。每個單元都附上題目與操作實錄。
1. 用 student 用戶登入圖形介面
2. 打開一個 bash
3. 把 student 的密碼改成 T3st1ngtlme
[student@putlog Desktop]$ su - root Password: Last login: Sat Oct 15 05:11:02 EDT 2016 from 172.25.254.145 on pts/1 [root@putlog ~]# passwd student Changing password for user student. New password: Retype new password: passwd: all authentication tokens updated successfully.
4. 顯示當前系統時間
[root@putlog ~]# date Wed Oct 26 08:16:37 EDT 2016
5. 以「小時:分鐘:秒 AM/PM」格式顯示時間
[root@putlog Desktop]# date +%r 08:33:16 AM
6. 查看 /usr/bin/clean-binary-files 的類型與 7. 統計其大小
[root@putlog Desktop]# wc /usr/bin/clean-binary-files 594 1780 13220 /usr/bin/clean-binary-files [root@putlog Desktop]# wc -l /usr/bin/clean-binary-files 594 /usr/bin/clean-binary-files [root@putlog Desktop]# wc -w /usr/bin/clean-binary-files 1780 /usr/bin/clean-binary-files [root@putlog Desktop]# wc -c /usr/bin/clean-binary-files 13220 /usr/bin/clean-binary-files [root@putlog Desktop]# wc -m /usr/bin/clean-binary-files 13220 /usr/bin/clean-binary-files
8. 用快捷鍵叫回第 4 條命令;9. 執行最近一條含 date 的命令
[root@putlog Desktop]# !4 useradd fentiao useradd: user 'fentiao' already exists [root@putlog Desktop]# date +%r 08:53:33 AM
用一條命令建立 12 個檔案 WESTOS_classX_linuxY(X 為 1-2,Y 為 1-6),放在 root 桌面的 study 目錄
[root@pxeDesktop]# mkdir study
[root@pxe Desktop]# touch study/WESTOS_class{1..2}_linux{1..6}
用一條命令在 /mnt/VERSION 中建立 8 個 redhat_versionX(X 為 1-8)
[root@pxeDesktop]# cd /mnt
[root@pxe mnt]# mkdir VERSION
[root@pxe mnt]# touch VERSION/redhat_version{1..8}
接著做檔案的分流與搬運:
奇數檔複製到桌面 SINGLE
[root@pxeDesktop]# mkdir SINGLE
[root@pxe Desktop]# cp -r /mnt/VERSION/redhat_version{1,3,5,7} SINGLE
偶數檔複製到 /DOUBLE
[root@pxeDesktop]# mkdir /DOUBLE
[root@pxe Desktop]# cp -r /mnt/VERSION/redhat_version{2,4,6,8} /DOUBLE
class1 的檔案移到桌面 CLASS1
[root@pxeDesktop]# mkdir CLASS1
[root@pxe Desktop]# mv study/WESTOS_class1_linux{1..6} CLASS1
class2 的檔案移到桌面 CLASS2
[root@pxeDesktop]# mkdir CLASS2
[root@pxe Desktop]# mv study/WESTOS_class2_linux{1..6} CLASS2
把 /etc 下以 .conf 結尾且名字帶數字的檔案備份到桌面 confdir,最後全部清乾淨:
[root@pxeDesktop]# mkdir confdir [root@pxe Desktop]# cp /etc/*.conf confdir [root@pxe Desktop]# rm -fr study [root@pxe Desktop]# rm -fr CLASS1 [root@pxe Desktop]# rm -fr CLASS2 [root@pxe Desktop]# rm -fr confdir [root@pxe mnt]# rm -fr VERSION
(1)顯示正確輸出,屏蔽錯誤輸出 find /etc/ -name passwd 2>/dev/null find /etc/ -name passwd 2> file (2)正確輸出存到 /mnt/find.out,錯誤輸出存到 /mnt/find.err [student@foundation22Desktop]$ find /etc/ -name passwd >/mnt/file.out 2> /mnt/file.err bash: /mnt/file.out: Permission denied [student@foundation22Desktop]$ su - root Passwd: [root@foundation22Desktop]$ chmod ugo+rwx /mnt/ [student@foundation22Desktop]$ find /etc/ -name passwd >/mnt/file.out 2> /mnt/file.err (3)建立 /mnt/find.all 並保存所有輸出 find /etc/ -name passwd &>/mnt/file.all (4)再次追加所有輸出,保留原內容 find /etc/ -name passwd &>>/mnt/file.all (5)屏蔽所有輸出 find /etc/ -name passwd &>>file (6)顯示所有輸出並保存到桌面任意檔案 find /etc/ -name passwd (7)正確輸出存到 /mnt/find.out.1,屏蔽錯誤輸出 find /etc/ -name passwd > /mnt/file.out.1 2> /dev/null // 1>/dev/null 2>/dev/null &>/dev/null
[kiosk@foundation0 Desktop]$ rht-vmctl start desktop ――開啟 Error: desktop not started (is already running) ――已在運行 [kiosk@foundation0 Desktop]$ rht-vmctl view desktop ――檢視 [kiosk@foundation0 Desktop]$ rht-vmctl poweroff desktop ――關閉 Powering off desktop [kiosk@foundation0 Desktop]$ rht-vmctl view desktop Error: unable to view desktop - not currently running ――未啟動
帳號:普通用戶 student/student,超級用戶 root/redhat。
提示符 [kiosk@foundation0 Desktop]$ 依序是「用戶@主機 目錄」。su - username 用來換身分,高權限切低權限免密碼,反之則需要;exit 或 ctrl+D 退出。ctrl+alt+F(1|7) 回圖形,ctrl+alt+F(2~6) 進虛擬控制台。命令格式為「命令 參數 目標」,ctrl+c 撤銷執行。whatis 看功能,命令 --help 看幫助,[] 可選、<> 必填、... 數量不限。
開 bash 的四種方式:Applications → Utilities → Terminal、右鍵 open in terminal、直接跑 gnome-terminal,退出用 exit/ctrl+d。
date:watch -n 1 date 每秒刷新;date 10302206 設定為 10 月 30 日 22:06;date -d +xday 查看前後日期;+%... 自訂輸出格式。
passwd:root 改他人密碼,普通用戶改自己(需原密碼,且新密碼至少 8 位、不能與舊密碼過於相似或過於簡單)。參數 -s 查詢、-l 鎖定、-u 解鎖、-n/-x 設定有效期、-w 警告期、-I 非活躍天數、-d 清空密碼。
file / cat / head / tail / wc:分別負責看類型、看全文、看前幾行(預設 10)、看後幾行(預設 10)、統計行數(-l)字數(-w)位元組數(-c)字符數(-m)。
Tab 與 history:Tab 補全命令、檔名與參數;history 看歷史、history -c 清歷史;!數字 執行第 N 條、!字母 執行最近含該字母的命令、ctrl+R 反查關鍵字。
Linux 是倒樹結構,一切皆檔案,根目錄 / 之下二級目錄多由安裝時自動建立:
/bin 二進制可執行檔(系統命令) /sbin 系統管理命令 /boot 啟動分區(kernel、initramfs、grub) /dev 設備檔案 /etc 多數系統設定檔 /home 普通用戶家目錄 /lib 32 位庫檔 /lib64 64 位庫檔 /media /mnt /run 臨時掛載點 /opt 第三方軟體安裝位置 /proc 系統資訊 /sys 內核與設備管理 /root 超級用戶家目錄 /srv /var 系統數據 /tmp 臨時檔案 /usr 用戶相關數據與自訂軟體
路徑分絕對路徑(以 / 開頭的真實位置)與相對路徑(相對於當前位置,會自動拼上 pwd 的值)。
檔案操作:touch 建檔或改時間戳;mkdir -p 建目錄(父層不存在自動補);rm -f 強刪、-r 刪目錄;cp -r 複製目錄;mv 同磁碟是改名、跨磁碟是複製加刪除。vim 進入後預設是命令模式,按 i 進插入模式,esc 回命令模式,:wq 存檔退出、:q! 放棄退出。
cd 系列:cd 目錄、cd - 回上一個目錄、cd ~ 回家、cd ~username 到指定用戶家、cd .. 回上一層。ls 系列:-d 列目錄本身、-l 列屬性、-a 含隱藏檔、-R 遞歸。
* 任意多個字符 ? 單個字符
[[:alpha:]] 字母 [[:lower:]] 小寫 [[:upper:]] 大寫
[[:digit:]] 數字 [[:alnum:]] 數字或字母
[[:punct:]] 符號 [[:space:]] 空格
{1..9} 1 到 9 {a..f} a 到 f {1,3,5} 1 3 5
[1-3] 1 或 2 或 3 [145] 1 或 4 或 5
[^abc] / [!abc] 排除 a b c
~ 家目錄 ~username 指定用戶家 ~+ 當前目錄 ~- 上一個目錄
不建議用 [a-C] 這類跨大小寫寫法,會把 ASCII 碼中間的字符全部補進來,組合數量暴增。
man 是 manual 的縮寫。man man 看 man 自己,man passwd 看命令。章節劃分:1 系統命令、2 系統接口、3 函數庫、4 特殊檔案(設備)、5 檔案格式、6 遊戲、7 軟體包、8 系統管理命令、9 內核。man 5 passwd 看的是 /etc/passwd 的格式說明,man 1 passwd 才是命令用法(1 可省略)。翻頁用上下鍵、pageup/pagedown、d/u 半屏、g/G 首尾跳轉,/關鍵字 向下搜(高亮,n 同向、N 反向)。
系統中正確輸出編號為 1,錯誤輸出為 2。以普通用戶跑 find /etc -name passwd 會因權限不足混雜大量 Permission denied。
find /etc -name passwd >file 重定向正確輸出 find /etc -name passwd 2>file 重定向錯誤輸出 find /etc -name passwd &>file 重定向全部輸出 >file 清空檔案;>>、2>>、&>> 則是追加,不覆蓋原內容
管道 | 把前一條命令的輸出變成後一條命令的輸入,例如 ls /bin | wc -l 統計行數;錯誤輸出無法直接過管道,可用 2>&1 把 2 號改成 1 號。tee 則把輸出複製一份到指定位置:date | tee file | wc -l。