Linux系统之find日常使用大全

举报
江湖有缘 发表于 2023/01/16 13:35:16 2023/01/16
【摘要】 Linux系统之find日常使用大全

一、根据文件所在的目录层级进行查询

[root@192 ~]# find /etc -maxdepth 8 -mindepth 7 |head
/etc/selinux/targeted/active/modules/100/abrt/cil
/etc/selinux/targeted/active/modules/100/abrt/hll
/etc/selinux/targeted/active/modules/100/abrt/lang_ext
/etc/selinux/targeted/active/modules/100/accountsd/cil
/etc/selinux/targeted/active/modules/100/accountsd/hll
/etc/selinux/targeted/active/modules/100/accountsd/lang_ext
/etc/selinux/targeted/active/modules/100/acct/cil
/etc/selinux/targeted/active/modules/100/acct/hll
/etc/selinux/targeted/active/modules/100/acct/lang_ext
/etc/selinux/targeted/active/modules/100/afs/cil

二、根据文件的文件名来查询

1.使用-name查询

[root@192 ~]# find /etc -name *.conf |head
/etc/resolv.conf
/etc/fonts/conf.d/31-cantarell.conf
/etc/fonts/conf.d/66-sil-nuosu.conf
/etc/fonts/conf.d/59-liberation-sans.conf
/etc/fonts/conf.d/65-0-nhn-nanum-gothic.conf
/etc/fonts/conf.d/20-unhint-small-dejavu-sans.conf
/etc/fonts/conf.d/57-dejavu-sans.conf
/etc/fonts/conf.d/65-0-ttf-arphic-uming.conf
/etc/fonts/conf.d/10-hinting-slight.conf
/etc/fonts/conf.d/59-liberation-serif.conf

2.使用-iname查询

[root@192 ~]# find /etc -iname *.Conf |head
/etc/resolv.conf
/etc/fonts/conf.d/31-cantarell.conf
/etc/fonts/conf.d/66-sil-nuosu.conf
/etc/fonts/conf.d/59-liberation-sans.conf
/etc/fonts/conf.d/65-0-nhn-nanum-gothic.conf
/etc/fonts/conf.d/20-unhint-small-dejavu-sans.conf
/etc/fonts/conf.d/57-dejavu-sans.conf
/etc/fonts/conf.d/65-0-ttf-arphic-uming.conf
/etc/fonts/conf.d/10-hinting-slight.conf
/etc/fonts/conf.d/59-liberation-serif.conf

三、根据inode号查询

[root@192 ~]# find /etc -inum 17167100
/etc/passwd

四、根据文件的拥有人和拥有组查询

1.根据属主查询

[root@192 data]# find / -user admin
/home/admin
/home/admin/.mozilla
/home/admin/.mozilla/extensions
/home/admin/.mozilla/plugins
/home/admin/.bash_logout
/home/admin/.bash_profile
/home/admin/.bashrc
find:/proc/95731/task/95731/fd/5: No such file or directory
find:/proc/95731/task/95731/fdinfo/5: No such file or directory
find:/proc/95731/fd/6: No such file or directory
find:/proc/95731/fdinfo/6: No such file or directory
/var/spool/mail/admin


2.根据属组查询

[root@192 data]# find / -group admin 
/home/admin
/home/admin/.mozilla
/home/admin/.mozilla/extensions
/home/admin/.mozilla/plugins
/home/admin/.bash_logout
/home/admin/.bash_profile
/home/admin/.bashrc
find:/proc/95947/task/95947/fd/5: No such file or directory
find:/proc/95947/task/95947/fdinfo/5: No such file or directory
find:/proc/95947/fd/6: No such file or directory
find:/proc/95947/fdinfo/6: No such file or directory

3.根据uid查询

[root@192 data]# find / -uid 1000
/home/admin
/home/admin/.mozilla
/home/admin/.mozilla/extensions
/home/admin/.mozilla/plugins
/home/admin/.bash_logout
/home/admin/.bash_profile
/home/admin/.bashrc
find:/proc/96034/task/96034/fd/5: No such file or directory
find:/proc/96034/task/96034/fdinfo/5: No such file or directory
find:/proc/96034/fd/6: No such file or directory
find:/proc/96034/fdinfo/6: No such file or directory
/var/spool/mail/admin

4.根据gid查询

[root@192 data]# find / -gid 1000
/home/admin
/home/admin/.mozilla
/home/admin/.mozilla/extensions
/home/admin/.mozilla/plugins
/home/admin/.bash_logout
/home/admin/.bash_profile
/home/admin/.bashrc
find:/proc/96117/task/96117/fd/5: No such file or directory
find:/proc/96117/task/96117/fdinfo/5: No such file or directory
find:/proc/96117/fd/6: No such file or directory
find:/proc/96117/fdinfo/6: No such file or directory
[root@192 data]# 

