14.查找文件(快樂的Linux命令行)

1.本節學的命令

locate Find files by name
find Search for files in a directory hierarchy
xargs Build and execute command lines from standard input
touch Change file times
stat Display file or file system status

 

2.小知識

知識點1:爲什麼第一天安裝系統,locate不可使用,而第二天就自動好了?

答:因爲需要運行updatedb程序,但是這個是隔一天週期性的運行的,所以數據庫不能持續的更新,可以手動運行更新。

知識點2:find文件類型有哪幾種?

b 塊特殊設備文件
c 字符特殊設備文件
d 目錄
f 普通文件
l 符號鏈接

知識點3:find的指令測試條件有哪些?

測試條件 描述
-cmin n 匹配內容或屬性最後修改時間正好在 n 分鐘之前的文件或目錄。 少於 n 分鐘之前,使用 -n,多於 n 分鐘之前,使用 +n。
-cnewer file 匹配內容或屬性最後修改時間晚於 file 的文件或目錄
-ctime n 匹配內容和屬性最後修改時間在 n*24小時之前的文件和目錄。
-empty 匹配文件和目錄。
-group name 匹配屬於一個組的文件或目錄。組可以用組名或組 ID 來表示。
-iname pattern 就像-name 測試條件,但是不區分大小寫。
-name pattern 用指定的通配符模式匹配的文件和目錄。
-inum n 匹配 inode 號是 n的文件。這對於找到某個特殊 inode 的所有硬鏈接很有幫助。
-mmin n 匹配內容被修改於 n 分鐘之前的文件或目錄
-mtime n 匹配的文件或目錄的內容被修改於 n*24小時之前。
-newer file 匹配內容晚於指定的文件的文件和目錄。 每次你製作一個備份,更新文件(比如說日誌),然後使用 find 命令來判斷哪些文件自從上一次更新之後被更改了。
-nouser 匹配不屬於一個有效用戶的文件和目錄。這可以用來查找 屬於被刪除的帳戶的文件或監測攻擊行爲。
-nogroup 匹配不屬於一個有效的組的文件和目錄。
-perm mode 匹配權限已經設置爲指定的 mode的文件或目錄。mode 可以用 八進制或符號表示法。
-samefile name 類似於-inum 測試條件。匹配和文件 name 享有同樣 inode 號的文件。
-size n 匹配大小爲 n 的文件
-type c 匹配文件類型是 c 的文件。
-user name 匹配屬於某個用戶的文件或目錄。這個用戶可以通過用戶名或用戶 ID 來表示。

知識點4:find操作符操作有哪些?

-and 如果操作符兩邊的測試條件都是真,則匹配。可以簡寫爲 -a。 注意若沒有使用操作符,則默認使用 -and
-or 若操作符兩邊的任一個測試條件爲真,則匹配。可以簡寫爲 -o。
-not 若操作符後面的測試條件是假,則匹配。可以簡寫爲一個感嘆號(!)。
() 把測試條件和操作符組合起來形成更大的表達式。這用來控制邏輯計算的優先級。 默認情況下,find 命令按照從左到右的順序計算。經常有必要重寫默認的求值順序,以得到期望的結果。 即使沒有必要,有時候包括組合起來的字符,對提高命令的可讀性是很有幫助的。注意 因爲圓括號字符對於 shell 來說有特殊含義,所以在命令行中使用它們的時候,它們必須 用引號引起來,才能作爲實參傳遞給 find 命令。通常反斜槓字符被用來轉義圓括號字符。

知識點4:預定義的find操作符有哪些?

-delete 刪除當前匹配的文件。
-ls 對匹配的文件執行等同的 ls -dils 命令。並將結果發送到標準輸出。
-print 把匹配文件的全路徑名輸送到標準輸出。如果沒有指定其它操作,這是 默認操作。
-quit 一旦找到一個匹配,退出。

 

3.代碼實操

 

[me@linuxbox ~]$ locate bin/zip
/usr/bin/zip
/usr/bin/zipcloak
/usr/bin/zipgrep
/usr/bin/zipinfo
/usr/bin/zipnote
/usr/bin/zipsplit
[me@linuxbox ~]$ locate zip | grep bin
/bin/bunzip2
/bin/bzip2
/bin/bzip2recover
/bin/gunzip
/bin/gzip
/usr/bin/funzip
/usr/bin/gpg-zip
/usr/bin/preunzip
/usr/bin/prezip
/usr/bin/prezip-bin
/usr/bin/unzip
/usr/bin/unzipsfx
/usr/bin/zip
/usr/bin/zipcloak
/usr/bin/zipgrep
/usr/bin/zipinfo
/usr/bin/zipnote
/usr/bin/zipsplit

