grep\egrep fgrep 文本查找
文件查找:
locate
非實時的,模糊匹配,根據全系統數據庫完成的。
如:locate passwd
#updatedb ,手動生成文件數據庫
優勢:速度快
find
實時查找
精確
支持衆多查找標準
遍歷指定目錄中所有文件完成查找,速度慢。
find 查找路徑 查找標準 查找到以後的處理運作
查找路徑:默認爲當前目錄
查找標準:默認指定路徑下所有文件
處理動作:默認顯示
匹配標準:
-name 'FILENAME': 對文件名做精確的匹配
文件名通配:
* :
?
[]
[^]
如:find /etc -name 'pass*'
-iname 'FILENAME':文件名匹配不區分大小寫
-regex PATTERN:基於正則表達式進行文件名匹配
-user USERNAME:根據屬主查找
-group GROUPNAME:根據屬組查找
-uid UID:根據uid查找
-gid GID:根據gid查找
-nouser:沒有屬主的文件
-nogroup:沒有屬組的文件
-type:根據文件類型來查找
f:普通文件
d:目錄文件
c:字符文件
b:塊文件
l:鏈接文件
p:管道文件
s:套接字文件
如: find /etc -type s
-size:根據文件大小來查找 [+|-] + 大於 -小於 #精確
#k
#M
#G
如:find /etc -size 10k
組合條件:
-a 默認與條件
-o或
-not 非
如:find /etc -nouser -a -type d -ls
find /etc -not -type d
find /tmp -not -user user1 -a -not -user user2 或者 find /tmp -not \(-user user1 -o -user user2\) 德摩根定律
find /etc-not -user user1 -o -not -type d 或者 find /etc -not \(-user user1 -a -type d\)
-mtime
-ctime
-atime
+:表示#天之前
-:表示最近#天之內
不帶+-號:#天有過一次
如:find /etc -atime +5 (在/etc目錄下有5天沒有被訪問的文件)
-perm mode :每一位權限都必須精確匹配。
如:find ./ -perm 644
/mode : 只要有一個權限匹配即可。
-mode :文件權限能完全包含此MODE時才能顯示
如:find ./ -perm -644
處理動作:
-print :顯示
-ls :類似ls -l的顯示文件的詳細信息
-ok command {} \; 每一個操作都需要用戶的確認。
-exec command {} \; exec不需要。
注:這裏的 {} 表示佔位符,代表文件名。
xargs 空格隔開
如:find ./ -perm -006 -exec chmod o-w {} \;
find ./ -type d -ok chmod +x {} \;
find ./ -perm -020 -ok mv {} {}.new \;
find ./ -name "*.sh" -a -perm -111 -exec chmod o-x {} \;
find練習:
1.查找/var目錄下屬主爲root並且屬組爲mail的所有文件
find /var -user root -a -group mail
2.查找/usr目錄下不屬於root,bin或student的文件。
寫法一:find /usr -not -user root -a -not -user bin -a -not -user student
寫法二:find /usr -not \(-user root -o -user bin -o -user student \)
3.查找/etc目錄下最近一週內內容修改過且不屬於root及student用戶的文件
find /etc -mtime -7 -not \(-user root -o -user student\)
find /etc -mtime -7 -not -user root -a -not -user student
4.查找當前系統上沒有屬主、沒有屬組,最近1天內被訪問過的文件,並將其屬主屬組修改爲root
find / \(-nouser -o -nogroup\) -a -atime -1 -exec chown root:root {} \;
5.查找/etc目錄下大於1M的文件,並將其文件名寫入到/tmp/etc.largefile文件中。
find /etc -size +1M -exec echo {} >>/tmp/etc.largefile \;
find /etc -size +1M | xargs echo >>/tmp/etc.largefile
6.查找/etc目錄下所有用戶都沒有寫權限的文件,顯示出詳細信息
find /etc -not -perm /222 -ls