linux文件查找主要是locate、find.
locate
locate主要查找速度快,實時,但需要依賴系統文件數據庫,而這個文件數據庫需要手動生成:updatedb.
find用法
實時,精確,有衆多查找標準,它遍歷指定目錄的所有文件完成查找,速度慢。
語法:
find 查找路徑 查找標準 查到後處理command
查找路徑:默認爲當前目錄
查找標準:默認爲指定路徑下的所有文件
處理動作:默認爲顯示
匹配標準:
-name “filename”:對文件名進行精確匹配
文件名通配:
*:任意長度任意字符
?:
[]
-iname “filename” :文件名不區分大小寫。‘
-regex pattern :基於正則表達式進行文件名匹配
-user User:屬主屬於user用戶的文件
-group group 根據屬組查找
-uid UID
-gid GID
-nouser :查找沒有屬主的文件
-nogroup:查找沒有屬組的文件
-type
f:普通文件
d:目錄
c:字符設備
b:塊設備,二進制文件
l:鏈接文件
p:管道文件
s:套接字文件
-size
[+|-]#k
#M
#G
組合條件:【默認就是與條件】
-a與條件
-o或條件
-not 非條件
時間
-mtime修改時間
-ctime 改變時間
-atime 訪問時間
[+|-]#
-perm
mode 精確查找
-mode 文件權限能完全包含此mode時才符合條件
/mode 任意一位能匹配就行
-ok command {} \; 每一次都需要用戶確認
-exec command {} \;
常見案例練習
[root@desktop5 test]# find /etc/ -size +1M | xargs >> /tmp/etc.largefiles
1.查找/var目錄下屬主爲root並且屬組爲mail所有文件
[root@desktop5 test]# find /var/ -user root -group mail > /tmp/var.rootmail.file
2.查看/usr/目錄下不屬於root,bin,或student的文件
[root@desktop5 test]# find /usr/ -not \( -user root -o -user bin -o -user student \) -exec ls -al {} \;
-rwsr-xr-x. 1 abrt abrt 9856 Jan 24 2013 /usr/libexec/abrt-action-install-debuginfo-to-abrt-cache
3.查找/etc/目錄下最近一週內內容修改過且不屬於root及student用戶的文件
[root@desktop5 ~]# find /etc/ -mtime -7 -a -not \( -user root -o -user student \)
[root@desktop5 ~]# find /etc/ -mtime -7 -not -user root -a -not -user student
4.查找當前系統上沒有屬主或屬組且最近一天曾被訪問的文件,並將其屬主屬組修改爲root
[root@desktop5 ~]# find / -atime -1 -nouser -nogroup | xargs chown root:root {} \;
5.查找/etc/目錄下大於1M的文件,並將其寫入/tmp/etc.arggefile
[root@desktop5 ~]# find /etc/ -size +1M >> largefile
6.查找/etc/目錄下所有用戶都沒有寫權限的文件,顯示其詳細信息
[root@desktop5 ~]# find /etc/ -not -perm /222