zoukankan      html  css  js  c++  java
  • find命令

    命令描述:

      按层次查找目录中的文件

    语法:

      find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

      一般形式:find pathname -options [-print -exec -ok ...]

    参数:

      pathname: find命令所查找的目录路径。用.来表示当前目录,用/来表示系统根目录,..表示父目录。查到的结果会以pathname指定的路径为起始目录

      -exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' { } ;,注意{ }和;之间的空格

    选项

      -newer file1 ! file2     查找更改时间比文件file1新但比文件file2旧的文件。

      -depth:在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。
      -fstype:查找位于某一类型文件系统中的文件,这些文件系统类型通常可以在配置文件/etc/fstab中找到,该配置文件中包含了本系统中有关文件系统的信息。
      -mount:在查找文件时不跨越文件系统mount点。
      -follow:如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。
      -cpio:对匹配的文件使用cpio命令,将这些文件备份到磁带设备中。

    应用举例

    1、按文件名查找符合条件的文件

      -name   按照文件名查找文件。

    [root@localhost ~]# find /etc -name "*ass*"
    /etc/libnl/classid
    /etc/pam.d/passwd
    

    2、按文件属性查找文件

      -user   按照文件属主来查找文件。

      -group   按照文件所属的组来查找文件。

      -nogroup    查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。

      -nouser    查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在。

      -nogroup    查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。

      -nouser    查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在。

      逻辑运算符:

        -a  -o  -not 与、或、非

    准备环境
    
    [root@localhost ~]# mkdir -p /app/{a..c}
    [root@localhost ~]# touch  /app/{a..c}/{a..c}
    [root@localhost ~]# chgrp test1 /app/{a..b}/b
    [root@localhost ~]# chown test.test /app/{a..c}/a
    [root@localhost ~]# chown test.test1 /app/{a..c}/c
    
    [root@localhost ~]# ll /app/*
    /app/a:
    total 0
    -rw-r--r--. 1 test test  0 May 23 04:52 a
    -rw-r--r--. 1 root test1 0 May 23 04:52 b
    -rw-r--r--. 1 test test1 0 May 23 04:52 c
    
    /app/b:
    total 0
    -rw-r--r--. 1 test test  0 May 23 04:52 a
    -rw-r--r--. 1 root test1 0 May 23 04:52 b
    -rw-r--r--. 1 test test1 0 May 23 04:52 c
    
    /app/c:
    total 0
    -rw-r--r--. 1 test test  0 May 23 04:52 a
    -rw-r--r--. 1 root root 0 May 23 04:52 b
    -rw-r--r--. 1 test test1 0 May 23 04:52 c
    
    按属主或者ID号查找
    [root@localhost ~]# find /app/ -user test  ## 或者find /app/ -user 1000
    /app/a/a
    /app/a/c
    /app/b/a
    /app/b/c
    /app/c/a
    /app/c/c
    
    按属组或id号查找
    [root@localhost ~]# find /app/ -group root     # 或者find /app/ -group 0
    /app/
    /app/a
    /app/b
    /app/c
    /app/c/b
    
    按逻辑运算符查找
    [root@localhost ~]# find /app/ -group test1 -a -user test
    /app/a/c
    /app/b/c
    /app/c/c
    [root@localhost ~]# find /app/ -group test1 -o -user test
    /app/a/a
    /app/a/b
    /app/a/c
    /app/b/a
    /app/b/b
    /app/b/c
    /app/c/a
    /app/c/c
    [root@localhost ~]# find /app/ -group test1 -not -user test
    /app/a/b
    /app/b/b   

    3、按目录的深度  

      -mindepth 查找目录的最小深度。

         -maxdepth 查找目录的最大深度

    查找最大层次
    [root@localhost ~]# find / -maxdepth 2 -name "*ass*"
    /sys/class
    /etc/passwd-
    /etc/passwd
    
    查找最小层次
    [root@localhost ~]# find / -mindepth 2 -name "*asswd"
    /sys/fs/selinux/class/passwd
    /sys/fs/selinux/class/passwd/perms/passwd
    /etc/pam.d/passwd
    /etc/passwd
    /etc/security/opasswd
    /usr/bin/gpasswd
    /usr/bin/passwd
    
    介于某一层次区间的
    [root@localhost ~]# find / -mindepth 1 -maxdepth 3 -name "*asswd"
    /etc/pam.d/passwd
    /etc/passwd
    /etc/security/opasswd
    /usr/bin/gpasswd
    /usr/bin/passwd
    /usr/sbin/chpasswd
    /usr/sbin/lpasswd  

    4、按文件大小查找

      -size n:[c] 查找文件长度为n块的文件,带有c时表示文件长度以字节计。

    准备环境
    
    [root@localhost ~]# dd if=/dev/zero of=test1 bs=1024 count=10
    10+0 records in
    10+0 records out
    10240 bytes (10 kB) copied, 0.0004966 s, 20.6 MB/s
    
    [root@localhost ~]# dd if=/dev/zero of=test2 bs=1 count=20480
    20480+0 records in
    20480+0 records out
    20480 bytes (20 kB) copied, 0.0510684 s, 401 kB/s
    
    [root@localhost ~]# dd if=/dev/zero of=test3 bs=10 count=20480
    20480+0 records in
    20480+0 records out
    20480 bytes (20 kB) copied, 0.0512977 s, 399 kB/s
    
    查找小于20k的
    [root@localhost ~]# find . -size -20k -name 'test*'
    ./test1
    查找大于20k的
    [root@localhost ~]# find . -size +20k -name 'test*' 
    ./test3

    查找20k的

    [root@localhost ~]# find . -size 20k
    ./test2

    5、按文件类型查找

      -type    查找某一类型的文件,诸如:

        b - 块设备文件。
        d - 目录。
        c - 字符设备文件。
        p - 管道文件。
        l - 符号链接文件。
        f - 普通文件。

    [root@localhost ~]# find /dev -type f
    [root@localhost ~]# find /dev -type d
    /dev
    /dev/net
    /dev/vfio
    /dev/snd
    /dev/snd/by-path
    /dev/hugepages
    /dev/mqueue
    /dev/disk
    

    6、按文件权限查找

      -perm   按照文件权限来查找文件。

    [root@localhost ~]# find . -type f -perm 600 
    ./anaconda-ks.cfg
    ./.bash_history
    ./.mysql_history
    ./.viminfo
    

    7、忽略某个目录

      -prune  使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用-depth选项,那么-prune将被find命令忽略。  

    [root@localhost ~]# find /root -type f  -path "/root/a/1" -prune 
    /root/a/1
    
    [root@localhost ~]# find /root -type f  -path "/root/a/1" -prune -o -name "1" -print
    /root/b/1
    /root/c/1
    /root/1
    
    -path "/usr/sam" -prune -o -print 是 -path "/usr/sam" -a -prune -o -print 的简写表达式按顺序求值,
    -a 和 -o 都是短路求值,与 shell 的 && 和 || 类似
    如果 -path "/usr/sam" 为真,则求值 -prune , -prune 返回真,与逻辑表达式为真;否则不求值 -prune,与逻辑表达式为假。
    如果 -path "/usr/sam" -a -prune 为假,则求值 -print ,-print返回真,或逻辑表达式为真;否则不求值 -print,或逻辑表达式为真。 这个表达式组合特例可以用伪码写为 if -path "/usr/sam" then -prune else -print

    8、按修改时间查找

      -mtime -n +n    按照文件的更改时间来查找文件, - n表示文件更改时间距现在n天以内,+ n表示文件更改时间距现在n天以前。

      find命令还有-atime和-ctime 选项  

     

    查找修改时间在2天之内的目录
    [root@localhost ~]# find /data  -type d -mtime -2
    /data
    /data/mysql
    /data/mysql/data_3306
    /data/mysql/data_3306/mysql
    
    查找修改时间在3天之前的
    
    [root@localhost ~]# date -s '+4 day'
    
    [root@localhost ~]# find /etc  -type f -mtime +3
    /etc/resolv.conf
    /etc/group-
    /etc/gshadow-
    /etc/passwd-
    /etc/group
    /etc/shadow-
    /etc/gshadow
    /etc/passwd
    /etc/shadow
    /etc/security/pwquality.conf
    

    9、使用-exec执行shell命令

    [root@localhost ~]# find /root -type d -name "2"  -exec cp -r {} /tmp ;
     
    

    10、使用管道符 “|”配合xargs处理

      -n: 指定一次处理的参数个数

      -d: 自定义参数界定符

      -p: 询问是否运行 later command 参数

      -t : 表示先打印命令,然后再执行

      -i : 逐项处理

    [root@localhost ~]# find /etc -type d -name "*as*" |xargs -i cp -r {} /tmp   

     xargs与exec的区别

      1)exec参数是一个一个传递的,传递一个参数执行一次命令;xargs一次将参数传给命令,可以使用-n控制参数个数 

    xargs将参数一次性传递给tar命令
    [root@localhost ~]# find /etc/ -type d -name "ba*" |xargs tar -zcf /root/a.tar.gz 
    tar: Removing leading `/' from member names
    
    exec一次传递一个参数
    [root@localhost ~]# find /etc/ -type d -name "ba*" -exec tar -zcf /root/b.tar.gz {} ;
    tar: Removing leading `/' from member names
    tar: Removing leading `/' from member names
    tar: Removing leading `/' from member names
    tar: Removing leading `/' from member names
    

        2)如果指定命令不是只有find最后传递的参数保留(全部保留),最好不要用exec

    [root@localhost ~]# tar tf a.tar.gz 
    etc/systemd/system/basic.target.wants/
    etc/systemd/system/basic.target.wants/rhel-dmesg.service
    etc/systemd/system/basic.target.wants/microcode.service
    etc/bash_completion.d/
    etc/bash_completion.d/iprutils
    etc/bash_completion.d/redefine_filedir
    etc/selinux/targeted/active/modules/100/bacula/
    etc/selinux/targeted/active/modules/100/bacula/cil
    etc/selinux/targeted/active/modules/100/bacula/hll
    etc/selinux/targeted/active/modules/100/bacula/lang_ext
    etc/selinux/targeted/active/modules/100/base/
    etc/selinux/targeted/active/modules/100/base/cil
    etc/selinux/targeted/active/modules/100/base/hll
    etc/selinux/targeted/active/modules/100/base/lang_ext
    
    [root@localhost ~]# tar tf b.tar.gz 
    etc/selinux/targeted/active/modules/100/base/
    etc/selinux/targeted/active/modules/100/base/cil
    etc/selinux/targeted/active/modules/100/base/hll
    etc/selinux/targeted/active/modules/100/base/lang_ext
    
    应该是exec执行的时候,最后匹配的路径才当做最终参数了,前面的覆盖了
    
    [root@localhost ~]# mkdir a
    [root@localhost ~]# mkdir b
    [root@localhost ~]# touch a/a.b
    [root@localhost ~]# touch a/a.c
    
    [root@localhost ~]# find ./ -name "a.*"|xargs tar -zcf /root/c.tar.gz
    
    [root@localhost ~]# find ./ -name "a.*" -exec  tar -zcf /root/b.tar.gz {} ;
    
    [root@localhost ~]# tar tf c.tar.gz 
    ./a/a.b
    ./a/a.c
    [root@localhost ~]# tar tf b.tar.gz 
    ./a/a.c
    

      3)exec文件名有空格等特殊字符也能处理;xargs不能处理特殊文件名,如果想处理特殊文件名需要特殊处理

    find命令对正则表达式的支持

    查找某一路径下以数字命名的文件

    [root@iZzm446eh1ux98Z /]# find / -maxdepth 1 -regex '/[0-9]+'

     

      

  • 相关阅读:
    【转】Rhythm Of The Rain 雨的旋律
    【转】Mysql中文乱码的几个解决方法
    【转】XAMPP不能正常启动Apache的解决方案
    【转】Cotton Fields 家乡的棉花田
    ArrayList排序Sort()方法
    数据库锁表的分析与解决
    数据从Excel到DataSet图解
    C# 多态例子【原创】
    Collection List Set和Map用法与区别 (转载)
    G711 G723 G729线路占多少带宽问题
  • 原文地址:https://www.cnblogs.com/zh-dream/p/12240778.html
Copyright © 2011-2022 走看看