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

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