• 35 | find方式的使用


    1 按名称查找文件

    1.1 在单目录中查找

    [root@centos7 ~]# find . -name test.txt
    [root@centos7 ~]# find / -name test.txt
    /opt/test.txt
    [root@centos7 ~]# 
    
    • 1
    • 2
    • 3
    • 4

    1.2 在几个不同的目录

    [root@centos7 ~]# find . -name test.txt
    [root@centos7 ~]# find  /data/  /home/  /usr/local/   -name test.txt
    /opt/test.txt
    [root@centos7 ~]# 
    
    • 1
    • 2
    • 3
    • 4

    2 正则表达式查找文件

    2.1 单个字符

    [root@centos7 ~]# find /opt -type f   -name "test?"
    
    • 1
    [root@centos7 ~]# find /opt -name "*.pdf"
    
    • 1

    2.2 多个字符

    [root@centos7 ~]# find /opt -type f   -name "*.pdf"
    
    • 1
    [root@centos7 ~]# find /opt -type f   -name "test*"
    
    • 1

    2.3 附加条件参数”-a “,“ -o ” ,“ –not”的使用

    使用 -a这个参数可以链接2个不同的条件且这2个条件必须要满足

    [root@centos7 ~]# find /opt -type f   -name "test*"  -a   -test1  test2
    
    • 1

    用-o这个参数链接2个不同的条件仅满足其中一个条件即可!

    [root@centos7 ~]# find /opt -type f   -name "test*"   -o   -test1  test2
    
    • 1

    用-not表示对这2个条件取反

    [root@centos7 ~]# find /opt -type f   -name "test*"  -not   -test1  test2
    
    • 1

    3 查找不同类型的文件

    通过指定 -type 选项来搜索其他类型的文件。

    [root@centos7 ~]# find . -type d -name "test*"
    [root@centos7 ~]# 
    
    • 1
    • 2
    [root@centos7 ~]# find . -type | -name "test*"
    [root@centos7 ~]# 
    
    • 1
    • 2

    4 按指定的时间戳查找文件

    Linux 系统中的 3 个不同的时间戳:

     - 访问时间(-atime/天,-amin/分钟):最后一次读取文件的时间。
     - 修改时间(-mtime/天,-mmin/分钟):文件内容最后一次被修改的时间。
     - 变化时间(-ctime/天,-cmin/分钟):上次更改文件元数据的时间(如,所有权、位置、文件类型和权限设置)
    
    • 1
    • 2
    • 3

    注意:

    -atime n  查找系统中最后n*24小时访问的文件
    -ctime n  查找系统中最后n*24小时被改变文件状态的文件
    -mtime n  查找系统中最后n*24小时被改变文件数据的文件
    
    -amin n   查找系统中最后N分钟访问的文件
    -cmin n   查找系统中最后N分钟被改变文件状态的文件
    -mmin n   查找系统中最后N分钟被改变文件数据的文件
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述
    在这里插入图片描述

    4.1 搜索 atime 超过一年的文件

    [root@centos7 ~]# find . -type f -atime +365
    ./.bash_logout
    ./.cshrc
    ./.tcshrc
    [root@centos7 ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5

    4.2 查找 mtime 正好是 5 天前的文件

    请不要包含 +,因为它的意思是“大于”

    [root@centos7 ~]# find . -type f -mtime 5
    [root@centos7 ~]# 
    
    
    • 1
    • 2
    • 3

    4.3 搜索 ctime 在 5~10 天前的文件

    find . -type f -ctime +5 -ctime -10
    
    • 1

    5 按大小查找文件

    -size选项使我们能够按指定大小查找文件

    b:512 字节块(默认)
    c:字节
    w:双字节字
    k:KB
    M:MB
    G:GB

    按时间戳查找文件,+ 表示“大于”,- 表示“小于”。例如,要查找大小为 10 MB ~ 1 GB 的文件:

    find . -type f -size +10M -size -1G
    
    • 1

    6 按权限查找文件

    find 命令的 -perm 选项可以帮助我们按指定权限查找文件:

    [root@centos7 ~] find . -type f -perm 777
    
    • 1

    7 按所有权查找文件

    使用 -user 选项指定用户名:

    [root@centos7 ~] find -type f -user test
    
    • 1

    8 在找到文件后执行命令

    [root@centos7 ~] find . -type f -atime +365 -exec rm -rf {} \;
    
    • 1

    上述命令在 -exec 选项后是 rm -rf,其用于删除文件。{} 是用于查找结果的占位符。
    注意:占位符 {} 非常重要,尤其是在您想删除文件时。因为,如果您不使用它,该命令将对所有文件执行(而不是您刚刚通过 find 命令找到的文件)。

    [root@centos7 ~] find  /etc/  /tmp  /root/  -name  "test"   -exec mv {}  {}.`date + %F` \;
    
    • 1

    9 find常用参数

    -name   按照文件名查找文件。
    -perm   按照文件权限来查找文件。
    -prune  使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用-depth选项,那么-prune将被find命令忽略。
    -user   按照文件属主来查找文件。
    -group  按照文件所属的组来查找文件。
    -mtime -n +n  按照文件的更改时间来查找文件, - n表示文件更改时间距现在n天以内,+ n表示文件更改时间距现在n天以前。find命令还有-atime和-ctime 选项,但它们都和-m time选项。
    -nogroup  查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。
    -nouser   查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在。
    -newer file1 ! file2  查找更改时间比文件file1新但比文件file2旧的文件。
    -type  查找某一类型的文件,诸如:
    b - 块设备文件。
    d - 目录。
    c - 字符设备文件。
    p - 管道文件。
    l - 符号链接文件。
    f - 普通文件。
    -size n:[c] 查找文件长度为n块的文件,带有c时表示文件长度以字节计。-depth:在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。
    -fstype 查找位于某一类型文件系统中的文件,这些文件系统类型通常可以在配置文件/etc/fstab中找到,该配置文件中包含了本系统中有关文件系统的信息。
    -mount 在查找文件时不跨越文件系统mount点。
    -follow 如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。
    -cpio 对匹配的文件使用cpio命令,将这些文件备份到磁带设备中。
    -inum 按inode号查找文件(ls -i显示inode号)
    -maxdepth 搜索深度,最大搜索到距当前目录的第几层
    -mindepth 搜索深度,最小搜索到距当前目录的第几层
    -links 按硬链接数查找
    -uid 根据uid查找 (id命令用于查看uid=多少)
    -empty 查找空文件(常用)
    -print 打印到屏幕,默认动作
    -delete 删除找到的文件
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    10 xargs的使用

    10.1 常用参数

    -a file 从文件中读入作为sdtin
    -e flag ,注意有的时候可能会是-E,flag必须是一个以空格分隔的标志,当xargs分析到含有flag这个标志的时候就停止。
    -n num 后面加次数,表示命令在执行的时候一次用的argument的个数,默认是用所有的。
    -i 或者是-I,这得看linux支持了,将xargs的每项名称,一般是一行一行赋值给 {},可以用 {} 代替。
    -r no-run-if-empty 当xargs的输入为空的时候则停止xargs,不用再去执行了。
    -s num 命令行的最大字符数,指的是 xargs 后面那个命令的最大命令行字符数。
    -L num 从标准输入一次读取 num 行送给 command 命令。
    -d delim 分隔符,默认的xargs分隔符是回车,argument的分隔符是空格,这里修改的是xargs的分隔符。
    -x exit的意思,主要是配合-s使用
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    10.2 使用举例

    10.2.1 单行输入可控多行输出

    [root@node1 soft]# echo {a..z} |xargs -n6
    a b c d e f
    g h i j k l
    m n o p q r
    s t u v w x
    y z
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    10.2.2 多行输入单行输出

    [root@node1 soft]# seq 20 |xargs -n3
    1 2 3
    4 5 6
    7 8 9
    10 11 12
    13 14 15
    16 17 18
    19 20
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    10.2.3 -d 选项可以自定义一个定界符

    [root@node1 soft]# echo "testMtestMtestMtest" | xargs -dM
    test test test test
    
    • 1
    • 2

    10.2.4 结合 -n 选项使用

    [root@node1 soft]# echo "testMtestMtestMtest" | xargs -dM  -n3
    test test test
    test
    
    • 1
    • 2
    • 3

    10.2.5 复制所有文本文件到 /data/ 目录下

    ls *.txt | xargs -n1 -I {} cp {} /data/
    
    • 1

    10.2.6 xargs -0 将 \0 作为定界符

    find . -type f -name “*.log” -print0 | xargs -0 rm -f
    
    • 1

    10.2.7 统计一个源代码目录中所有 java文件的行数

    find . -type f -name “*.java” -print0 | xargs -0 wc -l
    
    • 1

    10.2.8 查找所有的 jpeg 文件,并且压缩

    find . -type f -name “*.jpeg” -print | xargs tar -czvf images.tar.gz
    
    • 1

    10.2.9 使用 xargs下载所有链接

    cat test-list.txt | xargs wget -c
    
    • 1
  • 相关阅读:
    # Vue3 setup 函数
    图像相似度识别算法aHash|dHash|PHash
    仿everything的文件搜索工具测试
    流量抓取工具(wireshark)
    Python 学习 Day44
    (七)《数电》——CMOS与TTL门电路
    基于ssm的潮牌运动服饰数码商城管理系统(idea+spring+springmvc+mybatis+jsp)
    基于JavaSwing开发中国象棋有效的设计与实现+论文+PPT+任务书+检查表 毕业设计
    软件部2022届讲课底稿------01背包
    ​iOS上架App Store的全攻略
  • 原文地址:https://blog.csdn.net/u013916029/article/details/128138025