linux基础-----------find详解

find 是实时查找工具,通过遍历指定路径完成文件查找

.1 指定搜索目录层级

-maxdepth N #最大搜索目录深度,指定目录下的文件为第1级

-mindepth N #最小搜索目录深度

仅搜索第二层目录 [root@ubuntu2204 ~]# find /etc/ -maxdepth 2 -mindepth 2

#最小搜索深度 [root@ubuntu2204 ~]# find /etc/ -mindepth 2

最大搜索深度 [root@ubuntu2204 ~]# find /etc/ -maxdepth 2

2.先处显示文件再显示目录

find -depth

3 根据文件名和inode查找

-name name #支持使用glob,如:*, ?, [], [^],通配符要加双引号引起来

-iname name #不区分字母大小写

-inum number #按inode号查找

-samefile name #相同inode号的文件

-links n   #链接数为n的文件

-regex "PATTERN"   #以PATTERN匹配整个文件路径,而非文件名称

例:

#指定文件名,忽略大小写 [root@ubuntu2204 ~]# find -iname test-a.log

./test-a.log ./test-A.log

#通配符 [root@ubuntu2204 ~]# find -name "test-a*"

./test-a.log ./test-a.txt

#正则表达式,路径匹配 [root@ubuntu2204 ~]# find -regex ".*dir3$" ./dir1/dir2/dir3

.4 根据属主属组查找

-user USERNAME #查找属主为指定用户(UID)的文件

-group GRPNAME #查找属组为指定组(GID)的文件

-uid UserID #查找属主为指定的UID号的文件

-gid GroupID #查找属组为指定的GID号的文件

-nouser #查找没有属主的文件

-nogroup #查找没有属组的文件

例:

#指定属主属组 [root@ubuntu2204 ~]# find -user zz -group root

./dir1/fa.txt ./dir1/fb.txt

#属主属组不存在 [root@ubuntu2204 ~]# find -nouser -nogroup

./dir1/f1

5 根据文件类型查找

-type TYPE #指定文件类型 #type 值

f #普通文件

d #目录文件

l #符号链接文件

s #套接字文件

b #块设备文件

c #字符设备文件

p #管道文件

6 空文件或目录

空文件或空目录 [root@ubuntu2204 ~]# find dir1/dir2/dir3/ -empty

dir1/dir2/dir3/fx

dir1/dir2/dir3/fy

dir1/dir2/dir3/dir4

7 组合条件

-a #与,多条件默认就是与关系,可省略 -o #或 -not|! #非

[root@ubuntu2204 ~]# find -user zz -o -name "*log"

./dir1/fa.txt

./dir1/fb.txt

./test-a.log

./test-b.log

./test-A.log

./test-B.log

find \( -user zz -o -name "*log" \)  -ls

-prune #跳过,排除指定目录,必须配合 -path使用

#查找所有 txt文件 [root@ubuntu2204 ~]# find -name "*.txt"

./dir1/fa.txt

./dir1/fb.txt

./test-a.txt

./test-b.txt

./dir4/f-x.txt

./dir4/f-y.txt

#排除多个目录 [root@ubuntu2204 ~]# find \( -path './dir1' -o -path './dir4' \) -prune -o - name "*.txt" -print

./test-a.txt ./test-b.txt

9 根据文件大小来查找

-size [+|-]N UNIT # N为数字,UNIT为常用单位 k, M, G, c(byte) 等

 10k #表示(9k,10k],大于9k 且小于或等于10k

-10k #表示[0k,9k],大于等于0k 且小于或等于9k

+10k #表示(10k,∞),大于10

大于2k,小于或等于9k

find /var/log/ -size +2k -size -10k -name "*log" -ls

10 根据时间戳

-atime [+|-]N

-mtime [+|-]N

-ctime [+|-]N

#以分钟为单位 -amin [+|-]N

-mmin [+|-]N

-cmin [+|-]N

N #表示[N,N+1),大于或等于N,小于N+1,表示第N天(分钟)

+N #表示[N+1,∞],大于或等于N+1,表示N+1天之前(包括)

-N #表示[0,N),大于或等于0,小于N,表示N天(分钟)内

#查找五分钟内被访问过的文件 [root@ubuntu2204 ~]# find -amin -5

11 根据权限查找

-perm [/|-]MODE MODE #精确权限匹配

