zoukankan      html  css  js  c++  java
  • Linux find常用用法

    简介

    find用于搜索文件或目录,功能非常强大。在搜索文件时,如果还要对搜索的文件进行后续的处理,一般都会结合xargs来实现。
    find搜索是从磁盘进行指定目录开始扫描,而不是从数据库搜索。

    语法

    find [path...] [expression_list]

    1.基础打印

    find命令默认接的命令是 -print,默认以 将找到的文件分割,如果想把 分割符换成其他分隔符,可使用tr。

    ydqun@VM-0-9-ubuntu /tmp % mkdir /tmp/test/a/ [0]
    ydqun@VM-0-9-ubuntu /tmp % touch /tmp/test/a/{1..5}.c [0]
    ydqun@VM-0-9-ubuntu /tmp % find /tmp/test/a/ [0]
    /tmp/test/a/
    /tmp/test/a/5.c
    /tmp/test/a/1.c
    /tmp/test/a/2.c
    /tmp/test/a/3.c
    /tmp/test/a/4.c
    

    2.文件名或文件路径搜索

    这里用到的是 -name(文件名)和-path(文件路径)。
    -name可以对文件的basename进行匹配,-path是可以对文件的dirname+basename。查找时,文件名最好使用引号保卫,通常会配合通配符使用。

    ydqun@VM-0-9-ubuntu /tmp % touch /tmp/test/a/other.c                                                         [130]
    ydqun@VM-0-9-ubuntu /tmp % ls /tmp/test/a/                                                                     [0]
    1.c  2.c  3.c  4.c  5.c  other.c
    ydqun@VM-0-9-ubuntu /tmp % find /tmp/test/a/ -name "*.c"                                                       [0]
    /tmp/test/a/other.c
    /tmp/test/a/5.c
    /tmp/test/a/1.c
    /tmp/test/a/2.c
    /tmp/test/a/3.c
    /tmp/test/a/4.c
    ydqun@VM-0-9-ubuntu /tmp % find /tmp/test/a/ -name "[1-5].c"                                                   [0]
    /tmp/test/a/5.c
    /tmp/test/a/1.c
    /tmp/test/a/2.c
    /tmp/test/a/3.c
    /tmp/test/a/4.c
    

    注意,不能再-name的模式中使用"/",除非文件名中到了字符"/",否则匹配不到任何东西,-name只对basename进行匹配。

    ydqun@VM-0-9-ubuntu test % find /tmp/test -name "*a*/*.c"                                            [0]
    find: warning: Unix filenames usually don't contain slashes (though pathnames do).  That means that '-name ‘*a*/*.c’' will probably evaluate to false all the time on this system.  You might find the '-wholename' test more useful, or perhaps '-samefile'.  Alternatively, if you are using GNU grep, you could use 'find ... -print0 | grep -FzZ ‘*a*/*.c’'.
    

    故,如果我们要在指定目录下搜索某个子目录中的文件,要使用-path而非-name。

    ydqun@VM-0-9-ubuntu test % find /tmp/test/ -path  "*a*/*.c"                                          [0]
    /tmp/test/a/other.c
    /tmp/test/a/5.c
    /tmp/test/a/1.c
    /tmp/test/a/2.c
    /tmp/test/a/3.c
    /tmp/test/a/4.c
    

    3.根据文件类型搜索:-type

    通常,如果我们知道要搜索文件的文件类型,我们可以使用-type进行搜索,一般需要搜索的文件有普通文件(f),目录(d)或链接文件(l)等。

    例如:

    搜索目录文件文件,且目录名字为a。

    ydqun@VM-0-9-ubuntu test % find /tmp/test/  -type d -name "a"                                        [0]
    /tmp/test/a
    

    搜索普通文件,且名字后缀为.c。

    ydqun@VM-0-9-ubuntu test % find /tmp/test/a/  -type f  -name "*.c"                                   [0]
    /tmp/test/a/other.c
    /tmp/test/a/5.c
    /tmp/test/a/1.c
    /tmp/test/a/2.c
    /tmp/test/a/3.c
    /tmp/test/a/4.c
    

    4.根据文件的三个时间进行修改

    ps:不理解文件的三个时间,可以看这个链接https://www.cnblogs.com/ydqblogs/p/14300625.html
    1)根据mtime进行搜索 -mtine
    2)根据ctime进行搜索 -ctime
    3)根据atime进行搜索 -atime

    例如

    搜索/tmp目录下一天内修改过内容的.c文件

    #注意 -1表示一天内,+1表示大于一天
    ydqun@VM-0-9-ubuntu a % find /tmp/test/a/ -type f  -mtime  -1  -name "*.c"                           [0]
    /tmp/test/a/other.c
    /tmp/test/a/5.c
    /tmp/test/a/1.c
    /tmp/test/a/2.c
    /tmp/test/a/3.c
    /tmp/test/a/4.c
    

    5.根据文件大小搜索:-size

    -size n[cwbkMG]

    注意:

    n表示文件大小数值,可以带+,-符号,带+表示大小数值比n大,带-表示大小数值比n小。
    [cwbkMG]:
    c -- 表示单位为字节
    w -- 表示单位为字
    k -- 表示KiB,1k = 1024c
    M -- 表示MiB,1M = 1024k = 1024 * 1024c
    G -- 表示GiB,1G = 1024M = 1024 * 1024k = 1024 * 1024 * 1024c
    

    例如

    搜索/tmp/test/a目录下,文件大小小于10个字节的后缀为.c的文件

    ydqun@VM-0-9-ubuntu a % find /tmp/test/a/ -type f  -size -10c   -name "*.c"                          [0]
    /tmp/test/a/other.c
    /tmp/test/a/5.c
    /tmp/test/a/2.c
    /tmp/test/a/3.c
    /tmp/test/a/4.c
    

    6.搜索空文件

    这里的空文件指没有内容的普通文件或者目录。

    例如:

    ydqun@VM-0-9-ubuntu a % mkdir empty                                                                  [0]
    ydqun@VM-0-9-ubuntu a % find /tmp/test/a/ -type d -empty                                             [0]
    /tmp/test/a/empty
    ydqun@VM-0-9-ubuntu a % find /tmp/test/a/ -type f  -empty                                            [0]
    /tmp/test/a/other.c
    /tmp/test/a/5.c
    /tmp/test/a/2.c
    /tmp/test/a/3.c
    /tmp/test/a/4.c
    

    7.根据权限搜索:-perm

    -perm可以根据文件的所有者对文件的可读可写可执行权限进行搜索。

    ydqun@VM-0-9-ubuntu a % chmod 775 1.c                                                                [0]
    ydqun@VM-0-9-ubuntu a % find ./ -perm -0775                                                          [0]
    ./1.c
    ydqun@VM-0-9-ubuntu a % find ./ -perm -0775  -name "*.c"                                             [0]
    ./1.c
    

    8.搜索到文件后删除该文件:-exec

    例如

    搜索到/tmp/test/a/目录下的后缀为.c的空文件,并删除。

    ydqun@VM-0-9-ubuntu /tmp % ls -lh /tmp/test/a/                                                       [0]
    total 8.0K
    -rwxrwxr-x 1 ydqun root   83 Jan 20 10:33 1.c
    -rw-r--r-- 1 ydqun root    0 Jan 20 11:10 2.c
    -rw-r--r-- 1 ydqun root    0 Jan 20 11:10 3.c
    -rw-r--r-- 1 ydqun root    0 Jan 20 11:10 4.c
    -rw-r--r-- 1 ydqun root    0 Jan 20 11:10 5.c
    drwxr-xr-x 2 ydqun root 4.0K Jan 20 10:56 empty
    -rw-r--r-- 1 ydqun root    0 Jan 20 11:10 empty.c
    ydqun@VM-0-9-ubuntu /tmp % find /tmp/test/a/ -type f -empty -exec rm -r '{}' ;                      [0]
    ydqun@VM-0-9-ubuntu /tmp % ls -lh /tmp/test/a/                                                       [0]
    total 8.0K
    -rwxrwxr-x 1 ydqun root   83 Jan 20 10:33 1.c
    drwxr-xr-x 2 ydqun root 4.0K Jan 20 10:56 empty
    ydqun@VM-0-9-ubuntu /tmp %  
    

    9.搜索指定日期范围的文件

    例如

    所有/tmp/test目录下,在日期2021-01-12到2021-01-20之间修改过的文件

    ydqun@VM-0-9-ubuntu test % touch -m -d 2021-01-12 tmp1.txt                                           [0]
    ydqun@VM-0-9-ubuntu test % touch -m -d 2021-01-19 target.txt                                         [0]
    ydqun@VM-0-9-ubuntu test % touch -m -d 2021-01-20 tmp2.txt                                           [0]
    ydqun@VM-0-9-ubuntu test % find ./ -type f -newer tmp1.txt -a ! -newer tmp3.txt                      [0]
    find: ‘tmp3.txt’: No such file or directory
    ydqun@VM-0-9-ubuntu test % find ./ -type f -newer tmp1.txt -a ! -newer tmp2.txt                      [1]
    ./tmp2.txt
    ./target.txt
    

    这里会把tmp2.txt也搜索出来,newer xxx表示是比xxx文件修改时间更新,!取反表示更久或者相同。

    10.获取文件名: -printf

    find的 -printf选项有很多修饰符功能,在处理路径方面,有%f、%P、%p,其中%f就是用来获取去掉前缀路径后的文件名;%p是获取路径自身;%P是获取除了find搜索路径的剩余部分路径。

    例如

    1)使用%f获取不带路径的文件名

    ydqun@VM-0-9-ubuntu test % touch b/{a,b,c}.log                                                       [0]
    ydqun@VM-0-9-ubuntu test % cd b/                                                                     [0]
    ydqun@VM-0-9-ubuntu b % pwd                                                                          [0]
    /tmp/test/b
    ydqun@VM-0-9-ubuntu b % ls                                                                           [0]
    a.log  b.log  c.log
    #find的默认输出的文件都带路径
    ydqun@VM-0-9-ubuntu b % find /tmp/test/b/                                                            [0]
    /tmp/test/b/
    /tmp/test/b/b.log
    /tmp/test/b/a.log
    /tmp/test/b/c.log
    #利用%f获取去掉路径的文件名字
    ydqun@VM-0-9-ubuntu b % find /tmp/test/b/  -printf "%f
    "                                            [0]
    b/
    b.log
    a.log
    c.log
    

    2)使用%P,去掉find搜索路径/tmp/test/b部分后,输出文件名,如果/tmp/test/b下还有带有普通文件的子目录,则打印的部分文件带该子目录。

    ydqun@VM-0-9-ubuntu b % mkdir c                                                                      [0]
    ydqun@VM-0-9-ubuntu b % touch c/{d,e,f}.log
    ydqun@VM-0-9-ubuntu b % ls c/                                                                        [2]
    d.log  e.log  f.log
    ydqun@VM-0-9-ubuntu b % find /tmp/test/b/                                                            [0]
    /tmp/test/b/
    /tmp/test/b/b.log
    /tmp/test/b/a.log
    /tmp/test/b/c
    /tmp/test/b/c/e.log
    /tmp/test/b/c/d.log
    /tmp/test/b/c/f.log
    /tmp/test/b/c.log
    ydqun@VM-0-9-ubuntu b % find /tmp/test/b/  -printf "%P
    "                                            [0]
    
    b.log
    a.log
    c
    c/e.log
    c/d.log
    c/f.log
    c.log
    

    总结

    这部分是find命令常用用法,掌握了之后,大多数时候,在Linux找文件就特别方便快捷了。

  • 相关阅读:
    qt tableview中如何添加右键菜单且不可编辑单元格
    C++typedef struct和struct的区别
    Qt5配置winpCap
    C++虚函数和纯虚函数的用法和区别
    c++11 智能指针 unique_ptr、shared_ptr与weak_ptr
    C++ windows进程间通信
    C++并发多线程(一)
    Maven无法下载2.1.7.js7版本的itext依赖
    PostgreSQL
    SQL
  • 原文地址:https://www.cnblogs.com/ydqblogs/p/14301518.html
Copyright © 2011-2022 走看看