1.為什么要有文件查找
資料很多,忘記放哪了,所以需要通過查找的方式進(jìn)行搜索.
2.windows如何實(shí)現(xiàn)文件查找
計(jì)算機(jī)-->搜索框
3.linux如何實(shí)現(xiàn)文件查找
1.沒有圖形工具
2.使用命令工具搜索--->find
命令? 路徑? ? ? ? 選項(xiàng)? ? ? ? ? ?表達(dá)式? ? ? ? ? ? 動(dòng)作
find? ?[path...]? ? [options]? ? ?[expression]? ?[action]
查找? 地區(qū)? ? ? ? ?妹紙? ? ? ? ? 18-25歲? ? ? ? ? ????
4.find 命令查找語法
5.find 針對(duì)文件名稱鉴分、類型、大小、修改時(shí)間等方式進(jìn)行查找文件
1) 按名稱查找 -name? ?
#1.按照名稱進(jìn)行查找
[root@oldboyedu ~]# find ./ -name "*eth0"
#2.按照名稱查找(不區(qū)分大小寫)
[root@oldboyedu ~]# find ./ -iname "*eth0"
2) 按文件大小查找 size
#1.查找/etc/目錄下大于5M的文件
[root@oldboyedu ~]# find /etc/ -size +5M
#2.查找/etc/目錄下小于5M的文件
[root@oldboyedu ~]# find /etc/ -size -5M
#2.查找/etc/目錄下等于5M的文件
[root@oldboyedu ~]# find /etc/ -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) 按修改時(shí)間進(jìn)行查找 -mtime
[root@oldboyedu ~]# for i in {1..31}; do date -s"2019/08/$i" ; touch file-$i ; done
#1.第7天
[root@oldboyedu ~]# find ./ -type f -mtime 7
#2.7天以前的內(nèi)容都會(huì)被篩選出來,然后刪除. 保留了最近7天的內(nèi)容
[root@oldboyedu ~]# find ./ -type f -mtime +7 -name "file-*"
#3.最近7天的內(nèi)容都會(huì)被篩選出來
[root@oldboyedu ~]# find ./ -type f -mtime -7 -name "file-*"
#5.本地文件保留最近7天的備份文件, 備份服務(wù)器保留3個(gè)月的備份文件
(實(shí)際使用方案)
find /backup/ -iname "*.bak" -mtime +7 -delete
find /backup/ -iname "*.bak" -mtime +180 -delete
5) 按用戶和組進(jìn)行查找 -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查找后的處理動(dòng)作
查找到一個(gè)文件后剑按,需要對(duì)文件進(jìn)行如何處理,find的默認(rèn)動(dòng)作是 -print
動(dòng)作? ? ? ? ? 含義
-print? ? ? ? 打印查找到的內(nèi)容(默認(rèn)) ---ignore
-ls? ? ? ? ? ? ?以長(zhǎng)格式顯示的方式打印查找到的內(nèi)容 ---ignore | xargs ls -l
-delete? ? ? 刪除查找到的文件 (刪除目錄,僅能刪除空目錄) ---ignore | xargs rm -f
-ok? ? ? ? ? ? 后面跟自定義 shell 命令(會(huì)提示是否操作) ---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
#查找/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
7.記得文件的內(nèi)容是什么,但是不清楚文件名稱是什么贸辈,也不知道路徑在哪兒
# find? 是查詢文件
# grep 過濾內(nèi)容
#將ifnd查詢的文件結(jié)果,作為grep的參數(shù)
[root@oldboyedu ~]# find /etc/ -type f | xargs grep "log_group" --color=auto
/etc/audit/auditd.conf:log_group = root
8.find邏輯運(yùn)算符
符號(hào)? ? 作用
-a? ? ? ? 與
-o? ? ? ? 或
-not|!? ?非
#1.查找當(dāng)前目錄下,屬主不是root的所有文件
[root@oldboyedu ~]# find /home/ ! -user root -ls
[root@oldboyedu ~]# find /home/ -not -user root -ls? #使用較少
#2.查找當(dāng)前目錄下肠槽,屬主屬于jack擎淤,并且大小大于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 hdfs -o -name '*.xml' \)