今日內(nèi)容
1.為什么要有文件查找?
資料很多,忘記放哪了,所以需要通過查找的方式進(jìn)行搜索.
2.windows如何實(shí)現(xiàn)文件查找?
計(jì)算機(jī)-->搜索框
3.linux如何實(shí)現(xiàn)文件查找?
1.沒有圖形工具
2.使用命令工具搜索--->find
4.find命令查找語法?
命令 路徑 選項(xiàng) 表達(dá)式 動(dòng)作
find [path...] [options] [expression] [action]
查找 地區(qū) 妹紙 18-25歲 ???
5.find針對(duì)文件名稱窖梁、類型嗅定、大小、修改時(shí)間等方式進(jìn)行查找文 件?
- 按名稱查找:
1.按照名稱進(jìn)行查找 [root@oldboyedu ~]# find ./ -name "*eth0"
2.按照名稱查找(不區(qū)分大小寫) [root@oldboyedu ~]# find ./ -iname "*eth0"
- 按文件大小查找 size
1.查找/etc/目錄下大于5M的文件
[root@oldboyedu ~]# find /etc/ -size +5M
2.查找/etc/目錄下小于5M的文件
[root@oldboyedu ~]# find /etc/ -size -5M
3.查找/etc/目錄下等于5M的文件
[root@oldboyedu ~]# find /etc/ -size 5M
- 按文件類型查找 -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
5.查找/dev下的類型是字符設(shè)備,并名稱是tty開頭的 [root@oldboyedu ~]# find /dev/ -type c -name "tty*"
- 按修改時(shí)間進(jìn)行查找 -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)容都會(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-*"
4.本地文件保留最近7天的備份文件, 備份服務(wù)器保留3個(gè)月的備份文件 (實(shí)際使用方案)
find /backup/ -iname ".bak" -mtime +7 -delete
find /backup/ -iname ".bak" -mtime +180 -delete
- 按用戶和組進(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
- l 刪除查找到的文件 (刪除目錄,僅能刪除空目錄) ---ignore | xargs delete 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
3.記得文件的內(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
4.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' )
5.find練習(xí)
1.查找/var目錄下谊却,屬主不是root柔昼,且文件名不以f開頭的文件 [root@oldboyedu ~]# find /var/ -type f ! -user root a ! -name "f*"
2.查找/var目錄下屬主為root,且屬組為mail的所有文件 [root@oldboyedu ~]# find /var/ -type f -user root -a group mail
3.查找/var目錄下不屬于root炎辨、lp的所有文件 [root@oldboyedu ~]# find /var/ -type f ! -user root -a ! -user lp
4.查找/var目錄下最近一周內(nèi)產(chǎn)生的文件捕透,同時(shí)屬主不為root,也不是 postfix的文件 [root@oldboyedu ~]# find /var/ -type f -mtime -7 ! user root ! -name "postfix"
5.查找/etc目錄下大于1M且類型為普通文件的所有文件 [root@oldboyedu ~]# find /etc/ -type f -size +1M
6.將/etc/中的所有目錄(僅目錄)復(fù)制到/tmp下碴萧,目錄結(jié)構(gòu)不變 [root@oldboyedu ~]# find /etc/ -type d -exec mkdir -p /tmp/{} ;
7.將/etc目錄復(fù)制到/var/tmp/,/var/tmp/etc的所有目錄權(quán)限 777/var/tmp/etc目錄中所有文件權(quán)限666 [root@oldboyedu ~]# cp /etc/ /var/tmp/ -rp [root@oldboyedu ~]# find /var/tmp/etc/ -type d -exec chmod 777 {} ; [root@oldboyedu ~]# find /var/tmp/etc/ -type f -exec chmod 666 {} ;
8.保留/var/log/下最近7天的日志文件,其他全部刪除 [root@oldboyedu ~]# find /var/log/ -type f -mtime +7 -exec rm -f {} ;
9.創(chuàng)建touch file{1..10}10個(gè)文件, 保留file9,其他一次全部刪除 [root@oldboyedu ~]# find ./ -type f -name "file*" ! name "file9" -exec rm -f {} ;
10.解釋如下每條命令含義
mkdir /root/dir1
ouch /root/dir1/file{1..10}
find /root/dir1 -type f -name "file5"
find /root/dir1 ! -name "file5"
find /root/dir1 -name "file5" -o -name "file9"
find /root/dir1 -name "file5" -o -name "file9" -ls
find /root/dir1 ( -name "file5" -o -name "file9" ) ls
find /root/dir1 ( -name "file5" -o -name "file9" ) exec rm -vf {} ;
find /root/dir1 ! ( -name "file4" -o -name "file8" ) -exec rm -vf {} ;
—————————————————————————————————————
今日總結(jié)
1.find 查找文件
文件名稱
文件大小
文件類型
修改時(shí)間
用戶用戶組
2.find處理動(dòng)作
- print 默認(rèn)送 (忽略)
- ls 長(zhǎng)格式顯示,不能加參數(shù),(忽略)
- delete 刪除文件,刪除目錄必須確保目錄為空
- ok 執(zhí)行command命令,但會(huì)提示,(忽略)
- exec 執(zhí)行command命令
3.find的邏輯運(yùn)算
與 -a
或 -o
非 ! | -not