[me@linuxbox ~]$ find ~
[me@linuxbox ~]$ find ~ | wc -l
47068
[me@linuxbox ~]$ find ~ -type d | wc -l
1695
[me@linuxbox ~]$ find ~ -type f | wc -l
38737
[me@linuxbox ~]$ find ~ -type f -name "*.JPG" -size +1M | wc -l
840

[me@linuxbox ~]$ find ~ \( -type f -not -perm 0600 \) -or \( -type d -not -perm 0700 \)
注:操作符操作

[me@linuxbox ~]$ find ~ -type f -name '*.BAK' -delete
[me@linuxbox ~]$ find ~ -type f -name '*.BAK' -print
[me@linuxbox ~]$ find ~ -type f -name 'foo*' -ok ls -l '{}' ';'
< ls ... /home/me/bin/foo > ? y
-rwxr-xr-x 1 me    me 224 2007-10-29 18:44 /home/me/bin/foo
< ls ... /home/me/foo.txt > ? y
-rw-r--r-- 1 me    me 0 2008-09-19 12:53 /home/me/foo.txt
[me@linuxbox ~]$ find ~ -type f -name 'foo*' -exec ls -l '{}' ';'
-rwxr-xr-x 1 me     me 224 2007-10-29 18:44 /home/me/bin/foo
-rw-r--r-- 1 me     me 0 2008-09-19 12:53 /home/me/foo.txt

[me@linuxbox ~]$ find ~ -type f -name 'foo*' -exec ls -l '{}' +
-rwxr-xr-x 1 me     me 224 2007-10-29 18:44 /home/me/bin/foo
-rw-r--r-- 1 me     me 0 2008-09-19 12:53 /home/me/foo.txt
注;過把末尾的分號改爲加號,就激活了 find 命令的一個功能,把搜索結果結合爲一個參數列表, 然後用於
所期望的命令的一次執行,而不是多次執行。

[me@linuxbox ~]$ find ~ -type f -name 'foo*' -print | xargs ls -l
-rwxr-xr-x 1 me     me 224 2007-10-29 18:44 /home/me/bin/foo
-rw-r--r-- 1 me     me 0 2008-09-19 12:53 /home/me/foo.txt

注:訓練
[me@linuxbox ~]$ mkdir -p playground/dir-{00{1..9},0{10..99},100}
[me@linuxbox ~]$ touch playground/dir-{00{1..9},0{10..99},100}/file-{A..Z}
[me@linuxbox ~]$ find playground -type f -name 'file-A'
[me@linuxbox ~]$ find playground -type f -name 'file-A' | wc -l
[me@linuxbox ~]$ touch playground/timestamp
[me@linuxbox ~]$ stat playground/timestamp
File: 'playground/timestamp'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 14265061 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1001/ me) Gid: ( 1001/ me)
Access: 2008-10-08 15:15:39.000000000 -0400
Modify: 2008-10-08 15:15:39.000000000 -0400
Change: 2008-10-08 15:15:39.000000000 -0400
[me@linuxbox ~]$ touch playground/timestamp
[me@linuxbox ~]$ stat playground/timestamp
File: 'playground/timestamp'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 14265061 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1001/ me) Gid: ( 1001/ me)
Access: 2008-10-08 15:23:33.000000000 -0400
Modify: 2008-10-08 15:23:33.000000000 -0400
Change: 2008-10-08 15:23:33.000000000 -0400
[me@linuxbox ~]$ find playground -type f -name 'file-B' -exec touch '{}' ';'
[me@linuxbox ~]$ find playground -type f -newer playground/timestamp
[me@linuxbox ~]$ find playground \( -type f -not -perm 0600 \) -or \( -type d -not -perm 0700 \)
[me@linuxbox ~]$ find playground \( -type f -not -perm 0600 -exec chmod 0600 '{}' ';' \)
   -or \( -type d -not -perm 0711 -exec chmod 0700 '{}' ';' \)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章