grep,egrep,fgrep:文本查找(文本文件中的內容)
文件查找:
locate:
全系統查找的命令,
非實時,
模糊匹配
查找是根據全系統文件數據庫進行的
#updatedb,手動生成文件數據庫,因爲新建的系統可能沒有建立文件數據庫
速度快
find:
實時查找
精確查找
支持衆多查找標準
遍歷指定目錄中的所有文件完成查找,速度慢
用法:
find 查找路徑 查找標準 查找到以後的處理運作
查找路徑:默認爲當前目錄
查找標準:默認爲指定路徑下的所有文件
處理運作:默認爲顯示
匹配標準:
-name 'FILENAME':對文件名做精確匹配
文件名通配:
* :任意長度的任意字符
?:任意單個字符
[]:範圍以內的字符
-iname 'FILENAME':文件名匹配不區分大小寫
-regex PATTERN:基於正則表達式進行文件名匹配
-user USERNAME:根據屬主查找
-group GROUPNAME:根據屬組查找
-uid UID:根據UID查找
-gid GID:根據GID查找
-nouser :查找沒有屬主的文件
-nogroup:查找沒有屬組的文件
-type
f:普通文件
d:目錄
c:字符設備文件
b:塊設備文件
l:鏈接文件
p:管道文件
s:套接字文件
-size
[+|-]#k
[+|-]#M
[+|-]#G
組合條件:
-a:與(and)
-o:或(or)
-not:非
查找/tmp 目錄下,不是目錄,並且還不能是套接字類型的文件
find /tmp -not -type d -a -not -type s
find /tmp -not \( -type d -o -type s \)
查找/tmp/test 目錄下,屬主不是user1,也不是user2的文件;
find /tmp/test -not -user user1 -a -not -user user2
find /tmp/test -not \( -user user1 -o -user user2 \)
關於時間戳的參數:
-mtime
-ctime
-atime
[+|-]#
-mmin
-cmin
-amin
[+|-]#
關於權限的參數
-perm
MODE:精確匹配
/MODE:任意一位匹配即滿足條件
-MODE:文件權限能完全包含此MODE時才能顯示
find ./ -perm -644
-644
644:rw-r--r--
755:rwxr-xr-x
運作:
-print:顯示
-ls:類似ls -l的形式顯示每一個文件的詳細
-ok COMMOND {} \;每一次操作都需要用戶確認
-exec COMMOND {} \;不需要用戶確認
# find -type d -ok chmod a-x {} \;
# find ./ -perm -020 -exec 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 -a -not \( -user root -o -user student \)
4、查找當前系統上沒有屬主或屬組且最近1天內曾被訪問過的文件,並將其屬主屬組均修改爲root;
# find -nouser -o -nogroup -a -atime -1 -exec chown root {} \;
5、查找/etc目錄下大於1M的文件,並將其文件名寫入/tmp/etc.largefiles文件中;
# find /etc -size +1M -print >> /tmp/etc.largefiles
6、查找/etc目錄下所有用戶都沒有寫權限的文件,顯示出其詳細信息;
# find /tmp/test -not -perm /222 -ls