path |
| ||||||||||||||||||||||||||||||||||||||
表示将结果输出到标准输出 | |||||||||||||||||||||||||||||||||||||||
exec | 对匹配的文件执行该参数所给出的shell命令 形式为command {} \;,注意{}与\;之间有空格 | ||||||||||||||||||||||||||||||||||||||
ok | 与exec作用相同, 区别在于,在执行命令之前,都会给出提示,让用户确认是否执行 | ||||||||||||||||||||||||||||||||||||||
options |
| ||||||||||||||||||||||||||||||||||||||
-o | 或者的意思 | ||||||||||||||||||||||||||||||||||||||
–a | 并且的意思(不然默认是-a) |
-ctime | 以天为单位通过change time(改变时间)查找文件 |
-atime | 以天为单位通过access time(访问时间)查找文件 |
-mmin | 以分钟为单位通过modify time(修改模式)查找文件 |
-amin | 以分钟为单位通过access time(访问时间)查找文件 |
-cmin | 以分钟单位通过change time(改变时间)查找文件 |
1、按名字查找
当前目录查找
[root@C7--01 ~]# find . -name '[0-9]*.txt' -print
./2passwd.txt
在/etc及子目录中,查找host开头的文件
[root@C7--01 ~]# find /etc -name 'host*' -print
/etc/selinux/targeted/active/modules/100/hostname
/etc/host.conf
/etc/hosts
/etc/hosts.allow
/etc/hosts.deny
/etc/hostname
在当前用户目录下查找所有文件
[root@C7--01 ~]# find ~ -name '*' -print
/root
/root/.bash_logout
/root/.bash_profile
/root/.bashrc
/root/.cshrc
/root/.tcshrc
/root/anaconda-ks.cfg
在当前目录及子目录中查找不是以a开头的文件
[root@C7--01 ~]# find . -name 'a*' -prune -o -name "*.txt" -print
./passwd.txt
./2passwd.txt
2、按目录查找
在当前目录除pass*开头之外的子目录搜索txt文件
[root@C7--01 ~]# find . -path "./pass*" -prune -o -name "*.txt" -print
./2passwd.txt
在当前目录及除了a*和.*之外的子目录中查找所有文件
[root@C7--01 ~]# find . \( -path './a*' -o -path './.*' \) -prune -o -name '*' -print
.
./yum.sh
./passwd.txt
./bbb.sh
./2passwd.txt
=--------或者除p* 和 2p*开头的文件查找所有文件
[root@C7--01 ~]# find . \( -path './p*' -o -path './2p*' \) -prune -o -name '*' -print
.
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./yum.sh
./.bash_history
./.aaa.sh.swp
./aaa.e
./.viminfo
./bbb.sh
./aaa
./a
./aaa.sh
3、按权限查找
在当前目录及子目录中,查找属主具有读写执行,其他具有读执行权限的文件
[root@C7--01 ~]# find . -perm 755 -print
./yum.sh
[root@C7--01 ~]# ls -l
-rwxr-xr-x. 1 root root 930 10月 25 23:39 yum.sh
查找用户有写权限或者组用户有写权限的文件或目录
[root@C7--01 ~]# find ./ -perm /220
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
....
..
4、按类型查找 (块设备、目录、字符设备、管道、符号链接、普通文件)
在当前目录及子目录下,查找符号链接文件
[root@C7--01 ~]# find / -type l -print
/dev/cdrom
/dev/snd/by-path/pci-0000:02:02.0
/dev/initctl
/dev/centos/home
/dev/centos/swap
/dev/centos/root
.....
...
5、按属主及属组
查找属主是root的文件
[root@C7--01 ~]# find . -user root -type f -print
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
查找属组 mysql 的文件
[root@C7--01 ~]# find / -group root -type f -print
/boot/grub2/device.map
/boot/grub2/i386-pc/gcry_rmd160.mod
/boot/grub2/i386-pc/acpi.mod
/boot/grub2/i386-pc/gcry_rsa.mod
/boot/grub2/i386-pc/adler32.mod
.......
...
6、按时间查找
查找2天内被更改过的文件
[root@C7--01 ~]# find . -mtime -2 -type f -print
./a.bak
./a
./aaa.e
./bbb.sh
查找2天前被更改过的文件
[root@C7--01 ~]# find . -mtime +2 -type f -print
./yum.sh
查找一天前被访问的文件
[root@C7--01 ~]# find . -atime +10 -type f -print
./yum.sh
查找一天前状态被改变的文件
[root@C7--01 ~]# find . -ctime +1 -type f -print
./anaconda-ks.cfg
./yum.sh
查找一天内状态被改变的文件
[root@C7--01 ~]# find . -ctime -1 -type f -print
./aaa.e
./bbb.sh
./2passwd.txt
./a.bak
./a
查找10分钟以前状态被改变的文件
[root@C7--01 ~]# find . -cmin +10 -type f -print
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
...
.
查找10分钟内状态被改变的文件
[root@C7--01 ~]# find . -cmin -10 -type f -print
./a
./.viminfo
7、按文件新旧
查找比 a 新的文件
[root@C7--01 ~]# find . -newer "a" -type f -print
./.viminfo
./a.bak
查找比 a 旧的文件
[root@C7--01 ~]# find . ! -newer "a" -type f -print
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./yum.sh
查找比 aaa.e 新,比 a 旧的文件
[root@C7--01 ~]# find . -newer 'aaa.e' ! -newer 'a' -type f -print
./.bash_history
./bbb.sh
./2passwd.txt
./a
8、按大小查找
查找超过1M的文件
[root@C7--01 ~]# find / -size +1M -type f -print
/boot/grub2/fonts/unicode.pf2
/boot/System.map-3.10.0-693.el7.x86_64
/boot/vmlinuz-3.10.0-693.el7.x86_64
........
......
...
查找等于6字节的文件
[root@C7--01 ~]# find / -size 6c -print
/boot/efi/EFI/centos
/dev/block/11:0
/dev/block/8:0
/dev/char/21:1
/dev/char/21:0
......
...
查找小于32k的文件
[root@C7--01 ~]# find . -size -32k -print
.
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
...
.
9、执行命令
查找 aaa 并删除,删除前提示确认
[root@C7--01 ~]# find . -name 'aaa.sh' -ok rm {} \;
< rm ... ./aaa.sh > ? yes
查找 a 并备份为a.bak
[root@C7--01 ~]# find . -name 'a' -exec cp {} {}.bak \;
[root@C7--01 ~]# ls
2passwd.txt a aaa.e a.bak anaconda-ks.cfg bbb.sh yum.sh
查当前目录下的所有普通文件
查当前目录下的所有普通文件,并在 - exec 选项中使用 ls -l 命令将它们列出
[root@C7--01 ~]# find . -type f -exec ls -l {} \;
-rw-r--r--. 1 root root 18 12月 29 2013 ./.bash_logout
-rw-r--r--. 1 root root 176 12月 29 2013 ./.bash_profile
-rw-r--r--. 1 root root 176 12月 29 2013 ./.bashrc
-rw-r--r--. 1 root root 100 12月 29 2013 ./.cshrc
-rw-r--r--. 1 root root 129 12月 29 2013 ./.tcshrc
-rw-------. 1 root root 1296 10月 25 23:11 ./anaconda-ks.cfg
-rwxr-xr-x. 1 root root 930 10月 25 23:39 ./yum.sh
-rw-------. 1 root root 2682 11月 14 21:40 ./.bash_history
-rw-r--r-- 1 root root 12288 11月 14 18:41 ./.aaa.sh.swp
-rw-r--r-- 1 root root 2 11月 14 18:52 ./aaa.e
-rw------- 1 root root 6072 11月 14 22:20 ./.viminfo
-rw-r--r-- 1 root root 395 11月 14 19:18 ./bbb.sh
-rw-r--r-- 1 root root 210 11月 14 19:18 ./2passwd.txt
-rw-r--r-- 1 root root 20 11月 14 21:55 ./aaa
-rw-r--r-- 1 root root 21 11月 14 21:58 ./a
-rw-r--r-- 1 root root 176 11月 14 22:20 ./aaa.sh
查询当天修改过的文件
[root@C7--01 ~]# find ./ -mtime -1 -type f -exec ls -l {} \;
-rw-r--r-- 1 root root 2 11月 14 18:52 ./aaa.e
-rw-r--r-- 1 root root 21 11月 14 21:58 ./a
-rw-r--r-- 1 root root 21 11月 15 01:06 ./a.bak
.......
....