• 14-find 查找文件


    find - search for files in a directory hierarchy 查找文件

    【语法】: find 【选项】 【参数】

    【功能介绍】

    find命令用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。

    【选项说明】

     1 -maxdepth levels  设置最大目录层级;
     2 Descend  at  most levels (a non-negative integer) levels of directories below the com-
     3 mand line arguments.  -maxdepth 0
     4 means only apply the tests and actions to the command line arguments.
     5 
     6 -mindepth levels 设置最小目录层级;
     7 Do not apply any tests or actions at levels less than levels (a non-negative integer).
     8 -mindepth 1 means process all files except the command line arguments.
     9 
    10 -name pattern 指定字符串作为寻找文件或目录的范本样式;
    11 
    12 -size  指定文件大小
    13 
    14 -type c  指定文件类型
    15 
    16 b      block (buffered) special           字符设备
    17 c      character (unbuffered) special     字符串
    18 d      directory                          目录
    19 p      named pipe (FIFO)                      
    20 f      regular file                       普通文件 
    21 l      symbolic link; this is never true if the -L option or the -follow option is  in  符号连接 
    22        effect, unless the symbolic link is broken.  If you want to search for symbolic
    23        links when -L is in effect, use -xtype.
    24              
    25 -exec <执行指令>:假设find指令的回传值为True,就执行该指令;
    26 --mtime 查找指定天数
    27 !取反  

    【参数说明】

    查找文件的起始目录

    【经验技巧】

    1. find指令支持逻辑运算符与(and)、或(or)和非(not)组成的复合查询条件。现象“-a”为默认选项。逻辑与表示当所有给定的条件都满足时,符合查找条件;逻辑非表示查找与所给条件相反的文件。
    2. 通过find指令的“-exec” 选项可以通过外部Linux指令对找到的文件进行操作。如果找到的文件较多,有可能出现“参数太长”,或者“参数溢出”的错误。可以使用xargs指令每次制度取一部分查找到的文件,等处理完毕后再读取一部分查找的文件,一次类推,直到所有的文件都被处理完毕。
    3.  为了缩短find指令的执行时间,要尽量的缩小查找的其实目录。因为find指令使用递归方式遍历目录,所以其实目录范围较大,会导致find指令的运行过程过长。
    4. 不带任何选项和参数的find指令可以打印当前目录下的所有内容,包括所有子目录下的文件列表。

    【实例】

    实例1:删除当前目录下所有.txt文件

     1 [root@cobbler6 ~]# find . -type f -name "*.txt"
     2 ./oldboy.txt
     3 ./luoahong/1129.txt
     4 ./test/e.txt
     5 ./test/b.txt
     6 ./test/a.txt
     7 ./test/c.txt
     8 ./test/d.txt
     9 [root@cobbler6 ~]# find . -type f -name "*.txt"|xargs rm -f
    10 [root@cobbler6 ~]# ll
    11 total 145900
    12 drwxr-xr-x  2 root root      4096 Nov 22 23:37 -
    13 -rw-------. 1 root root      1190 Oct  7 01:14 anaconda-ks.cfg
    14 -rw-r--r--. 1 root root     24908 Oct  7 01:13 install.log
    15 -rw-r--r--. 1 root root      6961 Oct  7 01:11 install.log.syslog
    16 -rw-r--r--  1 root root 149323776 Dec  4 19:23 luo1.tar.gz
    17 drwxr-xr-x  2 root root      4096 Dec 13 01:28 luoahong
    18 -rw-r--r--  1 root root         0 Nov 23 03:43 luo.conf
    19 -rw-r--r--  1 root root       120 Nov 23 03:48 nginx.conf
    20 -rw-r--r--  1 root root       556 Nov 29 16:23 oldboy-2016-11-26.tar.gz
    21 drwxr-xr-x  2 root root      4096 Nov 25 02:24 oldboydir
    22 lrwxrwxrwx  1 root root         9 Nov 25 02:23 oldboydir_hard_link -> oldboydir
    23 lrwxrwxrwx  1 root root         9 Nov 25 02:25 oldboydir_soft_link -> oldboydir
    24 -rw-r--r--  1 root root         5 Nov 27 16:53 oldboy.log
    25 drwxr-xr-x  2 root root      4096 Nov 22 23:37 p
    26 drwxr-xr-x  2 root root      4096 Dec 13 01:28 test

    实例2:借助-exec选项与其他命令结合使用

    1 找出当前目录下所有root的文件,并把所有权更改为用户tom 
    2 find .-type f -user root -exec chown tom {} ; 
    3 上例中,{} 用于与-exec选项结合使用来匹配所有文件,然后会被替换为相应的文件名。 找出自己家目录下所有的.txt文件并删除 
    4 find $HOME/. -name "*.txt" -ok rm {} ; 
    5 上例中,-ok和-exec行为一样,不过它会给出提示,是否执行相应的操作。 查找当前目录下所有.txt文件并把他们拼接起来写入到all.txt文件中
    6  find . -type f -name "*.txt" -exec cat {} ;> all.txt 
    7 将30天前的.log文件移动到old目录中
    8  find . -type f -mtime +30 -name "*.log" -exec cp {} old ;

    实例3:删除7前以前的日志文件

    [root@zabbix-agent log]# find  ./ -type f -name "*.log" -mtime +7|xargs rm -f  

    实例4:搜索大于10KB的文件 

    [root@zabbix-agent ~]# find . -type f -size +10k

    实例5:搜索出深度距离当前目录至少2个子目录的所有文件

    [root@zabbix-agent ~]# find . -mindepth 2 -type f

    实例6:向下最大深度限制为

     [root@zabbix-agent ~]# find . -maxdepth 3 -type f 
  • 相关阅读:
    docker 安装 kafka
    【LeetCode】406根据身高重建队列 A
    SketchUp自动化——在Ruby中创建3D模型>>机器翻译笔记
    阅读真经5解析小程序
    MySQL中的where和having的区别
    日志中常见的异常(Exeption)
    软件测试&接口测试基本流程
    Charles使用教程(macOS)—附加安装包以及注册码
    导读
    帝国CMS灵动标签循环调用多个栏目下文章
  • 原文地址:https://www.cnblogs.com/luoahong/p/6165352.html
Copyright © 2020-2023  润新知