/MODE #任何一类(u,g,o)对象的权限中只要有一位匹配即可,表示或者(or)关系

+MODE #从CentOS 7开始己淘汰

-MODE #每一类对象都必须同时拥有指定权限,表示与(and)关系

#0 如果要找时权限位上的值为0,则表示不关注该角色权限

#精确匹配,ugo都只能是 r权限 [root@ubuntu2204 ~]# find -perm 444 -ls

#或关系,ugo只要有一个角色有r权限即可 [root@ubuntu2204 ~]# find -perm /444 -ls

#ugo三个角色至少都要有r权限 [root@ubuntu2204 ~]# find -perm -444 -ls

root@zz:~# find -perm -444 -ls
   656493      4 -rw-r--r--   1 root     root           72 Sep 23 11:17 ./.pydistutils.cfg
   655803  70696 -rw-r--r--   1 root     root     72388192 Oct  5 13:27 ./fxsound_setup.exe
   655656      4 -rw-r--r--   1 root     root          161 Apr 22 21:04 ./.profile
   655810      0 -rw-r--r--   1 root     root            0 Oct  5 21:52 .

#只关注属主权限,6拆分成4+2,所以只要属主有r或w权限即可

root@zz:~# find -perm -600 -ls

656493      4 -rw-r--r--   1 root     root           72 Sep 23 11:17 ./.pydistutils.cfg
   655803  70696 -rw-r--r--   1 root     root     72388192 Oct  5 13:27 ./fxsound_setup.exe

655253      4 drwx------   2 root     root         4096 Sep 23 11:06 ./.ssh
   655790      0 -rw-------   1 root     root            0 Oct  4 18:25 ./.ssh/authorized_keys

12 正则表达式

-regextype type #正则表达式类型,emacs|posix-awk|posix-basic|posix-egrep|posix-extended

-regex pattern #正则表达式

find -regextype posix-egrep -regex ".*5.*"

./f-5.txt

find  -regex ".*5.*"

./f-5.txt

13 处理动作

-print #默认的处理动作,显示至屏幕

-print0 #不换行输出,常用于配合xargs

-ls #类似于对查找到的文件执行"ls -ils"命令格式输出

-fls file #查找到的所有文件的长格式信息保存至指定文件中,相当于 -ls > file

-delete #删除查找到的文件,慎用!

-ok COMMAND {} \; #对查找到的每个文件执行由COMMAND指定的命令,对于每个文件执行命令之 前,都会交互式要求用户确认

-exec COMMAND {} \; #对查找到的每个文件执行由COMMAND指定的命令 {} #用于引用查找到的文件名称自身

1)#将other权限有w的文件的权限去掉w权限 (3种写法)

[root@ubuntu2204 ~]# find -perm -002 -exec chmod o-w {} \;

find -perm -002 | xargs chmod o-w

chmod o-w ·find -perm -002·(反引号)

2) find找出/test/find/ 以.txt结尾的文件放 在/tmp/find.tar.gz

tar zcf /tmp/find.tar.gz  `find /test/find/ - type f -name '*.txt'`

find /test/find/  -type f  -name '*.txt'|xargs tar zcf /tmp/etc-xargs.tar.gz

find /test/find/  -type f  -name '*.txt'  -exec tar zcf /tmp/find-exec.tar.gz {}  +

注意: 最后即为是+ 而不是 \;,因为-exec \;执行方式 1个文件1个文件的压缩

而+则为 前面执行完了,在执行后面的

3)find找出/test/find/ 以.txt结尾的文件然后复制到/tmp 下面

cp 源 .......   目标

cp `find /test/find/ -type f -name '*.txt'` /tmp/

find /oldboy/find/  -type f  -name '*.txt' |xargs cp -t /tmp/

注意:cp -t 而不是cp。 cp -t为 目标   源 文件 目录 ..... mv -t 相同思路

find /test/find/  -type f  -name '*.txt' -exec cp {} /tmp/ \;

一次创建10000个文件 echo f-{1..130752}.txt | xargs -n 10000 touch

批量创建用户并显示命令 [root@ubuntu2204 ~]# echo user{1..5} |xargs -t -n1 useradd

useradd user1

useradd user2

useradd user3

useradd user4

useradd user5

批量删除用户

echo user{1..5} |xargs -n1 userdel -r

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值