find用法
find
用于按照指定条件来查找文件
参数 作用
-name 匹配名称
-perm 匹配权限(mode为完全匹配,-mode为包含即可)
-user 匹配所有者
-group 匹配所有组
-mtime -n +n 匹配修改内容的时间(-n指n天以内,+n指n天以前)
-atime -n +n 匹配访问文件的时间(-n指n天以内,+n指n天以前)
-ctime -n +n 匹配修改文件权限的时间(-n指n天以内,+n指n天以前)
-nouser 匹配无所有者的文件
-nogroup 匹配无所有组的文件
-type b/d/c/p/l/f 匹配文件类型(后面的字幕字母依次表示块设备、目录、字符设备、管道、链接文件、文本文0件)
-size 匹配文件的大小(+50KB为查找超过50KB的文件,而-50KB为查找小于50KB的文件)
-exec …… {} \; 后面可跟用于进一步处理搜索结果的命令(下文会有演示)
1. 匹配文件名字 ——name
╭─root@zxw ~
╰─➤ find / -name zxw | head -3 130 ↵
/var/lib/AccountsService/users/zxw
/var/lib/mysql/zxw
/home/zxw
2. 匹配权限——perm
╭─root@zxw ~
╰─➤ find / -perm 777 | head -3
/dev/cdrom
/dev/snd/by-path/pci-0000:02:02.0
/dev/initctl
3. 匹配属主——user
╭─root@zxw ~
╰─➤ find / -user zhao | tail -3 1 ↵
/home/zxw/.local/share/nautilus/scripts
/home/zxw/.esd_auth
/home/zxw/.bash_history
4. 匹配属组——group
╭─root@zxw ~
╰─➤ find / -group zxw | tail -3
find: ‘/proc/3318/task/3318/fd/6’: 没有那个文件或目录
find: ‘/proc/3318/task/3318/fdinfo/6’: 没有那个文件或目录
find: ‘/proc/3318/fd/6’: 没有那个文件或目录
find: ‘/proc/3318/fdinfo/6’: 没有那个文件或目录
/home/zxw/.local/share/nautilus/scripts
/home/zxw/.esd_auth
/home/zxw/.bash_history
5. 匹配文件大小——size
╭─root@zxw
╰─➤ find / -size +10M | tail -3 1 ↵
/mnt/Packages/webkitgtk3-2.4.9-6.el7.x86_64.rpm
/mnt/Packages/wireshark-1.10.14-10.el7.x86_64.rpm
/mnt/Packages/xulrunner-31.6.0-2.el7.centos.x86_64.rp
6. 匹配文件类型——type
目录、链接文件、 文本文件
b l f
╭─root@zxw ~
╰─➤ find / -type l | head -3 127 ↵
/dev/cdrom
/dev/snd/by-path/pci-0000:02:02.0
/dev/initctl
7. 删除
╭─root@zxw ~
╰─➤ find / -exec rm {} \;
9. mtime: 指文件修改时间,权限,属主,属组
例子1:找到文件修改时间一天以上的
[root@ken ~]# find / -mtime +1 | head -3
/boot
/boot/efi
/boot/efi/EFI
[root@ken ~]# stat /boot
File: ‘/boot’
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 801h/2049d Inode: 64 Links: 5
Access: (0555/dr-xr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-05-24 19:31:37.453000000 +0800
Modify: 2019-02-26 22:25:57.574000000 +0800
Change: 2019-03-14 18:49:48.066000000 +0800
Birth: –
10. atime:指定文件访问时间
[root@ken ~]# find / -atime +1 | head -3
/boot/grub2/device.map
/boot/grub2/i386-pc/gcry_rmd160.mod
/boot/grub2/i386-pc/acpi.mod
^C
[root@ken ~]# stat /boot/grub2/device.map
File: ‘/boot/grub2/device.map’
Size: 64 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 1572929 Links: 1
Access: (0644/-rw-r–r–) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-02-26 22:25:05.730000000 +0800
Modify: 2019-02-26 22:25:05.582000000 +0800
Change: 2019-02-26 22:25:05.582000000 +0800
Birth: –
11.ctime:指定文件内容
例子1:指定文件内容修改一天以上
[root@ken ~]# find / -ctime +1 | head -3
/boot
/boot/efi
/boot/efi/EFI
^C^
[root@ken ~]# stat /boot
File: ‘/boot’
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 801h/2049d Inode: 64 Links: 5
Access: (0555/dr-xr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-05-24 19:31:37.453000000 +0800
Modify: 2019-02-26 22:25:57.574000000 +0800
Change: 2019-03-14 18:49:48.066000000 +0800
Birth: –
12. exec: 执行命令
例子1:删除无属主的文件
[root@ken ~]# find / -nouser
find: ‘/proc/2395/task/2395/fd/6’: No such file or directory
find: ‘/proc/2395/task/2395/fdinfo/6’: No such file or directory
find: ‘/proc/2395/fd/5’: No such file or directory
find: ‘/proc/2395/fdinfo/5’: No such file or directory
/root/test
/var/spool/mail/kenken
/home/kenken
/home/kenken/.bash_logout
/home/kenken/.bash_profile
/home/kenken/.bashrc
[root@ken ~]# find / -nouser -exec rm -rf {} ;
find: missing argument to `-exec’
[root@ken ~]# find / -nouser -exec rm -rf {} \;
find: ‘/proc/2451/task/2451/fd/6’: No such file or directory
find: ‘/proc/2451/task/2451/fdinfo/6’: No such file or directory
find: ‘/proc/2451/fd/5’: No such file or directory
find: ‘/proc/2451/fdinfo/5’: No such file or directory
find: ‘/home/kenken’: No such file or directory
[root@ken ~]# find / -nouser
find: ‘/proc/2463/task/2463/fd/6’: No such file or directory
find: ‘/proc/2463/task/2463/fdinfo/6’: No such file or directory
find: ‘/proc/2463/fd/5’: No such file or directory
find: ‘/proc/2463/fdinfo/5’: No such file or directory
例子2:xargs
[root@ken ~]# touch {1..100}.txt
[root@ken ~]# powd
-bash: powd: command not found
[root@ken ~]# pwd
/root
[root@ken ~]# ls
100.txt 19.txt 28.txt 37.txt 46.txt 55.txt 64.txt 73.txt 82.txt 91.txt anaconda-ks.cfg
10.txt 1.txt 29.txt 38.txt 47.txt 56.txt 65.txt 74.txt 83.txt 92.txt a.out
11.txt 20.txt 2.txt 39.txt 48.txt 57.txt 66.txt 75.txt 84.txt 93.txt gitlab-ce-8.9.5-ce.0.el7.x86_64.rpm
12.txt 21.txt 30.txt 3.txt 49.txt 58.txt 67.txt 76.txt 85.txt 94.txt ken1
13.txt 22.txt 31.txt 40.txt 4.txt 59.txt 68.txt 77.txt 86.txt 95.txt
14.txt 23.txt 32.txt 41.txt 50.txt 5.txt 69.txt 78.txt 87.txt 96.txt
15.txt 24.txt 33.txt 42.txt 51.txt 60.txt 6.txt 79.txt 88.txt 97.txt
16.txt 25.txt 34.txt 43.txt 52.txt 61.txt 70.txt 7.txt 89.txt 98.txt
17.txt 26.txt 35.txt 44.txt 53.txt 62.txt 71.txt 80.txt 8.txt 99.txt
18.txt 27.txt 36.txt 45.txt 54.txt 63.txt 72.txt 81.txt 90.txt 9.txt
[root@ken ~]# find /root -name “*txt” | xargs rm -rf
[root@ken ~]# ls
anaconda-ks.cfg a.out gitlab-ce-8.9.5-ce.0.el7.x86_64.rpm ken1
13.find可以使用-a和-o或!
例子1:找到所有事普通文件并且权限是777
[root@ken ~]# find / -type f -a -perm 644 | head -3
/boot/grub2/device.map
/boot/grub2/i386-pc/gcry_rmd160.mod
/boot/grub2/i386-pc/acpi.mod
[root@ken ~]# ls -l /boot/grub2/device.map
-rw-r–r–. 1 root root 64 Feb 26 22:25 /boot/grub2/device.map