find命令详解

今天,来分享一下find命令的使用方法。
find: 与其他的查找命令相比,如locate,find的查找是实时的,精确的查找命令,但其缺点是查找速度慢。
find使用方法基本可以总结为:

find 查找路径 查找标准 处理动作

查找路径:你想要查找的文件路径,默认为当前路径
查找标准:查找的条件,默认为查找路径下的所有文件
处理动作:查找到文件后要做的动作,默认为显示

查找标准:

  • -name FILE :根据文件名FILE查找
    • *:代表任意长度字符
    • ?:任意单个字符
    • [ ]:括号里面的字符中的任意一个
  • iname:不区分大小写
  • regex PATTERN:基于正则表达式PATTERN进行查找
  • user:根据文件属主查找
  • group:根据文件属组查找
  • uid:根据文件属主的uid查找
  • gid:根据文件属组的gid查找
  • nouser:查找没有属主的文件
  • nogroup:查找没有属组的文件
  • type:根据文件类型查找
    • f:普通文件
    • d:目录
    • c:字符文件
    • l:链接文件
    • s:套接字文件
    • p:管道文件
    • b:块设备文件
  • -size:根据文件大小查找
    • [+|-] # k:[大于|小于]#KB的文件
    • [+|-] # M:[大于|小于]#KB的文件
    • [+|-] # G:[大于|小于]#KB的文件
  • -mtine [+|-] #:#天[外|内]内容被修改的文件
  • -ctime [+|-] #:#天[外|内]被更改的文件
  • -atime [+|-] # :#天[外|内]被访问的文件
  • -mmin [+|-] #:#分钟[外|内]内容被修改的文件
  • -cmin [+|-] #:#分钟[外|内]被更改的文件
  • -amin [+|-] # :#分钟[外|内]被访问的文件
    注:关于ctime与mtime的区别,戳这里:ctime与mtime区别
  • perm -MODE:按MODE权限查找,每一位都必须包含
  • perm /MODE :按MODE权限查找,任何一位包含都可以
  • perm MODE :按MODE权限查找,必须文件匹配,每一位都必须与MODE相同

处理动作:

  • -print:显示
  • -ls :显示详细信息
  • -ok COMMAND {} \; :操作需用户确认
  • -exec COMMAND {} \;:操作而不需用户确认

组合条件:

  • -a : 与
  • -o:或
  • -not:非

注:默认为与操作

举例:

1.找出/etc/下,所有名为passwd的文件和以ifcfg开头的文件:

[root@DesktopComputer /tmp/test 13:31:31 ]$ find /etc/ -name passwd
/etc/passwd
/etc/pam.d/passwd
[root@DesktopComputer /tmp/test 13:31:50 ]$find /etc/ -name ifcfg-*
/etc/sysconfig/network-scripts/ifcfg-lo
/etc/sysconfig/network-scripts/ifcfg-Profile_1
/etc/sysconfig/network-scripts/ifcfg-enp3s0
[root@DesktopComputer /tmp/test 13:34:48 ]$ 

2.找出/bin/下,以z结尾的文件

[root@DesktopComputer /tmp/test 13:50:08 ]$ find /bin/ -regex '.*z$' 
/bin/unxz
/bin/xz
/bin/lz
/bin/dwz
/bin/tgz
/bin/uz
/bin/rz
/bin/sz
[root@DesktopComputer /tmp/test 13:50:45 ]$ 

3.找出系统中,没有属主或属组的文件

[root@DesktopComputer /tmp/test 13:53:45 ]$ find / -nouser -o -nogroup
[root@DesktopComputer /tmp/test 13:55:38 ]$ 

注:等待时间较长,如果发现找到,请删掉这些无用文件

[root@DesktopComputer /tmp/test 13:53:45 ]$ find / -nouser -o -nogroup -exec rm {} \;
[root@DesktopComputer /tmp/test 13:55:38 ]$ 

4.查找/etc/下有读写权限的链接文件

[root@DesktopComputer /tmp/test 14:17:14 ]$ find /etc/ -type l -a -perm -666
/etc/mtab
/etc/fonts/conf.d/65-0-lohit-telugu.conf
/etc/fonts/conf.d/59-liberation-sans.conf
/etc/fonts/conf.d/65-wqy-microhei.conf
/etc/fonts/conf.d/59-liberation-mono.conf
/etc/fonts/conf.d/65-0-lohit-marathi.conf
...

注:所有的链接文件都为777,可以通过一下方式验证:

[root@DesktopComputer /tmp/test 14:17:24 ]$ find /etc/ -type l -ls
134320324    0 lrwxrwxrwx   1 root     root           17 Oct 17 18:54 /etc/mtab -> /proc/self/mounts
2835461    0 lrwxrwxrwx   1 root     root           55 Oct 17 19:15 /etc/fonts/conf.d/65-0-lohit-telugu.conf -> /usr/share/fontconfig/conf.avail/65-0-lohit-telugu.conf
495591    0 lrwxrwxrwx   1 root     root           56 Oct 17 18:56 /etc/fonts/conf.d/59-liberation-sans.conf -> /usr/share/fontconfig/conf.avail/59-liberation-sans.conf
2835462    0 lrwxrwxrwx   1 root     root           53 Oct 17 19:15 /etc/fonts/conf.d/65-wqy-microhei.conf -> /usr/share/fontconfig/conf.avail/65-wqy-microhei.conf
547486    0 lrwxrwxrwx   1 root     root           56 Oct 17 18:57 /etc/fonts/conf.d/59-liberation-mono.conf -> /usr/share/fontconfig/conf.avail/59-liberation-mono.conf
2835467    0 lrwxrwxrwx   1 root     root           56 Oct 17 19:15 /etc/fonts/conf.d/65-0-lohit-marathi.conf -> /usr/share/fontconfig/conf.avail/65-0-lohit-marathi.conf
553660    0 lrwxrwxrwx   1 root     root           59 Dec 19 07:07 /etc/fonts/conf.d/10-scale-bitmap-fonts.conf -> /usr/share/fontconfig/conf.avail/
...

5.查找家目录下,一天之内修改的文件

[root@DesktopComputer /tmp/test 14:34:32 ]$ find /root/ -ctime -1
/root/
/root/.cache/abrt
/root/.cache/abrt/lastnotification
/root/.config/gconf
/root/.xauthqWsH0f
[root@DesktopComputer /tmp/test 14:34:37 ]$ 

6.查找家目录下,权限为777的文件

[zhoupan@DesktopComputer ~ 14:38:00 ]$ find ~ -perm 777
/home/zhoupan/.config/google-chrome/SingletonLock
/home/zhoupan/.config/google-chrome/SingletonSocket
/home/zhoupan/.config/google-chrome/SingletonCookie
/home/zhoupan/.config/menus/gnome-applications-merged
...

7.查找/etc/下名为passwd的文件,并显示其内容

[root@DesktopComputer /tmp/test 15:51:32 ]$ find /etc/ -name passwd -exec cat {} \;
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt

8.找到文件并删除,删的时候,需要用户确认是否需要删除

[zhoupan@DesktopComputer /tmp/test 16:27:25 ]$ ls
11.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt
[zhoupan@DesktopComputer /tmp/test 16:27:26 ]$ find -name '1*' -ok rm {} \;
< rm ... ./1.txt > ? n
< rm ... ./11.txt > ? y
[zhoupan@DesktopComputer /tmp/test 16:27:52 ]$ ls
1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt
[zhoupan@DesktopComputer /tmp/test 16:27:53 ]$ 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值