shell find指令的用法

本文详细介绍了Linux中的find命令,包括基本格式、常用参数如-name、-iname、-user、-group、-type、-size、-mtime、-mmin等,并通过实例演示了如何根据文件名、大小、修改时间等条件查找文件。同时,还提到了一些不常用的选项如-nouser、-nogroup和-perm,以及如何排除特定目录的搜索。该文对于理解和使用find命令进行文件查找非常有帮助。
摘要由CSDN通过智能技术生成

本文参考:https://www.cnblogs.com/hchan/p/12882131.html

一、命令格式 

find [路径] [选项] [操作]

二、命令参数

三、常用选项

-name  查找 /etc 目录下以 conf 结尾的文件,文件名区分大小写,例如:find /etc -name '*.conf'

-iname      查找当前目录下所有文件名为 aa 的文件,文件名不区分大小写,例如:find . -name aa

-user  查找文件所属用户为 yangyang 的所有文件,例如:find . -user yangyang

-group    查找文件所属组为 yangyang 的所有文件,例如:find . -group yangyang

-type  根据类型查找:如下

    f   文件        find . -type f

    d  目录        find . -type d

    c  字符设备文件    find . -type c

    b  块设备文件     find . -type b

    l   链接文件      find . -type l

    p  管道文件      find . -type p

-size    根据文件大小查询

    -n  小于 大小为 n 的文件

    +n  大于 大小为 n 的文件

    举例1:查找 /ect 目录下,小于 10000 字节的文件。 find /etc -size -10000c

    举例2:查找 /etc 目录下,大于 1M 的文件。find /etc -size +1M

 -mtime  

    -n  n 天以内修改的文件。

    +n  n 天以外修改的文件。

    n    正好 n天 修改的文件

    举例1: 查询 /etc 目录下,5天以内修改 且以 conf 结尾的文件。 find /etc -mtime -5 -name '*.conf'

      举例2: 查询 /etc 目录下,10天之前修改,且属于 yangyang 的文件。  find /etc -mtime +10 -user yangyang

-mmin  

    -n  n 分钟以内修改过的文件

     +n  n 分钟之前修改过的文件

    举例1: 查询 /etc 目录下 30分钟 之前修改过的文件。   find /etc -mmin +30

     举例1: 查询 /etc 目录下 30分钟 之前修改过的目录。   find /etc -mmin -30 -type d

-mindepth n  从第 n 级目录开始搜索

   举例:从 /etc 的第三级子目录开始搜索。  find /etc -mindepth 3

-maxdepth n  表示至多搜索到第 n-1 级子目录。

   举例1: 在 /etc 中搜索符合条件的文件,但最多搜索到 2级 子目录。  find /etc -maxdepth 3 -name '*.conf'

  举例2: find /etc -type f -name '*.conf' -size +10k -maxdepthc 2

四、不常用选项

-nouser     查询没有所属用户的文件

     举例:find /etc -type f -nouser

-nogroup 查询没有所属组的文件

     举例:find /etc -type f -nogroup

-perm  根据权限查询

     举例:find /etc -perm 664

-prune  通常和 -path 一起使用,用于将特定目录排除在搜索条件之外。过滤条件写在其他条件前面。

    举例1:查找当前目录下的所有普通文件,但排除 test目录。

    find . -path ./test -prune -o -type f

    举例2:  查找当前目录下所有普通文件,但排除 test目录 和 opt目录。

    find . -path ./test -prune -o -path ./opt -prune -o -type f  

    举例3:  查找当前目录下所有普通文件,但排除 test目录 和 opt目录,但属主为 yangyang

    find . -path ./test -prune -o -path ./opt -prune -o -type f -a -user yangyang

    举例4:  查找当前目录下所有普通文件,但排除 test目录 和 opt目录,但属主为 yangyang,且文件大小必须大于 500字节

    find . -path ./test -prune -o -path ./opt -prune -o -tyep f -a -user yangyang -a -size +500

-newer file1 

    举例:查找当前目录下比 a 文件要新的文件。  find . -newer a  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值