find常見文件查找相關彙總

find查找相關知識點:

常見用法實例:
find path -option [ -print ] [ -exec -ok command ] {} ;

特殊:

1、-exec command {} ;

find查找的內容交給{} 替代找到的文件

[root@ljc ~]# find  /tmp  -atime  +30  –exec rm –rf  {}  \; #刪除查找到的超過30天沒有訪問過文件

2、xargs

xargs參數傳遞,主要讓一些不支持管道的命令可以使用管道技術

# [root@ljc ~]# ls |xargs cp -rvt /tmp/ -或-> ls | xargs -I {} cp -rv {} /tmp/
# [root@ljc ~]# ls |xargs mv -t /tmp/   -或-> ls | xargs -I {}  mv {} /tmp
# [root@ljc ~]# find /root/dir1 \( -name "file5" -o -name "file9" \) -exec rm -rvf {} \;

xargs 和-exec 兩個用於刪除時候:

xargs 打包一起刪除,速度快
-exec 找到的文件一個一個刪除,速度較慢

find常見參數:

-name   filename             #查找名爲filename的文件
-perm                        #按執行權限來查找
-user    username            #按文件屬主來查找
-group groupname             #按組來查找
-mtime   -n +n               #按文件更改時間來查找文件,-n指n天以內,+n指n天以前
-atime    -n +n              #按文件訪問時間來查GIN: 0px">
-ctime    -n +n              #按文件創建時間來查找文件,-n指n天以內,+n指n天以前
-nogroup                     #查無有效屬組的文件,即文件的屬組在/etc/groups中不存在
-nouser                      #查無有效屬主的文件,即文件的屬主在/etc/passwd中不存
-newer   f1 !f2              #查更改時間比f1新但比f2舊的文件
-type    b/d/c/p/l/f         #查是塊設備、目錄、字符設備、管道、符號鏈接、普通文件
-size      n[c]              #查長度爲n塊[或n字節]的文件
-depth                       #使查找在進入子目錄前先行查找完本目錄
-empty                       #查找空文件或目錄 

一些常見用法:

1.找出/tmp目錄下,屬主不是root,且文件名不以f開頭的文件

[root@ljc ~]# find /tmp/ -type f ! -user root ! -name "f*"
[root@ljc ~]# find /tmp/ -type f ! \( -user root -o  -name "f*" \)

2.查找/etc/目錄下,所有.conf後綴的文件

[root@ljc ~]# find /etc/ -type f  -name "*.conf"

3.查找/var目錄下屬主爲root,且屬組爲mail的所有文件

[root@ljc ~]# find /var/ -type f -user root  -group mail |xargs ls -l
-rw-------. 1 root mail 147329 Apr 17 19:21 /var/spool/mail/root

4.查找/var目錄下7天以前,同時屬主不爲root,也不是postfix的文件

[root@ljc ~]# find /var/ -type f -mtime +7 !  -user root  ! -name "postfix*"

5.查找/etc目錄下大於1M且類型爲普通文件的所有文件

[root@ljc ~]# find /etc/ -type f -size +1M |xargs ls -lh

6.查找/etc目錄下所有用戶都沒有寫權限的文件

[root@ljc ~]# find /etc/ -type f ! -perm /222|xargs ls -l

7.查找/目錄下最後創建時間是3天前,後綴是*.log的文件

[root@ljc ~]# find / -type f -mtime 3 -name "*.log"
/root/test.log

8.查找/目錄下文件名包含txt的文件

[root@ljc ~]# find / -type f -name "*txt*"

9.查找/目錄下屬主是ljc並且屬組是ljc的文件

[root@ljc ~]# find / -type f -user ljc -group ljc|xargs ls -l

10.查找/目錄下屬主是ljc但是屬組不是ljc的文件

[root@ljc ~]# find / -type f -user ljc ! -group ljc | xargs ls -l

11.查找/目錄下屬主是ljc或者屬主是ijiiyee的文件

[root@ljc ~]# find / -type f -user ljc -o -user ijiiyee |xargs ls -l

12.查找/tmp目錄下屬主既不是ljc,也不是ijiiyee的文件

[root@ljc ~]# find /tmp -type f ! -user ljc ! -user ijiiyee

13.查找/var/log目錄下7天以前的文件

[root@ljc ~]# find /var/log/ -type f -mtime +7

13.查找/tmp目錄下15天以前的文件刪除

[root@ljc ~]# find /tmp/ -type f -mtime +15 -exec "rm -rf {} \;"
[root@ljc ~]# find /tmp/ -type f -mtime +15 -delete

15.查找/home目錄下,類型是目錄的,並且屬主是ljc的目錄

[root@ljc ~]# find /home/ -type d -user ljc

16.查找/var/log下大於100kb且以log結尾的所有文件

[root@ljc ~]# find /var/log/ -type f -size "+100k" -name "*log" | xargs ls -lh

17.查找tmp目錄下所屬組group1,所屬主user1的目錄

[root@ljc ~]# groupadd group1
[root@ljc ~]# useradd user1
[root@ljc ~]# mkdir /tmp/123
[root@ljc ~]# chown user1.group1 /tmp/123
[root@ljc ~]# find /tmp/ -type d  -user user1 -group group1
/tmp/123

18.同時查找根目錄下名爲1.txt,2.txt的文件和名字帶a的目錄

[root@ljc ~]# find / -type f -name "[1-2].txt" -o -type d -name "*lamp*"

19.查找/tmp目錄下所有文件並刪除

[root@ljc ~]# find /tmp/ -type f -delete  #少
[root@ljc ~]# find /tmp/ -type f |xargs rm -f
[root@ljc ~]# find /tmp/ -type f -exec rm -f {}  \;

20.查找/etc目錄下至少有一類用戶沒有寫權限的文件

[root@ljc ~]# find /etc/ ! -perm -222 |xargs ls -l

21.查找/data/bak目錄下15天以前的文件刪除

[root@ljc ~]# find /data/bak -type f -mtime +15 -delete
[root@ljc ~]# find /data/bak -type f -mtime +15 -exec rm -f {} \;
[root@ljc ~]# find /data/bak -type f -mtime +15 |xargs rm -f

22.打包/etc目錄下所有普通文件到root用戶家目

[root@ljc ~]# find /etc/ -type f |xargs tar czf /root/ljc_v3.tar.gz 
[root@ljc ~]# tar czf /root/ljc_v4.tar.gz $(find /etc/ -type f) 

23.把/var/log/目錄中所有.log的文件進行打包成一個壓縮包,名稱定義爲log.tar.gz的壓縮包

[[email protected] /]# tar czf log.tar.gz $(find /var/log/ -type f -name "*.log")
[[email protected] /]# find /var/log/ -type f -name "*.log"|xargs  tar czf log.tar.gz

轉載原文鏈接 只是個小運維 https://www.ijiiyee.com/?p=1094

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章