• Linux_文件查找


    Linux基础命令

    文件命名规则

    • 长度不能超过255个字符;
    • 不能使用/当文件名;
    • 严格区分大小写;

    一.文本查找(grep egrep fgrep)

    Pattern(模式) //文本字符和正则表达式定义的模式来过滤文本的命令。

    grep(文本三剑客之一)

    • 根据模式搜索文本,并将符合模式的文本行显示出来。
    • 使用基本正则表达式定义的模式来过滤文本的命令。
    1. grep abc //过滤并取出含有abc(小写abc)关键字的内容。
    [root@lc ~]# ls|grep abc
    abc
    abc-214625
    [root@lc ~]
    

    -i //忽略大小写。

    1. grep -i //过滤取出含abc(忽略大小写)关键字的内容。
    [root@lc ~]# ls|grep -i abc
    abc
    aBc
    AbC
    ABc
    ABC
    abc-214625
    [root@lc ~]
    
    1. grep -i abc$ //$代表以...结尾,过滤取出abc(忽略大小写)结尾的内容。
    [root@lc ~]# ls|grep -i abc$
    abc
    aBc
    AbC
    ABc
    ABC
    [root@lc ~]
    

    -v //取反,显示没有被模式匹配到的行。

    1. grep -v //取反,过滤取出以abc(小写abc)关键字结尾以外的内容。
    [root@lc ~]# ls|grep -v abc$
    201105
    a
    ab
    aBc
    AbC
    ABc
    ABC
    abc-214625
    anaconda-ks.cfg
    test
    [root@lc ~]
    

    -o //只显示被模式匹配到的字符串。

    1. grep -o //只显示被模式匹配到的字符串。

      [root@lc ~]# ls|grep -o abc
      abc
      abc
      [root@lc ~]
      
    2. grep -o ^abc$ //^...$用来精确查找;只取abc(小写abc)。

    • ^a表示查找以a开头的内容
    • c$表示查找以c结尾的内容
    [root@lc ~]# ls|grep -o ^abc$
    abc
    [root@lc ~]
    
    1. grep -io ^abc$ //^...$用来精确查找;只取abc(不分abc)。
    [root@lc ~]# ls|grep -io ^abc$
    abc
    aBc
    AbC
    ABc
    ABC
    [root@lc ~]# 
    

    -E // 使用扩展正则表达式,grep -E相当于使用egrep。

    1. grep -E '.B.$' //‘^.B.$’代表中间是B的内容;只取中间是B的内容。
    [root@lc ~]# ls|grep -E '.B.$'
    aBc
    ABc
    ABC
    [root@lc ~]# 
    
    1. grep -E 'AB.$|lB.$' //以|做分隔符代表和;只取AB开头和lB开头的内容。
    [root@lc ~]# ls|grep -E '^AB.$|^lB.$'
    ABc
    ABC
    lBv
    [root@lc ~]# 
    

    -q // 静默模式,不输出任何信息。

    1. grep -Eq 'aBc|aBj' //搜索aBc和aBj,不输出。
    • echo $? //0表示搜索到了;1表示没有搜索到。
    [root@lc ~]# ls|grep -Eq 'aBc|aBj'
    [root@lc ~]# echo $?
    0
    [root@lc ~]# ls|grep -Eq 'aBc|sdsds'
    [root@lc ~]# echo $?
    0
    [root@lc ~]# ls|grep -Eq 'aBcdsdsd|sdsds'
    [root@lc ~]# echo $?
    1
    [root@lc ~]# 
    
    • -A 1 //后面一行
    • -B 1//前面一行
    • -C 1//前后各一行
    1. A1 B1 C1 // after;before;contest,数字代表行数;搜索结果如下:
    [root@lc ~]# grep -A1 '^network' anaconda-ks.cfg 
    network  --bootproto=dhcp --device=ens160 --ipv6=auto --activate
    network  --hostname=localhost.localdomain
    repo --name="AppStream" --baseurl=file:///run/install/repo/AppStream
    [root@lc ~]# grep -B1 '^network' anaconda-ks.cfg 
    # Network information
    network  --bootproto=dhcp --device=ens160 --ipv6=auto --activate
    network  --hostname=localhost.localdomain
    [root@lc ~]# grep -C1 '^network' anaconda-ks.cfg 
    # Network information
    network  --bootproto=dhcp --device=ens160 --ipv6=auto --activate
    network  --hostname=localhost.localdomain
    repo --name="AppStream" --baseurl=file:///run/install/repo/AppStream
    [root@lc ~]# 
    

    文本查找(find)

    • text实时查找,精确性强,遍历指定目录中所有文件完成查找。
    • 查找速度慢,支持众多查找标准。
    • 语法:find [OPTION...] 查找路径 查找标准 查找到以后的处理动作。
    • 查找路径 //默认为当前目录。
    • 查找标准 //默认为指定路径下的所有文件。

    -name ‘filename’// 对文件名作精确匹配.支持glob通配符机制。

    -iname ‘filename’// -i 文件匹配时不区分大小写。

    1. find / - name abc //在根目录下查找以abc(小写abc)为名字的目录。

    [root@lc ~]# find / -name abc
    /opt/abc
    /tmp/abc
    [root@lc ~]# 
    

    2. find / - iname abc //在根目录下查找以abc(忽略大小写)为名字的目录。

    [root@lc ~]# find / -iname abc
    /etc/abC
    /root/aBc
    /opt/abc
    /opt/aBc
    /opt/AbC
    /opt/ABc
    /opt/ABC
    /tmp/abc
    /ABC
    [root@lc ~]# 
    

    -user username //根据用户来查找。

    1. find / -user jerry //查找和jerry用户有关的文件。
    [root@lc ~]# find / -user jerry
    /home/jerry
    /home/jerry/.bash_logout
    /home/jerry/.bash_profile
    /home/jerry/.bashrc
    find: ‘/proc/5309/task/5309/fd/7’: No such file or directory
    find: ‘/proc/5309/task/5309/fdinfo/7’: No such file or directory
    find: ‘/proc/5309/fd/6’: No such file or directory
    find: ‘/proc/5309/fdinfo/6’: No such file or directory
    /var/spool/mail/jerry
    

    -group groupname //根据用户组来查找。

    1. find / -group jerry //查找jerry用户组。
    [root@lc ~]# find / -group jerry
    /home/jerry
    /home/jerry/.bash_logout
    /home/jerry/.bash_profile
    /home/jerry/.bashrc
    find: ‘/proc/5316/task/5316/fd/7’: No such file or directory
    find: ‘/proc/5316/task/5316/fdinfo/7’: No such file or directory
    find: ‘/proc/5316/fd/6’: No such file or directory
    find: ‘/proc/5316/fdinfo/6’: No such file or directory
    [root@lc ~]# 
    

    -nouer // 查找没有属主的文件.用户被删除的情况下产生的文件,只有uid没有属主。

    -nogroup // 查找没有属组的文件.组被删除的情况下产生的文件,只有gid没有属组。

    1. find -nouer/find -nogroup //结果如下:
    [root@lc ~]# ll
    total 4
    -rw-r--r--. 1 1000 root    0 Nov  8 00:17 aBc
    -rw-------. 1 root 1000 1188 Nov  7 23:59 anaconda-ks.cfg
    [root@lc ~]# find -nouser
    ./aBc
    [root@lc ~]# find -nogroup
    ./anaconda-ks.cfg
    [root@lc ~]# 
    

    -type//根据文件类型来查(f,d,c,b,l,p,s)

    • f 普通文件
    • d 目录文件
    • c 字符设备文件
    • b 块设备文件
    • l 链接文件
    • p 管道文件
    • s 套接字文件

    find -type f/find -type d //查找普通文件和目录文件。

    [root@lc ~]# find -type f
    ./.bash_logout
    ./.bash_profile
    ./.bashrc
    ./.cshrc
    ./.tcshrc
    ./anaconda-ks.cfg
    ./abc/xixi
    [root@lc ~]# find -type d
    .
    ./abc
    [root@lc ~]# 
    

    -size //根据文件大小进行查找。如1k、1M,+10k、+10M,-1k、-1M。

    -size +2G /-2G //+代表大于2G;-代表小于2G。

    [root@lc ~]# find /opt -size +1G
    /opt/abc
    [root@lc ~]# find  -size -2k
    .
    ./.bash_logout
    ./.bash_profile
    ./.bashrc
    ./.cshrc
    ./.tcshrc
    ./hehe
    ./abc
    ./abc/xixi
    
    • -mtime //修改时间
    • -ctime //改变时间
    • -atime //访问时间

    查找2天以前;查找2天以内//+2是2天前,-2是2天以内。

    [root@lc ~]# find -mtime -2
    .
    ./anaconda-ks.cfg
    ./hehe
    ./abc
    ./abc/xixi
    [root@lc ~]# find -mtime +2
    ./.bash_logout
    ./.bash_profile
    ./.bashrc
    ./.cshrc
    ./.tcshrc
    [root@lc ~]# 
    
    • -mmin //多少分钟修改过
    • -cmin //多少分钟改变过
    • -amin //多少分钟访问过

    查找5分钟以前;查找5分钟以内//+2是2分钟前,-2是2分钟以内(60分钟是1小时)。

    [root@lc ~]# find -mmin +5
    .
    ./.bash_logout
    ./.bash_profile
    ./.bashrc
    ./.cshrc
    ./.tcshrc
    ./anaconda-ks.cfg
    ./hehe
    ./abc
    ./abc/xixi
    [root@lc ~]# find -mmin -5
    [root@lc ~]# 
    

    二.组合条件

    1. -a //默认情况;查找名字是abc并且大于1G的文件。
    [root@lc ~]# find / -name abc -a -size +1G
    /opt/abc
    
    1. -o //-o是或者的意思;查找大于1G或者名字叫abc的文件(proc目录里的文件忽略)。
    [root@lc ~]# find / -size +1G -o -name abc
    /proc/kcore
    find: ‘/proc/5569/task/5569/fd/6’: No such file or directory
    find: ‘/proc/5569/task/5569/fdinfo/6’: No such file or directory
    find: ‘/proc/5569/fd/5’: No such file or directory
    find: ‘/proc/5569/fdinfo/5’: No such file or directory
    /root/abc
    /opt/abc
    
    1. -not //取反;查找名字是abc但是大小不是大于1G的文件(在什么条件前面加not就是给那个条件取反)。
    [root@lc ~]# find / -name abc -not -size +1G
    /root/abc
    [root@lc ~]# ll -h /opt/
    total 2.0G
    -rw-r--r--. 1 root root 2.0G Nov  8 00:58 abc
    [root@lc ~]# 
    

    三.处理动作

    1. -print //默认是打印。
    [root@lc ~]# find -type l
    ./hehe
    [root@lc ~]# find -type l -print
    ./hehe
    [root@lc ~]# find -type l -ls
    101232241      0 lrwxrwxrwx   1  root     root            3 Nov  8 00:43 ./hehe -> aBc
    [root@lc ~]# 
    
    1. -ls //需要打出ls显示详细信息。
    [root@lc ~]# find -type l -ls
    101232241      0 lrwxrwxrwx   1  root     root            3 Nov  8 00:43 ./hehe -> aBc
    [root@lc ~]# 
    
    1. -delete//删除文件(不可以删除目录)。
    [root@lc ~]# find -type l -delete
    [root@lc ~]# ls
    abc  anaconda-ks.cfg
    [root@lc ~]# 
    
    1. -fls //把长格式信息文件写到至指定文件中。
    [root@lc ~]# find -type d -fls hehe
    [root@lc ~]# ls
    abc  anaconda-ks.cfg  hehe
    [root@lc ~]# cat hehe
    100663425      0 dr-xr-x---   3  root     root          137 Nov  8 01:58 .
     67533296      0 drwxr-xr-x   2  root     root           18 Nov  8 00:48 ./abc
    [root@lc ~]# 
    
    1. -ok //对查找到的每个文件执行COMMAND,每次操作都需要用户确认(这里是询问是否删除,输入y确定,n是拒绝)。
    [root@lc ~]# find / -name hehe
    /etc/hehe
    /opt/hehe
    /tmp/hehe
    [root@lc ~]# find / -name hehe -ok rm -rf {} ;
    < rm ... /etc/hehe > ? y
    < rm ... /opt/hehe > ? y
    < rm ... /tmp/hehe > ? y
    [root@lc ~]# 
    
    1. -exec // 对查找到的每个文件执行COMMAND,操作不需要确认(这里是不询问直接删除)。
    [root@lc ~]# find / -name hehe -exec rm {} ;
    [root@lc ~]# ls
    abc  anaconda-ks.cfg
    [root@lc ~]# ls /etc/hehe
    ls: cannot access '/etc/hehe': No such file or directory
    [root@lc ~]# ls /tmp/
    ks-script-axebdlv6  vmware-root_967-4248221830
    [root@lc ~]# 
    
    1. xargs //过管道将查找到的内容给xargs处理,xargs后面直接跟命令即可(一般用来删除文件)。
    [root@lc ~]# ls 
    123  abc  anaconda-ks.cfg
    [root@lc ~]# find -name abc|xargs rm -rf
    [root@lc ~]# ls
    123  anaconda-ks.cfg
    [root@lc ~]# 
    
  • 相关阅读:
    JSP
    token防止表单重复提交
    web应用程序性能优化
    js 优化
    文本查看及处理工具简单命令
    DNS之一---DNS服务及BIND服务,并实现DNS正向与反向解析
    企业级自动化运维工具应用实战ansible
    Linux小试牛刀
    Linux特殊权限及ACL权限
    文件的默认权限UMASK
  • 原文地址:https://www.cnblogs.com/leixixi/p/14202243.html
Copyright © 2020-2023  润新知