linux中文件匹配含义,Linux文件查找find命令用法

注意:选项在[ ]内的是可以省略的

很多实例都是根据当时环境而改变属性进行匹配的

所以可能会导致上下不同匹配规则会出现同个文件

一、含义

find是linux命令,可以将系统内符合expression的文件列出来。指定文件按名称、类别、时间、大小、权限等不同属性组合时,只有完全相符的才会被列出来。

相对于locate查找命令,find具有精确匹配、实时查找的优势,但是同时由于需要遍历指定路径下的文件,所以查找速度慢,比较耗费系统资源。

二、find命令

find [option] [查找路径] [查找条件] [处理动作]

查找路径:默认当前目录

查找条件:默认为查找指定路径下的所有文件

处理动作:默认为显示

查找条件:

-name "文件名称":根据文件名称进行查找,支持文件名通配(globbing)#查找/etc/下文件名为network的文件

[Linux]#find /etc/ -name "network"

/etc/sysconfig/network

/etc/rc.d/init.d/network

-iname "文件名称":查找时忽略大小写[Linux]#find /tmp/ -iname abc

/tmp/Abc

/tmp/abc

-u username:根据文件属主查找[Linux]#find /tmp/ -user hadoop

/tmp/abc.tar.xz

/tmp/etc

/tmp/etc/netconfig

[Linux]#

-g groupname:根据文件属组查找[Linux]#find /tmp/ -group hadoop

/tmp/Abc

/tmp/abc.tar.xz

/tmp/etc

/tmp/etc/netconfig

[Linux]#

-uid UID:根据文件UID查找[Linux]#find /tmp/ -uid 500

/tmp/1M.txt

/tmp/1.5M.txt

[Linux]#

-gid GID:根据文件GID查找[Linux]#find /tmp/ -gid 501

/tmp/2M.txt

/tmp/1.5M.txt

/tmp/2.1M.txt

[Linux]#

-nouser:查找没有属主的文件[Linux]#find /tmp/ -nouser

/tmp/myMBR

/tmp/3M.txt

/tmp/abc

/tmp/512K.txt

[Linux]#

-nogroup:查找没有属组的文件[Linux]#find /tmp/ -nogroup

/tmp/abc

/tmp/512K.txt

[Linux]#

对查找条件可以组合一起使用:

-a:与;同时满足#查找用户为root,且组为hadoop的文件

[Linux]#find /tmp/ -user root -a -group hadoop

/tmp/Abc

[Linux]#

-o:或;至少一个满足[Linux]#find /tmp/ -nouser -o -user stu7

/tmp/myMBR

/tmp/3M.txt

/tmp/abc

/tmp/512K.txt

[Linux]#

-not|!:非;取反[Linux]#find /tmp/ -not -user root

/tmp/myMBR

/tmp/1M.txt

/tmp/1.5M.txt

/tmp/abc.tar.xz

/tmp/3M.txt

/tmp/etc

/tmp/etc/netconfig

/tmp/abc

/tmp/512K.txt

[Linux]#

type:根据文件类型查找文件

f:普通文件

d:目录文件

b:块设备

c:字符设备

l:符号链接[Linux]#find /dev/ -type l

/dev/myvg/mylv

/dev/myvg/snap-mylv

/dev/dvd

/dev/cdrom

/dev/md/station73.magelinux.com:5

/dev/MAKEDEV

/dev/core

s:套接字[Linux]#find /dev/ -type s

/dev/log

[Linux]#

p:管道

-size:根据文件大小查找

-size [+|-]#Unit{K|M|G}

#Unit:精确匹配到#到#-1个单位内的文件[Linux]#find /tmp/ -size 2M

/tmp/2M.txt

/tmp/1.5M.txt

[Linux]#

+#Unit:匹配大于#个单位的文件[Linux]#find /tmp/ -size +2M

/tmp/3M.txt

/tmp/2.1M.txt

[Linux]#

-#Unit:匹配大小为#-1个单位之外的文件[Linux]#find /tmp/ -size -2M

/tmp/

/tmp/myMBR

/tmp/1M.txt

/tmp/scripttest

/tmp/rc.sysinit

根据时间戳查找:

以天为单位(time):支持[+|-]

-atime:访问时间

+#:表示(#+1)天之外访问过的文件[Linux]#find /tmp/ -atime +2

/tmp/myMBR

/tmp/rc.sysinit

/tmp/abc.tar.xz

/tmp/etc/netconfig

/tmp/script

[Linux]#

-mtime:修改时间

-#:表示#天之内被修改过的文件[Linux]#find /tmp/ -mtime -2

/tmp/

/tmp/2M.txt

/tmp/1M.txt

/tmp/1.5M.txt

/tmp/Abc

/tmp/3M.txt

/tmp/2.1M.txt

/tmp/network

/tmp/abc

/tmp/512K.txt

/tmp/NetworkManager

/tmp/.ICE-unix

[Linux]#

-ctime:改变时间

#:表示大于#+1> # >=#天内改变的文件[Linux]#find /tmp/ -ctime 1

/tmp/network

/tmp/NetworkManager

[Linux]#

以分钟为单位(min):支持[+|-]

用法与以天为单位是一样的;就不再举例说明

-amin [+|-]#

-mmin [+|-]#

-cmin [+|-]#

根据权限查找:

