1.為什么要有查找?
文件很多,所以需要通過查找篩選
2.windows的查找方式
計算機(jī) ---->搜索框 查找
3.linux 系統(tǒng)的查找方式
沒有圖形工具
使用 find命令查找
4.find的查找語法
命令 路徑 選項 表達(dá)式 動作
find path.... options expresstion action
5.按照文件的名稱,類型,大小,修改時間等方式查找
1.按照文件的名稱查找 -name
1.按照名稱進(jìn)行查找
[root@oldboyedu ~]# find ./ -name "*eth0" #在當(dāng)前目錄下查找包含"eth0"的文件
2.按照名稱查找(不區(qū)分大小寫)
[root@oldboyedu ~]# find ./ -iname "*eth0" #在當(dāng)前目錄下查找包含"eth0"的文件,不區(qū)分大小寫
2.按照文件的大小查找 -size
1.查找/etc/目錄下大于5M的文件
[root@oldboyedu ~]# find /etc/ -size +5M 大于5M 用 -size +5M
2.查找/etc/目錄下小于5M的文件
[root@oldboyedu ~]# find /etc/ -size -5M 小于5M 用 -size -5M
2.查找/etc/目錄下等于5M的文件
[root@oldboyedu ~]# find /etc/ -size 5M 等于5M 用 -size 5M
3.按照文件的類型查找 -type
-f 文件
-d 目錄
-s socket套接字文件
-l 鏈接文件
-c 字符設(shè)備
-b 塊設(shè)備
1.查找當(dāng)前目錄下類型是文件的,并且名稱跟eth0相關(guān)的都列出來
[root@oldboyedu ~]# find ./ -type f -iname "*eth0" |
xargs ls -l
2.查找/etc/目錄下類型是文件的,大小是大于5M,名稱以.bin結(jié)尾的
[root@oldboyedu ~]# find /etc/ -type f -size +5M -
name "*.bin"
3.查找/etc/目錄下類型是文件的,名稱是.repo結(jié)尾的
[root@oldboyedu ~]# find /etc/ -type f -name "*.repo"
4.查找/dev下的類型是塊設(shè)備的,并名稱是sda開頭的
[root@oldboyedu ~]# find /dev/ -type b -name "sda*" |
xargs ls -l
4.查找/dev下的類型是字符設(shè)備,并名稱是tty開頭的
[root@oldboyedu ~]# find /dev/ -type c -name "tty*"
4.按照修改時間查找 -mtime
[root@oldboyedu ~]# for i in {1..31}; do date -s
"2019/08/i ; done
1. 第7天
[root@oldboyedu ~]# find ./ -type f -mtime 7
2. 7天以前的內(nèi)容都會被篩選出來,然后刪除. 保留了最近7天的內(nèi)容
[root@oldboyedu ~]# find ./ -type f -mtime +7 -name
"file-*"
3.最近7天的內(nèi)容都會被篩選出來
[root@oldboyedu ~]# find ./ -type f -mtime -7 -name
"file-*"
4. 本地文件保留最近7天的備份文件, 備份服務(wù)器保留3個月的備份文件
(實際使用方案)
find /backup/ -iname ".bak" -mtime +7 -delete
find /backup/ -iname ".bak" -mtime +180 -delete
5.按用戶和組查找 -user -group -nouser -nogroup
查找屬主是jack
[root@xuliangwei ~]# find /home -user jack
查找屬組是admin
[root@xuliangwei ~]# find /home -group admin
查找屬主是jacky, 屬組是jack
[root@oldboyedu ~]# find /home/ -type d -user jacky -
group jack
查找沒有屬主
[root@xuliangwei ~]# find /home -nouser
查找沒有屬組
[root@xuliangwei ~]# find /home -nogroup
查找沒有屬主或?qū)俳M
[root@xuliangwei ~]# find / -nouser -nogroup
6.find 查找后的處理動作
查找到文件以后,需要對文件如何進(jìn)行, find動作的默認(rèn)是 #print (打印)
動作 含義
-print 打印查找到的內(nèi)容(默認(rèn)) ---ignore
-ls 以長格式顯示的方式打印查找到的內(nèi)容 ---ignore | xargs ls -l
-delete 刪除查找到的文件 (刪除目錄,僅能刪除空目錄) ---ignore | xargs
rm -f
-ok 后面跟自定義 shell 命令(會提示是否操作) ---ignore
-exec 后面跟自定義 shell 命令(標(biāo)準(zhǔn)寫法 -exec ;) | xargs
例如:[root@oldboyedu ~]# time find ./ -type f -name
"file" -exec rm -f {} ;
real 0m6.585s
user 0m3.617s
sys 0m3.532s
[root@oldboyedu ~]# time find ./ -type f -name
"file" | xargs rm -f
real 0m0.152s
user 0m0.016s
sys 0m0.146s
用 -exec rm -f {}; 和 |xargs rm -f 刪除的對比 ,第二種比第一種要快捷一點
查找/var/log/ 類型是文件的,并且名稱是.log結(jié)尾的,并且7天以前
的,然后刪除 ------>三種方式
[root@oldboyedu ~]# #find /var/log/ -type f -name
".log" -mtime +7 -exec rm -f {} ;
[root@oldboyedu ~]# #find /var/log/ -type f -name
".log" -mtime +7 -delete
[root@oldboyedu ~]# #find /var/log/ -type f -name
"*.log" -mtime +7 | xargs rm -f
- find 的邏輯運算符
符號 作用
-a 與
-o 或
-not|! 非(not 不常用 ,一般用 ! )
1.查找當(dāng)前目錄下,屬主不是root的所有文件
[root@oldboyedu ~]# find /home/ ! -user root -ls
[root@oldboyedu ~]# find /home/ -not -user root -ls #
使用較少
2.查找當(dāng)前目錄下,屬主屬于jacky通殃,并且大小大于1k的文件
[root@oldboyedu ~]# find /home/ -type f -a -user jacky
-a -size +1k
3.查找當(dāng)前目錄下的屬主為root 或者 以xml結(jié)尾的普通文件
[root@xuliangwei ~]# find . -type f -a ( -user root -
o -name '*.xml' )