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


    find文件查找

    在目录层次结构中搜索文件。find命令可以根据名称、属性、创建时间、大小、所属用户等方式查找文件。

    find命令介绍

    格式:find 搜索路径 参数 参数相关匹配值 指令(-print)

    find指令

    • -print (输出结果,默认)
    • -ls 类似于ls -lhi
    #选项
    -type	#根据文件类型进行查找
    	f	#普通文件
    	d	#目录
    	l	#软连接文件
    	s	#sockt文件	套接字文件	
    

    根据名称查找一个文件

    我记得,Linux系统中有一个文件叫redhat-release,我怎查找到它的位置。

    [root@localhost ~]# find  /   -name  "redhat-release" 
    /etc/redhat-release
    /usr/share/doc/redhat-release
    /usr/share/redhat-release
    
    参数
    • -name “文件名称” 基于名称的精确查找

    • -iname 基于名称忽略大小写的精确查找

      [root@localhost ~]# find /root/ -iname "abc*"
      /root/abc1
      /root/abc2
      /root/abc3
      /root/abc4
      /root/abc5
      /root/abc6
      /root/Abc1
      /root/Abc2
      /root/Abc3
      /root/Abc4
      /root/Abc5
      /root/Abc6
      /root/Abc7
      /root/Abc8
      /root/Abc9
      
    • 通配符

      • *:匹配所有(可以匹配空字符)

        [root@localhost ~]# find /etc/ -name "ifcfg-*"
        /etc/sysconfig/network-scripts/ifcfg-lo
        /etc/sysconfig/network-scripts/ifcfg-eth0
        /etc/sysconfig/network-scripts/ifcfg-eth1
        
        # 查询以eth0开头的文件
        [root@localhost ~]# find / -name "eth0*"
        /root/eth0xxx
        
        # 查询以eth0结尾的文件
        [root@localhost ~]# find / -name "*eth0"
        /etc/sysconfig/network-scripts/ifcfg-eth0
        /root/xxxeth0
        
        # 查询文件名称包含eth0的文件
        [root@localhost ~]# find / -name "*eth0*"
        /etc/sysconfig/network-scripts/ifcfg-eth0
        /root/eth0xxx
        /root/xxxeth0
        /root/xxxeth0xxx
        
      • [] : 匹配中括号中的任意一个字符

        [root@localhost ~]# touch abc{1..9}
        [root@localhost ~]# find /root/ -name "abc[359]"
        /root/abc3
        /root/abc5
        /root/abc9
        [root@localhost ~]# find /root/ -name "oldboy[tsdgh]"
        /root/oldboyd
        /root/oldboyg
        /root/oldboyh
        /root/oldboys
        /root/oldboyt
        
      • [^] : 匹配除掉中括号中的内容之外的跟前面匹配的上的所有文件

        [root@localhost ~]# find /root/ -name "abc[^359]"
        /root/abc1
        /root/abc2
        /root/abc4
        /root/abc6
        /root/abc7
        /root/abc8
        [root@localhost ~]# ls
        abc1  abc8             oldboyc  oldboyj  oldboyq  oldboyx
        abc2  abc9             oldboyd  oldboyk  oldboyr  oldboyy
        abc3  anaconda-ks.cfg  oldboye  oldboyl  oldboys  oldboyz
        abc4  dir              oldboyf  oldboym  oldboyt  xxxeth0
        abc5  eth0xxx          oldboyg  oldboyn  oldboyu  xxxeth0xxx
        abc6  oldboya          oldboyh  oldboyo  oldboyv  系统优化.md
        abc7  oldboyb          oldboyi  oldboyp  oldboyw
        
      • ! : 对整个搜索结果进行取反

        [root@localhost ~]# find /root/ -name "abc[^359]"
        /root/abc1
        /root/abc2
        /root/abc4
        /root/abc6
        /root/abc7
        /root/abc8
        [root@localhost ~]# find /root/ -name "abc[359]"
        /root/abc3
        /root/abc5
        /root/abc9
        [root@localhost ~]# find /root/ ! -name "abc[359]"
        /root/
        /root/.bash_logout
        /root/.bash_profile
        /root/.bashrc
        /root/.cshrc
        /root/.tcshrc
        /root/anaconda-ks.cfg
        /root/.bash_history
        
      • ?:匹配任意一个字符(该字符不能为空)

        [root@localhost ~]# find /root/ -name "abc*"
        /root/abc1
        /root/abc2
        /root/abc3
        /root/abc4
        /root/abc5
        /root/abc6
        /root/abc7
        /root/abc8
        /root/abc9
        /root/abc10
        /root/abc11
        /root/abc12
        /root/abc13
        /root/abc14
        /root/abc15
        /root/abc16
        /root/abc17
        /root/abc18
        /root/abc19
        /root/abc20
        [root@localhost ~]# find /root/ -name "abc?"
        /root/abc1
        /root/abc2
        /root/abc3
        /root/abc4
        /root/abc5
        /root/abc6
        /root/abc7
        /root/abc8
        /root/abc9
        [root@localhost ~]# 
        

    根据文件大小查找

    根据文件的体积查询出我们所需要的文件,find会自动四舍五入

    参数

    • -a :并且(默认使用-a)

      [root@localhost ~]# find /var/log -size +2k -size -10k
      /var/log
      /var/log/secure
      /var/log/vmware-vgauthsvc.log.0
      /var/log/vmware-vmsvc.log
      /var/log/cron
      /var/log/vmware-network.3.log
      [root@localhost ~]# find /var/log -size +2k -a -size -10k
      /var/log
      /var/log/secure
      /var/log/vmware-vgauthsvc.log.0
      /var/log/vmware-vmsvc.log
      /var/log/cron
      /var/log/vmware-network.3.log
      
    • -o : 或者

      [root@localhost ~]# find /var/log -size -2k -o -size +10k
      /var/log/tallylog
      /var/log/grubby_prune_debug
      /var/log/lastlog
      /var/log/wtmp
      /var/log/btmp
      
    • -size

      # b(默认)    c    w   K  M   G
      
      -size   n  (单位)
      
      +n : 大于n个单位
      -n : 小于n个单位
      n  : 等于n个单位
      
      # 注:find查找同时打印出隐藏文件
      # 注:n必须是整数,不能是小数
      [root@localhost log]# find /root/ -size 8.8k
      find: Invalid argument `8.8k' to -size
      
      • +n : 大于n个单位的文件

        [root@localhost ~]# find /root/ -size +1k 
        /root/anaconda-ks.cfg
        /root/系统优化.md
        /root/123.log
        
      • -n : 小于n个单位的文件

        [root@localhost ~]# find /root/ -size -1k 
        /root/eth0xxx
        /root/xxxeth0
        /root/xxxeth0xxx
        
      • n : 等于n个单位的文件但是不精确

        [root@localhost ~]# ls -lh
        total 640K
        -rw-r--r--  1 root root 632K Mar  9 10:31 123.log
        -rw-------. 1 root root 1.8K Mar  3 10:51 anaconda-ks.cfg
        drwxr-xr-x. 3 root root   17 Mar  4 10:51 dir
        -rw-r--r--  1 root root    0 Mar  9 09:28 eth0xxx
        -rw-r--r--  1 root root    0 Mar  9 09:28 xxxeth0
        -rw-r--r--  1 root root    0 Mar  9 09:28 xxxeth0xxx
        -rw-r--r--. 1 root root 2.3K Mar  4 10:46 系统优化.md
        [root@localhost ~]# find /root/ -size 2k -ls
        33574978    4 -rw-------   1 root     root         1757 Mar  3 10:51 /root/anaconda-ks.cfg
        
      • c : 字节

        [root@localhost log]# find /var/log -size 674c
        /var/log/vmware-network.5.log
        /var/log/vmware-network.4.log
        /var/log/vmware-network.2.log
        
        [root@localhost log]# find /var/log/ -size -300c -ls
        33771984    0 -rw-------   1 root     root            0 Mar  3 10:45 /var/log/tallylog
        33852252    0 -rw-------   1 root     utmp            0 Mar  4 10:38 /var/log/btmp
        34126340    0 -rw-------   1 root     root            0 Mar  3 10:47 /var/log/spooler
        34126393    0 drwxr-xr-x   2 root     root           23 Feb  3 00:30 /var/log/tuned
        17662708    0 drwx------   2 root     root           23 Aug  8  2019 /var/log/audit
        51428521    0 drwxr-xr-x   2 root     root          176 Mar  3 10:51 /var/log/anaconda
        51428529    0 -rw-------   1 root     root            0 Mar  3 10:51 /var/log/anaconda/ks-script-NeF0lj.log
            70    0 drwxr-xr-x   2 root     root            6 Mar  3 10:51 /var/log/rhsm
        33574980    0 -rw-------   1 root     root            0 Mar  9 10:28 /var/log/boot.log
        33574994    4 -rw-r-----   1 root     root          253 Mar  4 10:41 /var/log/firewalld
        

    按照时间进行查找

    按照文件的创建时间、修改时间以及访问时间进行筛选查找。

    参数

    • -ctime: 文件变更时间(修改了位置(mv)、所属组、所属用户)

      [root@localhost test]# for i in 1 2 3 4 5 6 7 8 9;do date -s "2021-03-0$i 11:30:30";touch 2021-03-0$i.txt;  done && ntpdate ntp.aliyun.com
      
      [root@localhost test]# 
      [root@localhost test]# # 1、查询文件变更时间在3天以前
      [root@localhost test]# # 2、查询文件变更时间在3天以内
      [root@localhost test]# # 3、查询文件变更时间在3天这个时候的
      [root@localhost test]# 
      [root@localhost test]# 
      [root@localhost test]# find ./ -ctime +3
      ./2021-03-01.txt
      ./2021-03-02.txt
      ./2021-03-03.txt
      ./2021-03-04.txt
      ./2021-03-05.txt
      [root@localhost test]# find ./ -ctime -3
      ./
      ./2021-03-07.txt
      ./2021-03-08.txt
      ./2021-03-09.txt
      [root@localhost test]# find ./ -ctime 3
      ./2021-03-06.txt
      [root@localhost test]# 
      
      
      
      [root@localhost test]# mkdir backup
      [root@localhost test]# mv 2021-03-01.txt backup/
      [root@localhost test]# stat backup/2021-03-01.txt
        File: ‘backup/2021-03-01.txt’
        Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
      Device: fd00h/64768d	Inode: 17307574    Links: 1
      Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
      Access: 2021-03-01 11:30:30.000000000 +0800
      Modify: 2021-03-01 11:30:30.000000000 +0800
      Change: 2021-03-09 11:58:06.369682903 +0800
       Birth: -
      [root@localhost test]# find ./ -ctime ^C
      [root@localhost test]# mv 2021-03-01.txt backup/^C
      [root@localhost test]# 
      [root@localhost test]# 
      [root@localhost test]# find ./ -ctime -3
      ./
      ./2021-03-07.txt
      ./2021-03-08.txt
      ./2021-03-09.txt
      ./backup
      ./backup/2021-03-01.txt
      [root@localhost test]# 
      
    • atime : 访问时间(cat)

      [root@localhost test]# rm -rf ./* && for i in 1 2 3 4 5 6 7 8 9;do date -s "2021-03-0$i 11:30:30";touch 2021-03-0$i.txt; done
      Mon Mar  1 11:30:30 CST 2021
      Tue Mar  2 11:30:30 CST 2021
      Wed Mar  3 11:30:30 CST 2021
      Thu Mar  4 11:30:30 CST 2021
      Fri Mar  5 11:30:30 CST 2021
      Sat Mar  6 11:30:30 CST 2021
      Sun Mar  7 11:30:30 CST 2021
      Mon Mar  8 11:30:30 CST 2021
      Tue Mar  9 11:30:30 CST 2021
      [root@localhost test]# ntpdate ntp.aliyun.com
       9 Mar 12:03:08 ntpdate[22696]: step time server 203.107.6.88 offset 1938.313235 sec
      [root@localhost test]# 
      [root@localhost test]# 
      [root@localhost test]# 
      [root@localhost test]# 
      [root@localhost test]# 
      [root@localhost test]# 
      [root@localhost test]# 
      [root@localhost test]# find ./ -atime +3
      ./2021-03-01.txt
      ./2021-03-02.txt
      ./2021-03-03.txt
      ./2021-03-04.txt
      ./2021-03-05.txt
      [root@localhost test]# find ./ -atime -3
      ./
      ./2021-03-07.txt
      ./2021-03-08.txt
      ./2021-03-09.txt
      [root@localhost test]# find ./ -atime 3
      ./2021-03-06.txt
      [root@localhost test]# cat 2021-03-03.txt 
      [root@localhost test]# find ./ -atime +3
      ./2021-03-01.txt
      ./2021-03-02.txt
      ./2021-03-04.txt
      ./2021-03-05.txt
      [root@localhost test]# find ./ -atime -3
      ./
      ./2021-03-03.txt
      ./2021-03-07.txt
      ./2021-03-08.txt
      ./2021-03-09.txt
      [root@localhost test]# stat 2021-03-03.txt 
        File: ‘2021-03-03.txt’
        Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
      Device: fd00h/64768d	Inode: 17307576    Links: 1
      Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
      Access: 2021-03-09 12:03:59.906083902 +0800
      Modify: 2021-03-03 11:30:30.000000000 +0800
      Change: 2021-03-03 11:30:30.000000000 +0800
       Birth: -
      [root@localhost test]# 
      
    • -mtime: 内容修改时间(包含创建时间)

      # 正好3天前,这一天创建的文件
      [root@localhost test]# find /root/test/ -mtime 3
      /root/test/2021-03-06.txt
      
      # 正好3天以内,创建的文件
      [root@localhost test]# find /root/test/ -mtime -3
      /root/test/
      /root/test/2021-03-07.txt
      /root/test/2021-03-08.txt
      
      # 正好3天以前创建的文件
      [root@localhost test]# find /root/test/ -mtime +3
      /root/test/
      /root/test/2021-03-05.txt
      /root/test/2021-03-04.txt
      [root@localhost test]# 
      

    按照属性查找(-type)

    格式

    find 路径 -type  文件类型 指令
    
    f(-) : 普通文件
    d    : 目录文件
    l	 : 连接文件(快捷方式)
    b	 : 设备文件
    c	 : 字符设备文件
    s	 : 套接字文件
    p	 : 管道文件
    
    
    [root@localhost dev]# find ./ -type b
    ./dm-1
    ./dm-0
    ./sda2
    ./sda1
    ./sda
    ./sr0
    [root@localhost dev]# find ./ -type b -ls
     10766    0 brw-rw----   1 root     disk     253,   1 Mar  9 09:11 ./dm-1
     10706    0 brw-rw----   1 root     disk     253,   0 Mar  9 09:11 ./dm-0
     10562    0 brw-rw----   1 root     disk       8,   2 Mar  9 09:11 ./sda2
     10561    0 brw-rw----   1 root     disk       8,   1 Mar  9 09:11 ./sda1
     10560    0 brw-rw----   1 root     disk       8,   0 Mar  9 09:11 ./sda
     10557    0 brw-rw----   1 root     cdrom     11,   0 Mar  9 09:11 ./sr0
    [root@localhost dev]# find / -type s -ls
       328    0 srw-rw-rw-   1 root     root            0 Mar  9 09:11 /dev/log
     20775    0 srw-rw-rw-   1 root     root            0 Mar  9 09:11 /run/abrt/abrt.socket
     20753    0 srw-rw-rw-   1 root     root            0 Mar  9 09:11 /run/vmware/guestServicePipe
     19525    0 srw-rw-rw-   1 root     root            0 Mar  9 09:11 /run/dbus/system_bus_socket
     11833    0 srw-------   1 root     root            0 Mar  9 09:11 /run/lvm/lvmpolld.socket
     11777    0 srw-------   1 root     root            0 Mar  9 09:11 /run/lvm/lvmetad.socket
     11884    0 srw-------   1 root     root            0 Mar  9 09:11 /run/udev/control
     11831    0 srw-------   1 root     root            0 Mar  9 09:11 /run/systemd/shutdownd
     
     # 查询出/root目录下,3天前创建的文件
     [root@localhost dev]# find /root/ -type f -ctime +3
    /root/.bash_logout
    /root/.bash_profile
    /root/.bashrc
    /root/.cshrc
    /root/.tcshrc
    /root/anaconda-ks.cfg
    /root/.viminfo
    /root/系统优化.md
    [root@localhost dev]# find /root/ -type f -a -ctime +3
    /root/.bash_logout
    /root/.bash_profile
    /root/.bashrc
    /root/.cshrc
    /root/.tcshrc
    /root/anaconda-ks.cfg
    /root/.viminfo
    /root/系统优化.md
    
    # 查询系统三天以内[root@localhost dev]# find / -type s -ctime -3
    /dev/log
    /run/abrt/abrt.socket
    /run/vmware/guestServicePipe
    /run/dbus/system_bus_socket
    /run/lvm/lvmpolld.socket
    /run/lvm/lvmetad.socket
    /run/udev/control创建的套接字文件有哪些?
    [root@localhost dev]# find / -type s -ctime -3
    /dev/log
    /run/abrt/abrt.socket
    /run/vmware/guestServicePipe
    /run/dbus/system_bus_socket
    /run/lvm/lvmpolld.socket
    /run/lvm/lvmetad.socket
    /run/udev/control
    

    find指令

    常用的find指令

    -print :打印结果集
    [root@localhost dev]# find / -type s -ctime -3 -print
    /dev/log
    /run/abrt/abrt.socket
    /run/vmware/guestServicePipe
    /run/dbus/system_bus_socket
    /run/lvm/lvmpolld.socket
    /run/lvm/lvmetad.socket
    /run/udev/control
    
    -ls : 打印结果集详情
    [root@localhost dev]# find / -type s -ctime -3 -ls
       328    0 srw-rw-rw-   1 root     root            0 Mar  9 09:11 /dev/log
     20775    0 srw-rw-rw-   1 root     root            0 Mar  9 09:11 /run/abrt/abrt.socket
     20753    0 srw-rw-rw-   1 root     root            0 Mar  9 09:11 /run/vmware/guestServicePipe
     19525    0 srw-rw-rw-   1 root     root            0 Mar  9 09:11 /run/dbus/system_bus_socket
     11833    0 srw-------   1 root     root            0 Mar  9 09:11 /run/lvm/lvmpolld.socket
     11777    0 srw-------   1 root     root            0 Mar  9 09:11 /run/lvm/lvmetad.socket
    
    -delete : 删除结果集
    [root@localhost test]# ls
    Abc  abc1  Abc1  abc10  abc2  abc23  abc3  abc4  abc5
    [root@localhost test]# find ./ -iname "abc?" -delete
    [root@localhost test]# ls
    Abc  abc10  abc23
    [root@localhost test]# 
    
    -exec : 对结果集进行下一步处理
    # 格式
    	find 路径 参数  参数表达式  -exec  命令 {} ;
    
    [root@localhost test]# find ./ -iname "abc"
    ./Abc
    [root@localhost test]# find ./ -iname "abc" -exec ls -l {} ;
    -rw-r--r-- 1 root root 0 Mar 10 09:44 ./Abc
    [root@localhost test]# 
    [root@localhost test]# find ./ -iname "abc"
    ./Abc
    [root@localhost test]# find ./ -iname "abc" -exec rm -rf {} ;
    [root@localhost test]# ls
    abc10  abc23
    [root@localhost test]# 
    
    -ok : 对结果集进行下一步处理(交互)
    # 格式
    	find 路径 参数  参数表达式  -ok   命令 {} ;
    
    [root@localhost test]# find ./ -iname "abc10" 
    ./abc10
    [root@localhost test]# find ./ -iname "abc10" -ok rm {} ;
    < rm ... ./abc10 > ? n
    [root@localhost test]# ls
    abc10  abc23
    [root@localhost test]# find ./ -iname "abc10" -ok rm {} ;
    < rm ... ./abc10 > ? y
    [root@localhost test]# ls
    abc23
    [root@localhost test]# 
    

    find和xargs命令配合

    # 格式
    	find   路径  参数   参数表达式  | xargs ...(其他命令做处理)
    
    [root@localhost ~]#  find ./test/ -type f | xargs -I {} ls -l {}
    -rw-r--r-- 1 root root 0 Mar 10 09:24 ./test/abc23
    [root@localhost ~]# ls -l
    total 640
    -rw-r--r--  1 root root 646165 Mar  9 10:31 123.log
    -rw-------. 1 root root   1757 Mar  3 10:51 anaconda-ks.cfg
    drwxr-xr-x. 3 root root     17 Mar  4 10:51 dir
    -rw-r--r--  1 root root      0 Mar  9 09:28 eth0xxx
    drwxr-xr-x  2 root root     19 Mar 10 11:08 test
    -rw-r--r--  1 root root      0 Mar  9 09:28 xxxeth0
    -rw-r--r--  1 root root      0 Mar  9 09:28 xxxeth0xxx
    -rw-r--r--. 1 root root   2344 Mar  4 10:46 系统优化.md
    [root@localhost ~]# find ./test/ -type f
    ./test/abc23
    [root@localhost ~]# find ./test/ -type f | xargs -I {} ls {}
    ./test/abc23
    [root@localhost ~]# 
    
    [root@localhost ~]# find ./test/ -type f 
    ./test/abc23
    [root@localhost ~]# find ./test/ -type f | xargs -I {} cp {} /opt/
    [root@localhost ~]# ls -l /opt/
    total 0
    -rw-r--r-- 1 root root 0 Mar 10 11:04 Abc
    -rw-r--r-- 1 root root 0 Mar 10 11:23 abc23
    
    [root@localhost ~]# find  ./test/ -type f -exec cp {} /opt/ ;
    [root@localhost ~]# ls -l /opt/
    total 0
    -rw-r--r-- 1 root root 0 Mar 10 11:24 abc23
    [root@localhost ~]# 
    

    习题

    1、find的格式
    
    命令  路径  参数   参数表达式  指令
    
    2、查询系统中文件名为:resolv.conf
    
    find / -name "resolv.conf"
    
    3、查询系统中大于1个G的文件
    
    find / -size +1G
    
    4、查询系统中文件名包含sys的文件
    
    find / -name "*sys*"
    
    5、查询大于1k小于10k, 同时创建时间在1天以外的文件
    
    find / -size +1k -a -size -10k -ctime +1
    
    6、查询/root目录下的,大于1k小于10k的文件并删除
    #方式一:使用指令删除
     find /root/ -size +1k -a -size -10k -delete
     #方式二:用-exec删除
     find /root/ -size +1k -a -size -10k -exec rm -f {} ;
     #方式三:用| xargs删除
     find /root/ -size +1k -a -size -10k | xargs -I {} rm -f {}
     #方式四:
     rm -rfv $(find /root/ -size +1k -a -size -10k)
    
    7、查询/root目录下的,大于0小于100的文件并移动到/opt目录
    
    find /root/ -size -100c  -exec mv {} /opt
    
    8、查找/var目录下属主为root,且属组为mail的所有文件
    
    find /var -user root -group mail -type f
    
    9、查找/var目录下最近一周内其内容修改过,同时没有属主的文件
    
    find /var -mtime -7 -nouser -type f
    
    10、查找当前系统上没有属主或属组,且最近一个周内曾被访问过的文件
    
    find / -nouser -nogroup -atime -7
    
    11、查找/etc目录下大于1M且类型为普通文件的所有文件
    find /etc/ -size +1M -type f
    
  • 相关阅读:
    webpack详解
    扩展运算符及其在vuex的辅助函数里的应用详解
    react组件间传值详解
    类的构造函数(3)
    类构造函数(4)
    new delete
    类的构造函数(1)
    类的构造函数(2)
    类的继承(1)
    静态成员数据与函数
  • 原文地址:https://www.cnblogs.com/zhaokunhao/p/14506459.html
Copyright © 2011-2022 走看看