shell 文件查找(六)

1、文件查找之find(一)

缺点效率比较低

语法格式: find [路径][选项][操作]

选项参数

  • -name                        根据文件名查找,区分大小写
    • find /etc -name '*.conf '       在/etc目录下查找以.conf结尾的文件或目录
    • find . -iname 'aA'                   在当前目录下查找文件aA,忽略大小写(即aa,aA,Aa,AA均符合)
  • -perm                         根据权限查找
    • find . -perm 664
  • -prune                       该选项可以排除某些查找目录
  • -user                          根据属主查找
    • find . -user hdfs                      查找属主为hdfs的所有文件或目录
  • -group                        根据属组查找
    • find . -group yarn                   查找属组为yarn的所有文件或目录
  • -mtime -n | +n           根据更改时间查找(天)
    • -n        n天以内修改的文件
    • +n        n天以外修改的文件
    • n          正好n天修改的文件
    • find /etc -mtime +10 -user root    查找/etc目录下10天之前修改且属主为root的文件
    • find /etc -mtime -5 -name '*.conf' 查找/etc目录下5天之内修改且以conf结尾的文件
  • -nouser                        查找无有效属主的文件或目录
    • find . -type f -nouser
  • -nogroup                      查找无有效属组的文件或目录
  • -newer file1                  查找更改时间比file1新的文件或目录
    • find /etc -newer /etc/rdma/rdma.conf       查找/etc目录下更改时间在/etc/rdma/rdma.conf  之后的文件或目录
  • -type                          按类型查找
    • f         文件                       find . -type f 
    • d        目录                       find . -type d
    • c        字符设备文件        find . -type c
    • b        块设备文件            find . -type b
    • l          链接文件               find . -type l
    • p         管道文件               find . -type p
  • -size -n +n                 按大小查找文件
    • -n        大小大于n的文件
    • +n        大小小于n的文件
    • find /etc -size +1M           查找/etc目录下大于1M的文件
    • find /etc -size -10000c   查找/etc目录下小于10000字节的文件
  • -mindepth n               从n级子目录开始搜索?
    • find /etc -mindepth 3      在/etc下的3级子目录开始搜索,例如:/etc/pki/tls/certs,一级子目录是/etc?
  • -maxdepth n              最多搜索到n级子目录?
    • find /etc -maxdepth 1 -name '*.conf'      在/etc下搜索符合条件的文件,但最多搜索到1级子目录,例如/etc/chrony.conf,1级子目录是/etc就是当前文件夹,最多搜到这种层次的目录,在其下找文件

-prune

通常和-path一起使用,用于将特定目录排除在搜索条件之外(-path ./opt -prune -o一起使用)
例子1:查找当前目录下所有普通文件,但排除test目录
find . -path ./test -prune -o -type f

例子2:查找当前目录下所有普通文件,但排除etc和opt目录
find . -path ./etc -prune -o -path ./opt -prune -o -type f(标红色的-o表示或)

例子3:查找当前目录下所有普通文件,但排除etc和opt目录,但属主为hdfs
find . -path ./etc -prune -o -path ./opt -prune -o -type f -a -user hdfs

shell 文件查找(六)_第1张图片

shell 文件查找(六)_第2张图片

2、文件查找之find(二)

操作

-print        打印输出

find ./ -name '*.conf' -print

-exec        对搜索到的文件执行特定的操作,格式为-exec 'command' {} \;

find ./test/ -type f -name '*.conf' -size +10k -exec rm -f {} \;         搜索/test下的文件(非目录),文件名以conf结尾,且大于10k,然后将其删除

find ./test -size +1M -exec cp {} ./test_5/ \;   将/test下大于1M的文件复制到test_5目录下

find ./test -name '*.log' -mtime +7 -exec rm -rf {} \;   将。/test目录下以log结尾的文件,且更改时间在7天以上的删除

-ok            和-exec功能一样,只是每次操作都会给用户提示

逻辑运算符:

  • -a            与
  • -o            或
  • -not|!        非

 find /etc -not -newer /etc/rdma/rdma.conf    查找/etc目录下更改时间在/etc/rdma/rdma.conf  之前的文件或目录

例子1:查找当前目录下,属主不是hdfs的所有文件        
            
                find . -not -user hdfs    

               find . ! -user hdfs

例子2:查找当前目录下的属主为hdfs或者以conf结尾的普通文件
            
                find . -type f -a -user hdfs -o -name '*.conf'

               观察find . -type f -a -user hdfs -o -name '*.conf' -exec ls -rlt {} \;
                
                find . -type f -a \( -user hdfs -o -name '*.conf' \) -exec ls -rlt {} \;

例子3:查找当前目录下,属主属于hdfs,且大小大于300字节的文件
            
                find . -type f -a -user hdfs -a -size +300c

3、find、locate、whereis和which总结及适用场景分析

locate命令介绍

  • 文件查找命令,所属软件包mlocate
  • 不同于find命令是在整块磁盘中搜索,locate在数据库文件中查找
  • find是默认完全匹配,locate则是默认部分匹配。例如按文件名查找log文件,find则只会找log文件,locate会找到所有的*log*

updatedb命令

  • 用户更新/var/lib/mlocate/mlocate.db
  • 所使用配置文件/etc/updatedb.conf,定义哪些目录下的文件更新到数据库。
  • 该命令在后台cron计划任务中定期执行

locate是部分匹配,只要部分匹配到,无论是文件还是目录都会找到

shell 文件查找(六)_第3张图片
 

[root@test ~]# touch abc.txt

[root@test ~]# locate abc.txt

[root@test ~]# ll -ah /var/lib/mlocate/mlocate.db

-rw-r----- 1 root slocate 948K 7月  20 08:25 /var/lib/mlocate/mlocate.db

[root@test ~]# updatedb

[root@test ~]# ll -ah /var/lib/mlocate/mlocate.db

-rw-r----- 1 root slocate 948K 7月  20 08:39 /var/lib/mlocate/mlocate.db

[root@test ~]# locate abc.txt

/root/abc.txt

whereis命令

查找某个命令的二进制程序文件,帮助文档,源代码文件。

  • -b      只返回二进制文件 
  • -m     只返回帮助文档文件
  • -s      只返回源代码文件

[root@test ~]# whereis python

python: /usr/bin/python /usr/bin/python2.7 /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/python.1.gz

[root@test ~]# whereis -m python

python: /usr/share/man/man1/python.1.gz

[root@test ~]# whereis -b python

python: /usr/bin/python /usr/bin/python2.7 /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7

[root@test ~]# whereis -s python

which命令

仅查找二进制程序文件

  • -b 只返回二进制文件

python:[root@test ~]# which python

/usr/bin/python

各命令适用场景推荐

shell 文件查找(六)_第4张图片

 

你可能感兴趣的:(shell,shell)