5.查找没有属主和属组的文件

find /var -nouser
 find /var -nogroup 

五、组合查询

-a 逻辑与
-o 逻辑或
! 和 -not 非

find / -nouser -o -nogroup  -a -atime -7
查找当前系统_上没有属主或属组,且最近一个周内曾被访问过的文件


六、根据文件类型查找

f 普通文件
d 目录
b 块设备
c 字符文件
l 链接文件
p 管道文件
s 套接字文件

[root@192 data]# find /etc -type f |head
/etc/fstab
/etc/crypttab
/etc/resolv.conf
/etc/fonts/conf.d/25-no-bitmap-fedora.conf
/etc/fonts/conf.d/README
/etc/fonts/fonts.conf
/etc/grub.d/00_header
/etc/grub.d/01_users
/etc/grub.d/10_linux
/etc/grub.d/20_linux_xen

七、根据文件大小查询

[root@192 data]# find /etc -size +3M -a -type f
/etc/udev/hwdb.bin
/etc/selinux/targeted/policy/policy.31
/etc/selinux/targeted/active/policy.kern
/etc/selinux/targeted/active/policy.linked

八、根据文件权限查询

1.查询权限为777的文件

[root@192 data]# find /etc -perm 777  -ls|head
16797764    0 lrwxrwxrwx   1 root     root           17 Feb  2 02:53 /etc/mtab -> /proc/self/mounts
17327003    0 lrwxrwxrwx   1 root     root           50 Jan 31 08:11 /etc/fonts/conf.d/31-cantarell.conf -> /usr/share/fontconfig/conf.avail/31-cantarell.conf
18202953    0 lrwxrwxrwx   1 root     root           50 Jan 31 08:12 /etc/fonts/conf.d/66-sil-nuosu.conf -> /usr/share/fontconfig/conf.avail/66-sil-nuosu.conf
17327007    0 lrwxrwxrwx   1 root     root           56 Jan 31 08:11 /etc/fonts/conf.d/59-liberation-sans.conf -> /usr/share/fontconfig/conf.avail/59-liberation-sans.conf
18202946    0 lrwxrwxrwx   1 root     root           59 Jan 31 08:12 /etc/fonts/conf.d/65-0-nhn-nanum-gothic.conf -> /usr/share/fontconfig/conf.avail/65-0-nhn-nanum-gothic.conf
17326542    0 lrwxrwxrwx   1 root     root           65 Jan 31 08:11 /etc/fonts/conf.d/20-unhint-small-dejavu-sans.conf -> /usr/share/fontconfig/conf.avail/20-unhint-small-dejavu-sans.conf
17326544    0 lrwxrwxrwx   1 root     root           52 Jan 31 08:11 /etc/fonts/conf.d/57-dejavu-sans.conf -> /usr/share/fontconfig/conf.avail/57-dejavu-sans.conf
18202958    0 lrwxrwxrwx   1 root     root           59 Jan 31 08:12 /etc/fonts/conf.d/65-0-ttf-arphic-uming.conf -> /usr/share/fontconfig/conf.avail/65-0-ttf-arphic-uming.conf
17326549    0 lrwxrwxrwx   1 root     root           55 Jan 31 08:11 /etc/fonts/conf.d/10-hinting-slight.conf -> /usr/share/fontconfig/conf.avail/10-hinting-slight.conf
18240100    0 lrwxrwxrwx   1 root     root           57 Jan 31 08:12 /etc/fonts/conf.d/59-liberation-serif.conf -> /usr/share/fontconfig/conf.avail/59-liberation-serif.conf

2.查找/etc目录下至少有1类用户没有执行权限的文件

[root@192 data]# find /etc  ! -perm    111 |head
/etc
/etc/fstab
/etc/crypttab
/etc/mtab
/etc/resolv.conf
/etc/fonts
/etc/fonts/conf.d
/etc/fonts/conf.d/31-cantarell.conf
/etc/fonts/conf.d/66-sil-nuosu.conf
/etc/fonts/conf.d/59-liberation-sans.conf

九、根据文件的时间戳查询

1.atime、mtime、ctime

-atime   以天为单位
-atime -77天之内,包括倒数第七天
-atime  +77天以前 ,包含倒数第八天

2.amin、mmin、cmin

-amin  以分钟为单位
-amin -77分钟之内
-amin  +77分钟以前

3.使用atime查询

[root@192 data]# find /data -atime -7
/data
/data/log

十、使用exec查询

1.将以.conf文件结尾的文件改后缀名

find /tmp  -name "*.conf" -exec cp -r {} /data/{}.org \;

2.删除/tmp下存在7天以上并且属主为admin的文件

find /tmp  -ctime +7 -user admin -exec rm -rf {} \;
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。