5.find針對文件名稱天揖、類型、大小诸典、修改時間等方式進行查找文件?
1) 按名稱查找
1.按照名稱進行查找
[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
3.查找/etc/目錄下等于5M的文件
[root@oldboyedu ~]# find /etc/ -size 5M
3) 按文件類型查找 -type
f 文件
d 目錄
s socket套接字文件
l 鏈接文件
c 字符設(shè)備
b 塊設(shè)備
1.查找當前目錄下類型是文件的,并且名稱跟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" ; touch file-$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查找后的處理動作?
查找到一個文件后描函,需要對文件進行如何處理,find的默認動作是 -print
動作 含義
-print 打印查找到的內(nèi)容(默認) ---ignore
-ls 以長格式顯示的方式打印查找到的內(nèi)容 ---ignore | xargs ls -l
-delete 刪除查找到的文件 (刪除目錄,僅能刪除空目錄) ---ignore | xargs rm -f
-ok 后面跟自定義 shell 命令(會提示是否操作) ---ignore
-exec 后面跟自定義 shell 命令(標準寫法 -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邏輯運算符
練習
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)生的文件酸休,同時屬主不為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/中的所有目錄(僅目錄)復制到/tmp下,目錄結(jié)構(gòu)不變
[root@oldboyedu ~]# find /etc/ -type d -exec mkdir -p /tmp/{} \;
7.將/etc目錄復制到/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個文件, 保留file9,其他一次全部刪除
[root@oldboyedu ~]# find ./ -type f -name "file*" ! -name "file9" -exec rm -f {} \;
10.解釋如下每條命令含義
mkdir /root/dir1
touch /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 {} \;