-perm [+|-]MODE

MODE:精确匹配#匹配权限精确为444的文件

[Linux]#find /tmp/ -perm 444

/tmp/1M.txt

/tmp/1.5M.txt

/tmp/3M.txt

[Linux]#

+MODE:任何一类用户的任何一位权限匹配;通常查找某类用户的某特定权限是否存在#查找其他用户是否有写权限,0代表不查找

[Linux]#find /tmp/ -perm +002

/tmp/

/tmp/Abc

/tmp/abc

/tmp/.ICE-unix

[Linux]#

-MODE:每类用户的指定要检查的权限位都匹配#查找每类用户都有写权限的

[Linux]#find /tmp/ -perm -222

/tmp/

/tmp/.ICE-unix

[Linux]#

处理动作:

-print:打印在标准输出上

-ls:以长格式输出匹配文件的信息[Linux]#find /tmp/ -perm -222 -ls

262145 4 drwxrwxrwt 6 root root 4096 Mar 1 14:23 /tmp/

262146 4 drwxrwxrwt 2 root root 4096 Mar 1 13:48 /tmp/.ICE-unix

[Linux]#

-exec COMMAND {} \;:对匹配到的文件执行指定的命令[Linux]#find /tmp/ -iname "*.txt" -exec mv {} {}x \;

[Linux]#find /tmp/ -iname "*.txtx"

/tmp/512K.txtx

/tmp/1M.txtx

/tmp/3M.txtx

/tmp/ccc.txtx

/tmp/1.5M.txtx

/tmp/2.1M.txtx

/tmp/2M.txtx

[Linux]#

-ok COMMAND {} \;:同于-exec,但是是交互式操作

find ...| xargs COMMAND[Linux]#find /tmp/ -iname "*.txtx" | xargs ls -lh

-r--r--r-- 1 centos gentoo 1.6M Mar 1 11:45 /tmp/1.5M.txtx

-r--r--r-- 1 centos root 1.0M Mar 1 11:43 /tmp/1M.txtx

-rw-r--r-- 1 root gentoo 2.1M Mar 1 11:44 /tmp/2.1M.txtx

-rw-r--r-- 1 root gentoo 2.0M Mar 1 11:43 /tmp/2M.txtx

-r--r--r-- 1 stu7 stu7 3.0M Mar 1 11:43 /tmp/3M.txtx

-rw-r--r-- 1 514 515 512K Mar 1 11:44 /tmp/512K.txtx

-rw-r--r-- 1 root root 0 Mar 1 14:23 /tmp/ccc.txtx

[Linux]#

三、具体实例

1、查找/var/目录属主为root且属组为mail的所有文件;[Linux]#find /var/ -user root -a -group mail

/var/spool/mail

[Linux]#

2、查找/usr目录下不属于root、bin或hadoop的所用文件;[Linux]#find /usr/ -not \( -user root -o -user bin -o -user hadoop \) |less

/usr/libexec/abrt-action-install-debuginfo-to-abrt-cache

/usr/local/apache/cgi-bin/test-cgi

/usr/local/apache/cgi-bin/printenv.wsf

/usr/local/apache/cgi-bin/printenv.vbs

/usr/local/apache/cgi-bin/printenv

3、查找/etc/目录下最近一周内其内容修改过的,且不属于root或hadoop的文件;[Linux]#find /etc/ -atime -7 -not \( -user root -o -user hadoop \)

/etc/keystone/policy.json

[Linux]#

4、查找当前系统上没有属主或属组,且最近1个月内曾被访问过的文件;[Linux]#find /tmp/ -atime -30 -a \( -nouser -o -nogroup \)

/tmp/abc

[Linux]#

5、查找/etc/目录下大于1M且类型为普通文件的所有文件;[Linux]#find /etc/ -size +1M -a -type f

/etc/selinux/targeted/modules/active/policy.kern

/etc/selinux/targeted/policy/policy.24

/etc/gconf/gconf.xml.defaults/%gconf-tree.xml

[Linux]#

6、查找/etc/目录所有用户都没有写权限的文件;[Linux]#find /etc/ -not -perm +222

/etc/pam.d/cups

/etc/openldap/certs/password

/etc/shadow

/etc/dbus-1/system.d/cups.conf

/etc/shadow-

/etc/ld.so.conf.d/kernel-2.6.32-358.el6.x86_64.conf

/etc/gshadow

/etc/rc.d/init.d/lvm2-monitor

/etc/rc.d/init.d/blk-availability

/etc/rc.d/init.d/lvm2-lvmetad

/etc/sudoers

[Linux]#

7、查找/etc/目录下至少有一类用户没有写权限;[Linux]#find /etc/ -not -perm -222 | less

/etc/pam.d

/etc/pam.d/system-auth-ac

/etc/pam.d/config-util

/etc/pam.d/runuser-l

/etc/pam.d/remote

/etc/pam.d/setup

/etc/pam.d/gdm-fingerprint

/etc/pam.d/system-config-users

/etc/pam.d/password-auth-ac

8、查找/etc/init.d/目录下,所有用户都有执行权限且其它用户有写权限的文件;[Linux]#find /etc/init.d/ -perm -113

[Linux]#

find相对较占用系统资源,所以建议不要经常使用,不过当别人不小心进了你的服务器,这时查找就会方便很多的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值