1.lshtml
功能:列出文件或目錄node
命令幫助:linux
man ls算法
info lsshell
ls --helpwindows
help cd #只有內部命令才能用
ls /usr/share/man 文檔位置安全
經常使用選項:bash
-a:列出全部的文件(包含隱藏文件)服務器
-l:以長格式的形式列出信息網絡
-i:顯示文件inode號
-h:單位換算
-m:以逗號分割文件名列出
-t:按照修改時間來排序
-r:按照修改時間來反向排列
-S:按大小排
示例:
將家目錄下的全部文件列出來(含屬性與隱藏檔) [root@localhost /]# ls -al ~ total 88 dr-xr-x---. 3 root root 4096 May 8 22:27 . ...... 長格式列出 [root@localhost ~]# ls -l total 44 -rw-------. 1 root root 1166 May 7 10:08 anaconda-ks.cfg -rw-r--r--. 1 root root 17957 May 7 10:08 install.log -rw-r--r--. 1 root root 4178 May 7 10:06 install.log.syslog -rw-r--r--. 1 root root 7273 May 7 21:30 system.sh 按照修改時間倒序 [root@localhost ~]# ls -lt total 44 -rw-r--r--. 1 root root 7273 May 7 21:30 system.sh -rw-------. 1 root root 1166 May 7 10:08 anaconda-ks.cfg -rw-r--r--. 1 root root 17957 May 7 10:08 install.log -rw-r--r--. 1 root root 4178 May 7 10:06 install.log.syslog 按照修改時間正序 [root@localhost ~]# ls -lrt total 44 -rw-r--r--. 1 root root 4178 May 7 10:06 install.log.syslog -rw-r--r--. 1 root root 17957 May 7 10:08 install.log -rw-------. 1 root root 1166 May 7 10:08 anaconda-ks.cfg -rw-r--r--. 1 root root 7273 May 7 21:30 system.sh
2.cd
功能:更改工做路徑
cd是Change Directory的縮寫
用法:
cd 直接回車 直接切換到當前用戶的家目錄裏
cd ~ 直接切換到當前用戶的家目錄裏
cd .或者 ./ 切換到當前目錄
cd .. 切換到上一級目錄
cd - 切換到上一次的工做目錄
示例:
絕對路徑切換: [root@localhost ~]# cd /root/test 相對路徑切換: [root@localhost /]# cd ./test 當前是root,那就是回到root家目錄 [root@localhost /]# cd ~ 回到上一級目錄 [root@localhost test]# cd ..
3.cp
功能:複製文件或目錄
語法:
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...
經常使用選項:
-a 歸檔,至關於-dR --perserv=all
-b 目標文件存在建立備份,備份文件是文件名跟~
-f 強制複製文件或目錄
-r 遞歸複製目錄
-p 保留原有文件或目錄屬性,--perserv=mode,ownership,timestamp
-i 交互式,即複製文件或目錄會詢問
-u 當源文件比目的文件修改時間新時才複製
-v 顯示覆制信息,--verbose
區別:
若是是目錄須要拷貝全部屬性,用-a或者-rp
若是是文件須要拷貝全部屬性,用-p或者-a
注意:cp在管理員的狀況下慎用,儘可能用交互式來肯定是否真的想cp覆蓋文件或目錄
示例:
[root@localhost scripts]# cp -v file file2 #常規復制 `file' -> `file2' [root@localhost scripts]# cp -v file2{,.bak} #用大括號複製改變是否是更簡潔些 `file2' -> `file2.bak'
4.mkdir
功能:建立新目錄(make directory)
語法:mkdir [OPTION]... DIRECTORY...
經常使用選項:
-m:配置文件的權限
-p:遞歸建立目錄
-v:顯示建立過程
示例:
請到/tmp底下嘗試建立數個新目錄看看: [root@localhost tmp]# pwd /tmp [root@localhost tmp]# mkdir test #建立test新目錄 [root@localhost tmp]# mkdir -p test1/test2/test3/test4 遞歸建立多個目錄 [root@localhost tmp]# tree test1 查看建立成功 test1 └── test2 └── test3 └── test4 directories, 0 files 範例:建立權限爲rwx--x--x的目錄 [root@localhost tmp]# mkdir -m 655 test2 [root@localhost tmp]# ll -d test2 drw-r-xr-x 2 root root 4096 May 24 10:19 test2 上面的權限部分,若是沒有加上 -m 來強制配置屬性,系統會使用默認屬性。 建立多個目[root@localhost tmp]# mkdir {1,2} [root@localhost tmp]# ll total 24 drwxr-xr-x 2 root root 4096 May 24 10:21 1 drwxr-xr-x 2 root root 4096 May 24 10:21 2 建立連續目錄: [root@localhost tmp]# mkdir {3..10} [root@localhost tmp]# ll total 56 drwxr-xr-x 2 root root 4096 May 24 10:23 1 drwxr-xr-x 2 root root 4096 May 24 10:23 10 drwxr-xr-x 2 root root 4096 May 24 10:23 2 drwxr-xr-x 2 root root 4096 May 24 10:23 3 drwxr-xr-x 2 root root 4096 May 24 10:23 4 drwxr-xr-x 2 root root 4096 May 24 10:23 5 drwxr-xr-x 2 root root 4096 May 24 10:23 6 drwxr-xr-x 2 root root 4096 May 24 10:23 7 drwxr-xr-x 2 root root 4096 May 24 10:23 8 drwxr-xr-x 2 root root 4096 May 24 10:23 9
5.mv
功能:移動文件或目錄,或重命名
語法:
mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE..
經常使用選項:
-b 目標文件存在建立備份,備份文件是文件名跟
-u 當源文件比目的文件修改時間新時才移動
-v 顯示移動信息
-i 交互式
-f 強制
-t 將全部源移動到目錄中
注意:mv也是一個危險命令,使用時慎之又慎
示例:
[root@localhost scripts]# mv file2.bak{,.ori} 把file2.bak重名爲file2.bak.ori [root@localhost scripts]# ls yyl 10.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt 移動到上一級目錄有多種方法: [root@localhost yyl]# mv 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt 10.txt -t ../ #這個方法太笨 [root@localhost yyl]# ls ../ 10.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt [root@localhost yyl]# mv {1..10}.txt ../ [root@localhost yyl]# mv * ../
6.pwd(Print Working Directory)
功能:顯示當前所在位置的絕對路徑。
選項:
-P:顯示出實際的路徑,而非使用連接(link)路徑。
示例:
單純顯示出目前的工做目錄 [root@www ~]# pwd /root 顯示實際的工做目錄,而非鏈接擋自己的目錄名而已 root@www ~]# cd /var/mail <==注意,/var/mail是一個連結檔 [root@www mail]# pwd /var/mail <==列出目前的工做目錄 [root@www mail]# pwd -P /var/spool/mail <==怎麼跟上面顯示不同? [root@www mail]# ls -ld /var/mail lrwxrwxrwx 1 root root 10 Sep 4 17:54 /var/mail -> spool/mail # /var/mail 是連結檔,連結到 /var/spool/mail # 加上 pwd -P 的選項後,會不以連結檔的數據顯示,而是顯示正確的完整路徑
7.rename
功能:重命名文件,支持通配符
語法:rename from to file...
rename -V
經常使用選項:
-V:打印版本(惟一參數)
示例:
[root@localhost scripts]# rename -V rename (util-linux-ng 2.17.2) 批量命名文件 將file1-file5重名爲test1-test5 [root@localhost ~]# ls file* file1 file2 file3 file4 file5 [root@localhost ~]# rename "file" "test" * [root@localhost ~]# ls test* test1 test2 test3 test4 test5 建立文件後綴爲.jpg的,而後把後綴更名爲.html [root@localhost test]# for file in `seq -w 10`;do touch stu_$file.jpg;done [root@localhost test]# ls stu_01.jpg stu_03.jpg stu_05.jpg stu_07.jpg stu_09.jpg stu_02.jpg stu_04.jpg stu_06.jpg stu_08.jpg stu_10.jpg [root@localhost test]# rename ".jpg" ".html" ./* [root@localhost test]# ls stu_01.html stu_03.html stu_05.html stu_07.html stu_09.html stu_02.html stu_04.html stu_06.html stu_08.html stu_10.html
8.rm
功能:remove,刪除文件或目錄
語法:rm [OPTION]... FILE...
經常使用選項:
-i:交互式
-f:強制刪除
-r:遞歸
注意:rm相對於mv,cp,在生產環境就更危險了,好多同仁受其害不止一次兩次,建議禁用。
9.rmdir
功能:刪除空的目錄
語法:rmdir [OPTION]... DIRECTORY...
選項:
-p:連同上一級『空的』目錄也一塊兒刪除
-v:顯示過程
示例:
rmdir: removing directory, `a' [root@localhost ~]# rmdir -v 1 #這裏報錯,是由於1不是空目錄,經過tree就看的出。 rmdir: removing directory, `1' rmdir: failed to remove `1': Directory not empty [root@localhost ~]# tree 1 1 └── 2 └── 3 └── 4 3 directories, 0 files [root@localhost ~]# rmdir -p 1 #使用遞歸刪除時這樣寫刪除也是會報錯 rmdir: failed to remove `1': Directory not empty [root@localhost ~]# rmdir -p 1/2/3/4 #這樣寫的意思就是先刪除4,再刪除3,依次遞歸
3.10.touch
功能
建立文件,更改文件的時間戳
#當目標文件不存在:建立新的,空的文件 touch filename touch file1 file2 touch {a,b,c}.txt touch file{1..5} #當文件命有特殊字符時,能夠進行以下轉義: touch a\ b\ c touch "a b c" touch 'a b c' #當目標文件存在時,touch修改文件的時間戳: # stat file1 查看文件詳細信息(時間戳) File: `file1' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: fd00h/64768dInode: 275119 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2016-03-30 14:34:51.724484689 +0800 訪問時間 Modify: 2016-03-30 14:34:45.915484930 +0800 修改時間 Change: 2016-03-30 14:34:45.915484930 +0800 屬性時間 touch -a file1 -t 202011111111 修改文件的訪問時間 touch -m file1 -t 201011111212 修改文件的修改時間 touch -d 20201010 filename 修改文件的訪問時間和修改時間 block 塊,操做系統塊是一次IO的單位,系統塊默認大小爲4096字節;之後oracle裏也會學到塊的概念,oracle塊默認爲8192字節 inode 是文件系統給文件的一個編號,相似於文件的一個門牌號碼 一個優化有關的問題: 塊大好,還小好? 塊大,一次IO數據量大,IO吞吐量大,空間浪費較多 塊小,正好和塊大相反 批量建立文件: 第一種:touch {1..10} 第二種seq 11 2 20 |xargs touch seq產生一個序列, |符號是管道(表示把前面的結果傳到後面) 第三種:寫shell腳本作循環 #!/bin/bash . /etc/init.d/functions for i in `seq 10`;do touch file$i && File_Status=$? if [ $File_Status -eq 0 ];then action "this is file$i successed" /bin/true else action "this is file$i failed" /bin/false fi done
11.tree
功能:以樹狀圖列出目錄的內容;
語法:語法很繁瑣,這裏就不介紹了
經常使用選項:
-d:只顯示目錄
-L level:指定顯示的層級數目
-P pattern:只顯示由指定pattern匹配到的路徑,這裏的P是大寫;
-p:列出權限
示例:
[root@localhost ~]# ll total 444 -rw-------. 1 root root 1148 Apr 5 10:38 anaconda-ks.cfg drwxr-xr-x 2 root root 4096 May 22 10:52 b drwxr-xr-x 2 root root 4096 May 22 10:52 c drwxr-xr-x 2 root root 4096 May 22 10:52 d drwxr-xr-x 2 root root 4096 May 22 10:52 e -rw-r--r-- 1 root root 389473 May 9 15:22 inotify-tools-3.13.tar.gz drwxr-xr-x 2 root root 4096 May 22 10:48 install -rw-r--r--. 1 root root 17957 Apr 5 10:38 install.log -rw-r--r--. 1 root root 4178 Apr 5 10:37 install.log.syslog drwxr-xr-x 2 root root 4096 May 22 10:48 tmp [root@localhost ~]# tree -d . . ├── b ├── c ├── d ├── e ├── install └── tmp [root@localhost ~]# mkdir -p 1/2/3/4/5/6 [root@localhost ~]# tree -L 2 1 #從1開始的2個層級,到3 1 └── 2 └── 3 2 directories, 0 files [root@localhost ~]# tree 1 #不帶L參數,所有層級都顯示 1 └── 2 └── 3 └── 4 └── 5 └── 6 5 directories, 0 files [root@localhost ~]# tree -L 3 1 #從1開始的3個層級,到4 1 └── 2 └── 3 └── 4 [root@localhost ~]# tree -P *.gz . . ├── 1 │ └── 2 │ └── 3 │ └── 4 │ └── 5 │ └── 6 ├── b ├── c ├── d ├── e ├── inotify-tools-3.13.tar.gz ├── install └── tmp [root@localhost ~]# tree -P *.log . . ├── 1 │ └── 2 │ └── 3 │ └── 4 │ └── 5 │ └── 6 ├── b ├── c ├── d ├── e ├── install ├── install.log └── tmp [root@localhost ~]# tree -p *.gz . inotify-tools-3.13.tar.gz [error opening dir] . ├── [drwxr-xr-x] 1 │ └── [drwxr-xr-x] 2 │ └── [drwxr-xr-x] 3 │ └── [drwxr-xr-x] 4 │ └── [drwxr-xr-x] 5 │ └── [drwxr-xr-x] 6 ├── [-rw-------] anaconda-ks.cfg ├── [drwxr-xr-x] b ├── [drwxr-xr-x] c ├── [drwxr-xr-x] d ├── [drwxr-xr-x] e ├── [-rw-r--r--] inotify-tools-3.13.tar.gz ├── [drwxr-xr-x] install ├── [-rw-r--r--] install.log ├── [-rw-r--r--] install.log.syslog └── [drwxr-xr-x] tmp
12.basename
功能:打印路徑的最後一個後綴
語法:basename NAME [SUFFIX]
basename OPTION
經常使用選項:
-V:查看版本
man手冊示例:
basename /usr/bin/sort
Output "sort".
basename include/stdio.h .h
Output "stdio".
跟dirname恰好相反
驗證下:
[root@localhost scripts]# basename /usr/local/bin bin [root@localhost scripts]# ls read01.sh read01.sh [root@localhost scripts]# basename read01.sh read01.sh
13.dirname
功能:去除路徑的最後一個後綴
語法:dirname NAME
dirname OPTION
man手冊的示例:
dirname /usr/bin/sort
Output "/usr/bin"
dirname stdio.h
Output "."
驗證下:
[root@localhost test]# dirname /usr/local/bin/ /usr/local [root@localhost scripts]# ls read01.sh read01.sh [root@localhost scripts]# dirname read01.sh
14.chattr
功能:更改文件的隱藏屬性
語法格式:chattr [ -RVf ] [ -v version ] [ mode ] files…
經常使用選項:
-R 遞歸更改
-V 顯示詳細信息
-f 忽略大部分錯誤信息
-v version 設置文件的檔案號碼
mode 設置文件的隱藏屬性,其格式爲+-=[acdeijstuACDST]
最關鍵的是在[mode]部分,[mode]部分的格式是+-=[acdeijstuACDST],這部分是用來設置文件的屬性。其中+表示在原有參數設定基礎上追加參數;-表示在原有參數設定基礎上移除參數;=表示更新爲指定參數。下面列出幾個經常使用的屬性參數的含義:
屬性 | 含義 |
A | 文件的atime(access time)不可被修改,這樣能夠減小磁盤I/O數量,對於筆記本電腦有利於提升續航能力 |
S | 硬盤I/O同步選項,功能相似sync |
a | 即append,設定該參數後,只能向文件中添加數據,而不能刪除,多用於服務器日誌文件安全,只有root才能設定這個屬性 |
i | 文件不能被刪除、更名、設定連接關係,同時不能寫入或新增內容(即便是root用戶)。只有root才能設定這個屬性 |
c | 即compresse,文件會自動的經壓縮後再存儲,讀取時會自動的解壓 |
d | 即no dump,設定文件不能成爲dump程序的備份目標 |
j | 即journal,設定此參數使得當經過mount參數」data=ordered」或」data=writeback」掛載的文件系統,文件在寫入時會先被記錄(在journal中)。若是filesystem被設定參數爲data=journal,則該參數自動失效 |
s | 即secure,保密選項。設置了s屬性的文件在被刪除時,其全部數據塊會被寫入0 |
u | 即undelete,反刪除選項。與s相反,文件在被刪除時,其全部的數據塊都保留着,用戶從此能夠恢復該文件 |
示例:
這條命令,能夠將MySecretDir目錄下的文件設置爲不容許任何人修改: $ sudo chattr -R =i ~/MySecretDir # chattr +A 1.txt --這個屬性讓文件的access time訪問後也不會變化,節省了IO(IO優化);默認狀況下rhel5,訪問一次atime就會跟着變一次,rhel6下只有當atime等於或早於mtime和ctime之一時,纔會訪問跟着變,不然不會變。(緣由咱們在後面講mount參數時再補充) # stat 1.txt |tail -3 --用cat命令訪問這個文件,再用此命令查看access time,會發現再也不變化 Access: 2014-07-17 11:12:58.589838878 +0800 Modify: 2014-07-17 11:12:58.589838878 +0800 Change: 2014-07-17 11:13:15.405701371 +0800 # chattr -a 1.txt # chattr +i 1.txt --加了i屬性,此文件不可被修改,重命名
15.lsattr
功能:列出文件的隱藏屬性
語法格式:lsattr [ -RVadv ] [ files… ]
經常使用選項:
選項 | 含義 |
---|---|
-R | 遞歸顯示 |
-V | 顯示lsattr程序的版本信息 |
-a | 顯示全部文件的屬性信息,包括以.開頭的文件 |
-d | 顯示目錄的屬性,而不是目錄下的文件的屬性 |
-v | 顯示文件的檔案號碼 |
示例:
這條命令顯示MySecretDir目錄的隱藏屬性 $ lsattr -Rd MySecretDir/ ----i----------- MySecretDir/ 一個文件加了a屬於,重定向覆蓋之前的內容不容許,但追加內容是能夠的 # lsattr 1.txt -----a-------e- 1.txt # echo 456 > 1.txt bash: 1.txt: Operation not permitted # echo 456 >> 1.txt
16.file
功能:鑑定文件類型
# file /dev/tty1 /dev/tty1: character special 字符設備:輸入輸出 輸入設備:鍵盤、鼠標 輸出設備:顯示器、打印機 # file /dev/sda /dev/sda: block special 塊設備:軟盤|U盤|磁盤|光盤 # file /dev/stdin /dev/stdin: symbolic link to `/proc/self/fd/0' 連接文件,相似windows下快捷方式
17.MD5sum
功能:用於生成和校驗文件的md5值
md5校驗,有很小的機率不一樣的文件生成的md5可能相同。比md5更安全的校驗算法還有SHA*系列的
md5值是一個128位的二進制數據。
在網絡傳輸時,咱們校驗源文件得到其md5sum,傳輸完畢後,校驗其目標文件,並對好比果源文件和目標文件md5 一致的話,則表示文件傳輸無異常。不然說明文件在傳輸過程當中未正確傳輸。
經常使用選項:
-b 以二進制模式讀入文件內容
-t 以文本模式讀入文件內容
-c 根據已生成的md5值,對現存文件進行校驗
--status 校驗完成後,不生成錯誤或正確的提示信息,能夠經過命令的返回值來判斷。
示例:
生成文件md5值 md5sum file [root@localhost scripts]# md5sum 123.sh 5f745455dc1be7dea720d7de894c9eea 123.sh [root@localhost scripts]# md5sum 1234.sh 5f745455dc1be7dea720d7de894c9eea 1234.sh 說明:同一文件,不一樣名稱,單md5值是同樣,說明md5值效驗的是文件內容,跟文件名無關 讀取md5值 [root@localhost ~]# md5sum 123 f66d7df84020cbed01c9fdbb714d2079 123 [root@localhost ~]# file 123 123: ASCII text [root@localhost ~]# md5sum 123 f66d7df84020cbed01c9fdbb714d2079 123 [root@localhost ~]# md5sum -b 123 f66d7df84020cbed01c9fdbb714d2079 *123 [root@localhost ~]# md5sum -t 123 f66d7df84020cbed01c9fdbb714d2079 123 md5值重定向 將生成md5值重定向到指定的文件,一般文件的擴展名咱們會命爲.md5 [root@localhost ~]# md5sum 123 > 123.md5 [root@localhost ~]# cat 123.md5 f66d7df84020cbed01c9fdbb714d2079 123 [root@localhost ~]# md5sum 123 f66d7df84020cbed01c9fdbb714d2079 123 將多個文件的md5重定向到指定的文件每一個文件的md5生成爲一行 [root@localhost ~]# md5sum 123* >1.md5 [root@localhost ~]# cat 1.md5 f66d7df84020cbed01c9fdbb714d2079 123 f66d7df84020cbed01c9fdbb714d2079 1234 f66d7df84020cbed01c9fdbb714d2079 123.bak 1dd5e6301ec14b12e8f51fb319477884 123.md5 重定向追加這裏新增文件ls,單獨求其md5,將其md5追加到文件中 [root@master lianxi]# cp /bin/ls . [root@localhost ~]# md5sum ls >>1.md5 [root@localhost ~]# cat 1.md5 f66d7df84020cbed01c9fdbb714d2079 123 f66d7df84020cbed01c9fdbb714d2079 1234 f66d7df84020cbed01c9fdbb714d2079 123.bak 1dd5e6301ec14b12e8f51fb319477884 123.md5 ca226dd605e91b72e0d2060a6357c28f ls md5校驗 -c選項來對文件md5進行校驗。校驗時,根據已生成的md5來進行校驗。生成當前文件的md5,並和以前已經生成的md5進行對比,若是一致,則返回OK,不然返回錯誤信息 [root@localhost ~]# md5sum -c 1.md5 123: OK 1234: OK 123.bak: OK 123.md5: OK ls: OK 修改文件後,文件md5變化 [root@localhost ~]# md5sum 123 99d43d51be42c65df4b0f3177c07cbed 123 [root@localhost ~]# md5sum 123 >123.md5 [root@localhost ~]# echo "yyl" >>123 [root@localhost ~]# md5sum 123 ac2c2d832d38424b8ef996c8074f6c18 123 [root@localhost ~]# md5sum -c 123.md5 123: FAILED md5sum: WARNING: 1 of 1 computed checksum did NOT match --status,不顯示校驗信息,以命令返回值來判斷,校驗一致返回0,不一致返回1 [root@localhost ~]# md5sum -c --status 123.md5 [root@localhost ~]# echo $? 1 多個文件文件校驗和grep連用經過grep將正確的信息過濾掉 [root@master lianxi]# md5sum -c ../value.md5 acpid: OK acpid.1: OK anaconda.log: OK anaconda.syslog: OK anaconda.xlog: OK boot.log: OK boot.log.1: OK ... 省略中間部分 yum.log.2: OK 15: md5sum: WARNING: 1 of 56 computed checksums did NOT match [root@master lianxi]# md5sum -c ../value.md5 | grep -v OK md5sum: WARNING: 1 of 56 computed checksums did NOT match cron.1: FAILED
特殊說明
1)md5sum 是校驗文件內容,與文件名是否相同無關
2)md5sum值逐位校驗,因此文件越大,校驗時間越長。
總結
經過md5sum來校驗生成文件校驗碼,來發現文件傳輸(網絡傳輸、複製、本地不一樣設備間的傳輸)異常形成的文件內容不一致的狀況。
工做 實踐應用場景:
須要比較2個rc1.tar.gz 包和rc2.tar.gz兩個包,變動是否是和開發說的一致 1.獲取包,確保包正確無誤 獲取包後,驗證包的MD5值:md5sum rc*.tar.gz 2. 解壓縮到指定目錄 確保對應的目錄存在 tar -zxvf rc1.tar.gz -C ./test_rc1 tar -zxvf rc2.tar.gz -C ./test_rc2 3. 遞歸生成各文件的的MD5值 find ./test_rc1 -type f -print0| xargs -0 md5sum >> rc1_md5.txt find ./test_rc2 -type f -print0| xargs -0 md5sum >> rc2_md5.txt 4. 比較兩文件的MD5值 diff -c rc1_md5.txt rc2_md5.txt 或者用UltraCompare Professional比對結果
練習:
(1)如何建立目錄/tmp/x/y1,/tmp/x/y2,/tmp/x/y1/a, /tmp/x/y1/b, /tmp/x/y2/a,/tmp/x/y2/b,要求一次命令運行?
[root@localhost ~]# mkdir /tmp/x /tmp/x/{y1,y2} /tmp/x/y1/{a,b} /tmp/x/y2/{a,b} [root@localhost ~]# tree /tmp/x /tmp/x ├── y1 │ ├── a │ └── b └── y2 ├── a └── b 更簡單的方法: [root@localhost tmp]# mkdir -p /tmp/x/{y1,y2}/{a,b} [root@localhost tmp]# tree /tmp/x /tmp/x ├── y1 │ ├── a │ └── b └── y2 ├── a └── b
(2)如何建立目錄 x_m,y_m,x_n,y_n,要求一次命令運行?
[root@localhost yyl]# mkdir /tmp/{x,y}_{m,n} [root@localhost yyl]# ls /tmp/ x_m x_n y_m y_n
(3)如何建立目錄/tmp/bin, /tmp/sbin,/tmp/usr,/tmp/user/bin,/tmp/usr/sbin,要求一次命令運行?
[root@localhost tmp]# mkdir -p /tmp/{bin,sbin} /tmp/user/bin /tmp/usr/sbin [root@localhost tmp]# tree bin sbin user usr bin sbin user └── bin usr └── sbin
(4)看一個實驗
[root@localhost yyl]# touch test [root@localhost yyl]# cp test test1 [root@localhost yyl]# ll -i test test1 523308 -rw-r--r-- 1 root root 0 Nov 24 13:58 test 523309 -rw-r--r-- 1 root root 0 Nov 24 13:58 test1 [root@localhost yyl]# mv test test3 [root@localhost yyl]# ll -i test1 test3 523309 -rw-r--r-- 1 root root 0 Nov 24 13:58 test1 523308 -rw-r--r-- 1 root root 0 Nov 24 13:58 test3 解析:cp一個文件,新文件的inode會變,由523308到523309,mv一個文件,inode號不變,由523308到523308,這是同一分區不變,不一樣分區會改變嗎? [root@localhost yyl]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 18G 4.3G 13G 26% / tmpfs 491M 0 491M 0% /dev/shm /dev/sda1 477M 28M 425M 6% /boot /dev/mapper/VolGroup-lv01 4.8G 19M 4.6G 1% /data [root@localhost yyl]# mv test3 /data/test [root@localhost yyl]# ll -i test1 /data/test 12 -rw-r--r-- 1 root root 0 Nov 24 13:58 /data/test 523309 -rw-r--r-- 1 root root 0 Nov 24 13:58 test1
(5)如何把/etc/skel/目錄裏以.開頭的隱藏文件拷到/tmp目錄
有桌面能夠用下面命令打開目錄
nautilus /etc/skel/ --圖形打開一個目錄 # cp /etc/skel/* /tmp/ -rf --錯誤用法,*號不能表明隱藏文件 # cp /etc/skel/.* /tmp/ -rf --錯誤用法, .*也包括..,也就是會把上級目錄的東西也拷過去 方法一: # cp /etc/skel/.[a-Z]* /test/ -a或-rf --正確作法 方法2: #ls -a /etc/skel/ |xargs -i cp {} /test
(6)在/home目錄下建立yyl01/redhat/test目錄。而後在/home/yyl01下面建立文件file1~file5,而且修改file1的時間爲2020年的7月7號。
[root@localhost ~]# mkdir -p /home/yyl01/redhat/test [root@localhost yyl01]# touch /home/yyl01/file{1..5} [root@localhost yyl01]# touch -m file{1..5} -t 202007070707 [root@localhost home]# tree /home /home ├── yyl └── yyl01 ├── file1 ├── file2 ├── file3 ├── file4 ├── file5 └── redhat └── test [root@localhost home]# ll /home/yyl01/ total 4 -rw-r--r-- 1 root root 0 Jul 7 2020 file1 -rw-r--r-- 1 root root 0 Jul 7 2020 file2 -rw-r--r-- 1 root root 0 Jul 7 2020 file3 -rw-r--r-- 1 root root 0 Jul 7 2020 file4 -rw-r--r-- 1 root root 0 Jul 7 2020 file5 drwxr-xr-x 3 root root 4096 May 25 11:55 redhat
(7)將/home/yyl01下的file1~file5文件移動到/home/yyl01/redhat/test目錄下,而且重命名爲test1~test5
[root@localhost ~]# mv /home/yyl01/file{1..5} /home/yyl01/redhat/test/ [root@localhost ~]# cd /home/yyl01/redhat/test/ [root@localhost test]# rename "file" "test" * [root@localhost test]# ll total 0 -rw-r--r-- 1 root root 0 May 25 14:21 test1 -rw-r--r-- 1 root root 0 May 25 14:21 test2 -rw-r--r-- 1 root root 0 May 25 14:21 test3 -rw-r--r-- 1 root root 0 May 25 14:21 test4 -rw-r--r-- 1 root root 0 May 25 14:21 test5 方法2 for i in `ls ./*`;do mv $i `echo $i|sed 's/file/test/g'`;done #for循環加sed更名
(8)將/home/yyl01/redhat/test下的test5文件遠程拷貝到192.168.5.1上面的/home/test目錄下,而且重命名成本身名字。遠程服務器用戶redhat用戶,密碼是123
[root@localhost test]# scp /home/yyl01/redhat/test/file5 redhat@192.168.5.1:/home/test/yyl
(9)刪除/home/yyl01目錄下的redhat目錄
[root@localhost test]# cd /home/yyl01/ [root@localhost yyl01]# ls redhat [root@localhost yyl01]# rm -rf redhat