這份作業題目與前幾份大同小異,但示範的參數更完整,特別是 cp 的選項清單與 alias 的永久生效做法值得細看。
cp [OPTION]... [-T] SOURCE DEST cp [OPTION]... SOURCE... DIRECTORY cp [OPTION]... -t DIRECTORY SOURCE...
行為規則:
-r。常用選項:
-a, --archive 等於 -dR --preserve=all -d 等於 --no-dereference --preserve=links -f, --force 目標無法開啟時先移除再重試 -i, --interactive 覆寫前詢問 -L, --dereference 總是跟隨符號連結 -n, --no-clobber 不覆寫既有檔案 -P, --no-dereference 不跟隨來源中的符號連結 -p 等於 --preserve=mode,ownership,timestamps -R, -r, --recursive 複製目錄及其內容
實例:
[root@localhost /]# cp etc/fstab /tmp/123/ # 123 不存在則建立檔案 [root@localhost /]# cp /etc/host.conf /etc/fstab /tmp/mylinux/ # 多檔複製 [root@localhost /]# cp -a /etc/system-release /tmp/12/ # 保留屬性與連結 [root@localhost /]# ll /tmp/12/ 總用量 0 lrwxrwxrwx. 1 root root 14 8月 13 19:49 123 -> centos-release lrwxrwxrwx. 1 root root 14 8月 12 06:54 system-release -> centos-release
注意最後這個示範:以 -a 複製符號連結時,連結目標是相對路徑,換了位置之後指向就會出錯,實務上要留意。
mv [選項]... [-T] 源檔案 目標檔案 mv [選項]... 源檔案... 目錄 -f, --force 覆寫前不詢問 -i, --interactive 覆寫前詢問 -n, --no-clobber 不覆寫既有檔案
[root@localhost 12]# mv 123 ../a_c/ # 移動到目錄 [root@localhost 12]# mv system-release 321 # 重新命名 [root@localhost tmp]# mv 12/ b_c/ # 移動目錄
rm [選項]... 檔案... -f 強制刪除 -i 互動式 -r 刪除目錄 [root@localhost b_c]# rm -rf 12 # 強制遞迴刪除
命令歷史:相關環境變數有 HISTSIZE(記憶體中記錄的條數)、HISTFILE(預設 ~/.bash_history)、HISTFILESIZE(檔案中記錄的條數)。調用方式:
!# 重複執行第 # 條指令 !! 執行上一條命令 !string 調用最近一個以 string 開頭的命令 !$ 調用上一條命令的最後一個參數 ESC , . 同上
控制記錄方式的環境變數 HISTCONTROL:ignoredups 忽略連續重複(預設)、ignorespace 忽略以空白開頭的命令、ignoreboth 兩者同時生效。
命令補全:TAB 鍵補全命令與路徑。
命令展開:
[root@localhost /]# cd ~ # 展開為家目錄 [root@localhost ~]# cd ~jun_shao/ # 展開為指定使用者家目錄
執行狀態:0 成功,1–255 失敗,以 echo $? 取得。
[root@localhost /]# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var [root@localhost /]# echo $? 0
快捷鍵:Ctrl+L 清屏(等同 clear)、Ctrl+A 跳到命令首部、Ctrl+E 跳到尾部、Ctrl+C 取消執行、Ctrl+U 刪除首部到游標處、Ctrl+K 刪除游標處到行尾。
mkdir /tmp/{a,b}_{c,d}
mkdir -p /tmp/mylinux/{bin,boot/grub,dev,etc/{rc.d/init.d,sysconfig/network-scripts},lib/modules,lib64,proc,sbin,sys,tmp,usr/local/{bin,sbin},var,lock,log,run}
[root@localhost mylinux]# tree .
.
├── bin
├── boot
│ └── grub
├── dev
├── etc
│ ├── rc.d
│ │ └── init.d
│ └── sysconfig
│ └── network-scripts
├── lib
│ └── modules
├── lib64
├── lock
├── log
├── proc
├── run
├── sbin
├── sys
├── tmp
├── usr
│ └── local
│ ├── bin
│ └── sbin
└── var
元資料包含檔案大小、擁有者、權限,以及三個時間戳:存取時間 atime、修改時間 mtime、改變時間 ctime。以 stat 查詢:
[root@localhost /]# stat /tmp/mylinux/ File: "/tmp/mylinux/" Size: 4096 Blocks: 8 IO Block: 4096 directory Device: fd00h/64768d Inode: 206503646 Links: 17 Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root) 最近訪問:2016-08-13 02:40:09.054684514 +0800 最近更改:2016-08-13 02:40:02.371740577 +0800 最近改動:2016-08-13 02:40:02.371740577 +0800 建立時間:-
修改時間戳:
touch [選項]... 檔案... -a 只更改存取時間 -c, --no-create 不建立任何檔案 -d, --date=字串 使用指定字串表示時間 -m 只更改修改時間 -t STAMP [[cc]YY]MMDDhhmm[.ss]
[root@localhost /]# touch -t 1510101010.10 tmp/mylinux/ # 指定時間 [root@localhost /]# touch -a tmp/mylinux/ # 只更新 atime [root@localhost /]# touch tmp/mylinux/ # 更新 atime 與 mtime
可以觀察到:無論怎麼改 atime 與 mtime,ctime 一律被更新為操作當下的時間,因為 inode 本身發生了變動。
alias 列出目前 shell 所有別名 alias NAME='VALUE' 定義別名 unalias [-a] name [name...] 撤銷別名
命令列定義的別名只在當前 shell 有效;要永久生效需寫入設定檔:當前使用者用 ~/.bashrc,全體使用者用 /etc/bashrc。修改設定檔後不會立即生效,需重新讀取:source /path/to/config_file 或 ./path/to/config_file。若別名與原指令同名,前面加反斜線可執行原始指令,例如 \COMMAND。
[root@localhost ~]# alias net='cd /etc/sysconfig/network-scripts/' [root@localhost ~]# net [root@localhost network-scripts]# [root@localhost ~]# nano .bashrc [root@localhost ~]# source .bashrc # 重新讀取設定檔 [root@localhost ~]# alias cp="cp -r" # 定義與系統同名的別名 [root@localhost ~]# \cp /etc/fstab /tmp/b_c/ # 以 \cp 執行原始指令 [root@localhost ~]# unalias cp # 撤銷別名
# 6. /var 下以 l 開頭、小寫字母結尾、中間至少一位數字 ls -d /var/l*[0-9]*[[:lower:]] /var/l121231sdfasd /var/l2dc # 7. 數字開頭、非數字結尾 ls -d /tmp/[0-9]*[^0-9] /tmp/111223@ /tmp/111223b # 8. 非字母開頭,後接一個字母與任意字元 ls /etc/[^[:alpha:]][[:alpha:]]* # 9. 以 tfile 加當前日期時間命名 touch /tmp/`echo tfile-$(date +%Y-%m-%d-%H-%M-%S)` # 10. /etc 下 p 開頭、非數字結尾者複製到 mytest1 cp -an /etc/p[^0-9]* /tmp/mytest1/ # 11. /etc 下 .d 結尾者複製到 mytest2 cp -an /etc/*.d /tmp/mytest2/ # 12. /etc 下以 l、m、n 開頭且以 .conf 結尾者複製到 mytest3 cp -an /etc/[lmn]*.conf /tmp/mytest3 /tmp/mytest3 ├── ld.so.conf ├── libaudit.conf ├── libuser.conf ├── locale.conf ├── logrotate.conf ├── man_db.conf ├── mke2fs.conf ├── mtools.conf ├── nfsmount.conf ├── nsswitch.conf ├── ntp.conf └── numad.conf