find命令

path
要查找的目录路径
~表示$HOME目录(当前用户目录)
.表示当前目录
/表示根目录
print表示将结果输出到标准输出
exec

对匹配的文件执行该参数所给出的shell命令

形式为command {} \;,注意{}与\;之间有空格 

ok与exec作用相同,
区别在于,在执行命令之前,都会给出提示,让用户确认是否执行
options
表示查找方式
-name   xxx(文件名)查找名为xxx的文件
-newer查找新的文件或者目录(如果加!就是旧的文件或者目录)
-perm按执行权限来查找
-user    username按文件属主来查找
-group groupname按组来查找
-mtime   -n +n按文件更改时间来查找文件,-n指n天以内,+n指n天以前
-atime    -n +n按文件访问时间来查找文件,-n指n天以内,+n指n天以前
-ctime    -n +n 按文件创建时间来查找文件,-n指n天以内,+n指n天以前
-nogroup 查无有效属组的文件,即文件的属组在/etc/groups中不存在
-nouser 查无有效属主的文件,即文件的属主在/etc/passwd中不存
-type    b/d/c/p/l/f 查是块设备、目录、字符设备、管道、符号链接、普通文件
-size      n[c]查长度为n块[或n字节]的文件
-mount查文件时不跨越文件系统mount点
-follow 如果遇到符号链接文件,就跟踪链接所指的文件
-prune忽略某个目录
+n表示大于n
-n表示小于n
n表示等于n
-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
.......
....
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乘浪初心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值