find命令常见用法

 

 

 

1. find

        linux中,find命令一般用来按特定条件查找文件,生产环境中也常用其来过滤文件

名称
       find - 搜索目录层次结构中的文件


格式
       find 【目录】 {【选项】 【参数】}...


常见选项
    -type        指定要查找的文件类型(常用的有 1.f普通文件 2.d目录文件)
    -name        指定要查找的文件名(文件名称可用通配符模糊匹配)
    !           取反,在选项名称前加!可取反选项匹配的内容
    -exec        对查找到的内容执行相关操作,命令后要加 "\;" 为特定格式(如:find . -type f ! -name "love" -exec rm {} \;)
    -and         与匹配,find多条件查找时默认使用与匹配
    -or          或匹配
    -maxdepth    查找深度,1个目录则为1层深度

    扩展:
        |        通过管道把find查找的内容传下去以完成一些不为人知的事情
        xargs    将管道接收过来的内容进行整合成想要的形式以和其他命令配合使用

 

2. 简单用法

    1.查看当前目录的所有文件
    find .
    
    2.查看/test/目录下的常规文件
    find /test/ -type f
    
    3.查看/test/目录下名为love的目录文件
    find /test/ -type d -name love
    
    4.查看/test/目录下除了love文件的其他常规文件
    find /test/ -type f ! -name love
    
    5.查看/test/目录下以.log结尾的文件
    find /test/ -name *.log
    
    6.查看/test/目录下以.log结尾的文件和love文件
    find /test -name *.log -or -name love
    
    7.查看/test/目录下2层目录内的文件
    find /test -maxdepth 2
    
    8.查找到/test/目录下以.log结尾的文件并删除
    find /test -type f -name *.log$ -exec rm {} \;    
    或:
    find /test -type f -name *.log | xargs rm

 

3. find命令实用实例

将15天以前的日志文件删除掉
find . -type f -name "*.log$" -mtime +15 | xargs rm -f

 

posted @ 2019-02-14 11:26  糕事情  阅读(2119)  评论(0编辑  收藏  举报