美文网首页
find 使用

find 使用

作者: zhuchunyan_aiji | 来源:发表于2020-09-07 20:21 被阅读0次

    find 基础

    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
    

    linux查找find命令及删除7天前的文件

    find ./ -mtime +5 |xargs rm -rf
    
    语句写法:
    find 对应目录 -mtime +天数 -name "文件名" -exec rm -rf {} \;
    例1:
      find /usr/local/backups -mtime +10 -name "*.*" -exec rm -rf {} \;
      将/usr/local/backups目录下所有10天前带"."的文件删除
      find:Linux的查找命令,用户查找指定条件的文件
      /usr/local/backups:想要进行清理的任意目录
      -mtime:标准语句写法
      +10:查找10天前的文件,这里用数字代表天数,+30表示查找30天前的文件
      "*.*":希望查找的数据类型,"*.jpg"表示查找扩展名为jpg的所有文件,"*"表示查找所有文件,这个可以灵活运用,举一反三
      -exec:固定写法
      rm -rf:强制删除文件,包括目录
      {} \; :固定写法,一对大括号+空格+\+;
    

    find排除目录

    命令语法:
    find <src-path> -path '<ignor-path>' -prune -o -print
    find <src-path> -path '<ignor-path1>' -prune -o -path '<ignor-path2>' -prune -o -print
    
    类似的命令为:(还是使用了匹配,否则只忽略指定的path,不忽略指定path下的文件)
    find <src-path> ! -path '<ignor-path1>*' ! -path '<ignor-path2>*' -print
    
    
    1. 当前目录下的所有文件和文件夹
    find .  -maxdepth 1
    .
    ./common
    ./resource
    ./main
    ./spring
    ./circle
    ./init
    ./property
    ./app.properties
    ./application-config.xml
    ./method
    ./zero
    
    2. 把所有文件或文件夹移动到该目录的main/java下
    find .  -maxdepth 1 -path './main' -prune -o -print | xargs -i mv {} main/java/
    mv: cannot move '.' to 'main/java/.': Device or resource busy
    
    //.目录,即当前目录不会移到main/java下
    改成
    find .  -maxdepth 1 ! -path './main*'  ! -path '.'  | xargs -i mv {} main/java/
    排除 . 的目录
    
    1. 文件目录 展示
    ./a
    ./a/3.txt
    ./a/2.txt
    ./a/1.txt
    ./a/aa
    ./a/aa/3.txt
    ./a/aa/2.txt
    ./a/aa/1.txt
    
    ./b
    ./b/3.txt
    ./b/2.txt
    ./b/1.txt
    
    ./c
    ./c/3.txt
    ./c/2.txt
    ./c/1.txt
    ./c/cc
    ./c/cc/3.txt
    ./c/cc/2.txt
    ./c/cc/1.txt
    
    ./d
    ./d/3.txt
    ./d/2.txt
    ./d/1.txt
    
    3.1. 排除某个文件夹(注意,-type d中包含当前目录.)
    ------------------->$ find .  -path "./a" -prune -o -type d -print
    .
    ./c
    ./c/cc
    ./b
    ./d
    
    ------------------->$find .  -path "./a/aa" -prune -o -type d -print
    .
    ./c
    ./c/cc
    ./b
    ./d
    ./a
    
    ------------------->$ find .  -path "./a" -prune -o -type f -print
    ./c/3.txt
    ./c/2.txt
    ./c/cc/3.txt
    ./c/cc/2.txt
    ./c/cc/1.txt
    ./c/1.txt
    ./b/3.txt
    ./b/2.txt
    ./b/1.txt
    ./d/3.txt
    ./d/2.txt
    ./d/1.txt
    
    ------------------->$ find .  -path "./a/aa" -prune -o -type f -print
    ./c/3.txt
    ./c/2.txt
    ./c/cc/3.txt
    ./c/cc/2.txt
    ./c/cc/1.txt
    ./c/1.txt
    ./b/3.txt
    ./b/2.txt
    ./b/1.txt
    ./d/3.txt
    ./d/2.txt
    ./d/1.txt
    ./a/3.txt
    ./a/2.txt
    ./a/1.txt
    
    -------------------> find .  -path "./a" -prune -o -path './b' -prune -o -type d -prin
    .
    ./c
    ./c/cc
    ./d
    
    ------------------->$ find .  -path "./a" -prune -o -path './b' -prune -o -type f -print
    ./c/3.txt
    ./c/2.txt
    ./c/cc/3.txt
    ./c/cc/2.txt
    ./c/cc/1.txt
    ./c/1.txt
    ./d/3.txt
    ./d/2.txt
    ./d/1.txt
    
    ------------------->$ find .  -path "./a" -prune -o -path './b' -prune -o -print
    .
    ./c
    ./c/3.txt
    ./c/2.txt
    ./c/cc
    ./c/cc/3.txt
    ./c/cc/2.txt
    ./c/cc/1.txt
    ./c/1.txt
    ./d
    ./d/3.txt
    ./d/2.txt
    ./d/1.txt
    

    3.2 使用!方法
    首先在./c文件夹下新建了bb文件夹,总目录结构为

    .
    ./c
    ./c/3.txt
    ./c/bb
    ./c/2.txt
    ./c/cc
    ./c/cc/3.txt
    ./c/cc/2.txt
    ./c/cc/1.txt
    ./c/1.txt
    ./b
    ./b/3.txt
    ./b/2.txt
    ./b/1.txt
    ./d
    ./d/3.txt
    ./d/2.txt
    ./d/1.txt
    ./a
    ./a/3.txt
    ./a/2.txt
    ./a/1.txt
    ./a/aa
    ./a/aa/3.txt
    ./a/aa/2.txt
    ./a/aa/1.txt
    
    
    ------------------->$ find . ! -path './a*' ! -path './b*' -print
    .
    ./c
    ./c/3.txt
    ./c/bb
    ./c/2.txt
    ./c/cc
    ./c/cc/3.txt
    ./c/cc/2.txt
    ./c/cc/1.txt
    ./c/1.txt
    ./d
    ./d/3.txt
    ./d/2.txt
    ./d/1.txt
    
    ------------------->$ find . ! -path './a*' ! -path '*b*' -print
    .
    ./c
    ./c/3.txt
    ./c/2.txt
    ./c/cc
    ./c/cc/3.txt
    ./c/cc/2.txt
    ./c/cc/1.txt
    ./c/1.txt
    ./d
    ./d/3.txt
    ./d/2.txt
    ./d/1.txt
    
    ------------------->$ find . ! -path './a' ! -path './b' -print
    .
    ./c
    ./c/3.txt
    ./c/bb
    ./c/2.txt
    ./c/cc
    ./c/cc/3.txt
    ./c/cc/2.txt
    ./c/cc/1.txt
    ./c/1.txt
    ./b/3.txt
    ./b/2.txt
    ./b/1.txt
    ./d
    ./d/3.txt
    ./d/2.txt
    ./d/1.txt
    ./a/3.txt
    ./a/2.txt
    ./a/1.txt
    ./a/aa
    ./a/aa/3.txt
    ./a/aa/2.txt
    ./a/aa/1.txt
    
    

    也可以排除当前目录:

    ------------------->$ find . -type d -print
    .
    ./c
    ./c/bb
    ./c/cc
    ./b
    ./d
    ./a
    ./a/aa
    
    ------------------->$ find . ! -path '.' -type d -print
    ./c
    ./c/bb
    ./c/cc
    ./b
    ./d
    ./a
    ./a/aa
    
    ------------------->$ find /home/rain/test/find/ -type d -print
    /home/rain/test/find/
    /home/rain/test/find/c
    /home/rain/test/find/c/bb
    /home/rain/test/find/c/cc
    /home/rain/test/find/b
    /home/rain/test/find/d
    /home/rain/test/find/a
    /home/rain/test/find/a/aa
    
    ------------------->$ find /home/rain/test/find/ ! -path '/home/rain/test/find/' -type d -print
    /home/rain/test/find/c
    /home/rain/test/find/c/bb
    /home/rain/test/find/c/cc
    /home/rain/test/find/b
    /home/rain/test/find/d
    /home/rain/test/find/a
    /home/rain/test/find/a/aa
    
    ------------------->$ find /home/rain/test/find/ ! -path '*/' -type d -print
    /home/rain/test/find/c
    /home/rain/test/find/c/bb
    /home/rain/test/find/c/cc
    /home/rain/test/find/b
    /home/rain/test/find/d
    /home/rain/test/find/a
    /home/rain/test/find/a/aa
    

    相关文章

      网友评论

          本文标题:find 使用

          本文链接:https://www.haomeiwen.com/subject/reaxektx.html