zoukankan      html  css  js  c++  java
  • shell学习(19)- find查找命令

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

    语法

    find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} ;

    参数说明 :

    find 根据下列规则判断 path 和 expression,在命令列上第一个 - ( ) , ! 之前的部份为 path,之后的是 expression。如果 path 是空字串则使用目前路径,如果 expression 是空字串则使用 -print 为预设 expression。
    
    expression 中可使用的选项有二三十个之多,在此只介绍最常用的部份。
    
    -mount, -xdev : 只检查和指定目录在同一个文件系统下的文件,避免列出其它文件系统中的文件
    
    -amin n : 在过去 n 分钟内被读取过
    
    -anewer file : 比文件 file 更晚被读取过的文件
    
    -atime n : 在过去n天内被读取过的文件
    
    -cmin n : 在过去 n 分钟内被修改过
    
    -cnewer file :比文件 file 更新的文件
    
    -ctime n : 在过去n天内被修改过的文件
    
    -empty : 空的文件-gid n or -group name : gid 是 n 或是 group 名称是 name
    
    -ipath p, -path p : 路径名称符合 p 的文件,ipath 会忽略大小写
    
    -name name, -iname name : 文件名称符合 name 的文件。iname 会忽略大小写
    
    -size n : 文件大小 是 n 单位,b 代表 512 位元组的区块,c 表示字元数,k 表示 kilo bytes,w 是二个位元组。-type c : 文件类型是 c 的文件。
    
    d: 目录
    
    c: 字型装置文件
    
    b: 区块装置文件
    
    p: 具名贮列
    
    f: 一般文件
    
    l: 符号连结
    
    s: socket
    
    -pid n : process id 是 n 的文件
    
    你可以使用 ( ) 将运算式分隔,并使用下列运算。
    
    exp1 -and exp2
    
    ! expr
    
    -not expr
    
    exp1 -or exp2
    
    exp1, exp2

    为了方便测试,先建几个文件

    for i in $(seq 5);do touch $i.py; done;
    for i in $(seq 5);do touch $i.txt; done;
    touch TEST.txt
    touch test.txt
    ls
    1.py  1.txt  2.py  2.txt  3.py  3.txt  4.py  4.txt  5.py  5.txt TEST.txt test.txt

    1.基础查找

    find . -print
    .
    ./3.txt
    ./5.txt
    ./4.py
    ./1.py
    ./2.py
    ./1.txt
    ./3.py
    ./2.txt
    ./4.txt
    ./5.py

    .指定当前目录,..指定父目录,print选项使用 换行符分割输出的每个文件或目录,而-print0选择则使用空字符''来分割

    2.根据文件名查找

    find . -name '*.txt' -print
    ./3.txt
    ./5.txt
    ./1.txt
    ./2.txt
    ./4.txt
    注意*.txt两边的单引号,shell会扩展没有单引号或是出现在双引号中的通配符。单引号能阻止扩展,使得*.txt能够原封不动的传给find命令。
    -iname选项是忽略字母大小写
    find . -iname test.txt -print
    ./TEST.TXT
    ./test.txt

    3.find逻辑查找

    -a和-and选项可以执行逻辑与(AND)操作,-o和-or选项可以执行逻辑或(OR)操作
    查找后缀为py的或txt的文件
    find . -iname '*.py' -o -iname '*.txt'
    ./3.txt
    ./5.txt
    ./4.py
    ./TEST.TXT
    ./test.txt
    ./1.py
    ./2.py
    ./1.txt
    ./3.py
    ./2.txt
    ./4.txt
    ./5.py

    查找后缀为py且以1开头的文件

    find . -iname '*.py' -and -name '1*'
    ./1.py

    正则表达式查找

    find -iregex '.*(.py|.txt)$'
    ./3.txt
    ./5.txt
    ./4.py
    ./TEST.TXT
    ./test.txt
    ./1.py
    ./2.py
    ./1.txt
    ./3.py
    ./2.txt
    ./4.txt
    ./5.py

    4.否定参数

    find也可以用!排除匹配到的模式

    find . ! -name "*.txt" -print
    .
    ./4.py
    ./TEST.TXT
    ./1.py
    ./2.py
    ./3.py
    ./5.py

    5.基于目录深度搜索

     -maxdepth和-mindepth选项可以现在find命令遍历的目录深度。可以避免find没完没了的查找
    find -L /proc -maxdepth 1 -name 'bundlemaker.def' 2>/dev/null
     -L选项告诉find命令跟随符号链接
     从/proc目录开始查找
     -maxdepth 1将搜索范围仅限制在当前目录
     -name 'bundlemaker.def'指定待查找的文件
     2>/dev/null将有关循环链接的错误信息发送到空设备中
     
    打印出深度距离当前目录至少两个子目录的所有名字以f开头的文件:
     find . -mindepth 2 -name "f*" -print

    6.根据文件类型搜索

    列出普通文件

    find . -type f -print

    列出目录

    find . -type d -print

    列出符号链接

    find . -type l -print

    find能够识别的类型与参数

    文件类型
    类型参数
    普通文件
    f
    符合链接
    l
    目录
    d
    字符设备
    c
    块设备
    b
    套接字
    s
    FIFO
    p

    7.根据时间戳进行搜索

     访问时间(-atime):用户最近一次访问文件的时间。
     修改时间(-mtime):文件内容最后一次被修改的时间。
     变化时间(-ctime):文件元数据(例如权限或所有权)最后一次改变的时间。
    -atime、-mtime和-ctime可作为find的时间选项。它们可以用整数值来指定天数。这些数字前面可以加上-或+。-表示小于,+表示大于。
     
    打印出在最近7天内被访问过的所有文件
    find . -type f -atime -7 -print

    打印出恰好在7天前被访问过的所有文件

    find . -type f -atime 7 -print

    打印出访问时间超过7天的所有文件

    find . -type f -atime +7 -print
    -atime、-mtime以及-ctime都是以“天”为单位来计时的。find命令还支持以“分钟”
    为计时单位的选项。这些选项包括:
     -amin(访问时间);
     -mmin(修改时间);
     -cmin(变化时间)。
    打印出7分钟之前访问的所有文件:
    find . -type f -amin +7 -print

    8.基于文件大小的搜索

    # 大于2KB的文件
     find . -type f -size +2k
    # 小于2KB的文件
    find . -type f -size -2k
    # 大小等于2KB的文件
     find . -type f -size 2k
    除了k之外,还可以用其他文件大小单位。
    q b:块(512字节)。
    q c:字节。
    q w:字(2字节)。
    q k:千字节(1024字节)。
    q M:兆字节(1024K字节)。
    q G:吉字节(1024M字节)。
     
     
     9.基于文件权限和所有权的匹配
     
    打印出权限为644的文件
    find . -type f -perm 644 -print

    Web服务器上的PHP文件需要具有合适的执行权限。我们可以用下面的方法找出那些没有设置好执行权限的PHP文件:

    find . -type f -name "*.php" ! -perm 644 –print

    查找前目录中文件属主具有读、写权限,并且文件所属组的用户和其他用户具有读权限的文件:

    find . -type f -perm 644 -exec ls -l {} ;
     
    10.利用find执行相应操作
    (1)删除.swp文件
    find . -type f -name test.swp -delete

     (2)删除其他文件

    find . -type f -name "*黑名单*" -print0 | xargs -0 rm -f
    find . -type f -name "*.py" -exec rm {} ;
    find . -type f -name "*.py" -print | xargs rm -f
    find . -type f -name "*.py" | xargs rm -f
    find . -type f -name "*.py" | xargs -I {} rm -f {}
    查找/var/log目录中更改时间在7日以前的普通文件,并在删除之前询问它们:
    find /var/log -type f -mtime +7 -ok rm {} ;

    (3)执行命令-exec

    find命令使用一对花括号{}代表文件名。在下面的例子中,对于每一个匹配的文件,find命令会将{}替换成相应的文件名并更改该文件的所有权。如果find命令找到了root所拥有的两个文件,那么它会将其所有者改为slynux:
    find . -type f -user root -exec chown slynux {} ;
    注意该命令结尾的;。必须对分号进行转义,否则shell会将其视为find命令的结束,而非chown命令的结束。下列命令可以将10天前的 .txt文件复制到OLD目录中:
    find . -type f -mtime +10 -name "*.txt" -exec cp {} OLD ;
    我们无法在-exec选项中直接使用多个命令。该选项只能够接受单个命令,不过我们可以耍一个小花招。把多个命令写到一个shell 脚本中( 例如command.sh),然后在-exec中使用这个脚本:
    -exec ./commands.sh {} ;

    11.find跳过特定的目录

    find devel/source_path -name '.git' -prune -o -type f -print
    -name ".git" –prune是命令中负责进行修剪的部分,它指明了.git目录应该被排除在外。
    -type f –print描述了要执行的操作。
     
    参考:https://www.runoob.com/linux/linux-comm-find.html
              《Linux_Shell脚本攻略.第3版》第二章
  • 相关阅读:
    火星A+B
    分西瓜(DFS)
    H.数7(模拟)
    镜像树(dfs)
    锐雯上单不给就送(矩阵快速幂)
    STL容器
    优先队列(和fence repair完全一样)
    x位全排列(next_permutation)
    fence repair(队列水过)
    线段相交
  • 原文地址:https://www.cnblogs.com/kumufengchun/p/11359029.html
Copyright © 2011-2022 走看看