Linux “find“ 命令查找特定权限的文件(-perm参数)

本文详细解析了Linux find命令中关于-perm选项的使用,包括精确匹配、任何权限位匹配、所有权限位匹配等模式,并通过实例展示了如何查找不同权限设置的文件。此外,还介绍了如何查找具有SUID和SGID标志的文件。
摘要由CSDN通过智能技术生成

最近遇到了有 "find . -perm"的需求,搜了一些文章发现讲的都不是很清晰,甚至有的还有错误。所以还是回到Linux官方手册(https://linux.die.net/man/1/find),把find命令的相关部分截取出来便于一次学对。
(每段话附加了一些个人理解,如有错误,欢迎指正。)

Options

-perm mode

File’s permission bits are exactly mode (octal or symbolic). Since an exact match is required, if you want to use this form for symbolic modes, you may have to specify a rather complex mode string. For example -perm g=w will only match files which have mode 0020 (that is, ones for which group write permission is the only permission set). It is more likely that you will want to use the ‘/’ or ‘-’ forms, for example -perm -g=w, which matches any file with group write permission. See the EXAMPLES section for some illustrative examples.

(查找文件权限“准确”匹配。例如 -perm g=w 或 -perm 0020,只会检索出权限准确为0020的文件,其他权限不等于0020的文件都不会检索出来。而例如 -perm -g=w 会检索出所有group bit “包含”写权限的文件,其他权限不管)

-perm -mode

All of the permission bits mode are set for the file. Symbolic modes are accepted in this form, and this is usually the way in which would want to use them. You must specify ‘u’, ‘g’ or ‘o’ if you use a symbolic mode. See the EXAMPLES section for some illustrative examples.

(查找“所有”满足权限要求的文件,非严格匹配,而是不论其他未要求的权限位是如何设置,都会被检索出来。是常用的查找模式,具体详见EXAMPLES)

-perm /mode

Any of the permission bits mode are set for the file. Symbolic modes are accepted in this form. You must specify ‘u’, ‘g’ or ‘o’ if you use a symbolic mode. See the EXAMPLES section for some illustrative examples. If no permission bits in mode are set, this test matches any file (the idea here is to be consistent with the behavior of -perm -000).

(查找满足“任何”一个权限位的文件。如果mode中某个权限位没有限定,则认为该权限位满足条件,即 -perm -000等价于所有文件)

-perm +mode

Deprecated, old way of searching for files with any of the permission bits in mode set. You should use -perm /mode instead. Trying to use the ‘+’ syntax with symbolic modes will yield surprising results. For example, ‘+u+x’ is a valid symbolic mode (equivalent to +u,+x, i.e. 0111) and will therefore not be evaluated as -perm +mode but instead as the exact mode specifier -perm mode and so it matches files with exact permissions 0111 instead of files with any execute bit set. If you found this paragraph confusing, you’re not alone - just use -perm /mode. This form of the -perm test is deprecated because the POSIX specification requires the interpretation of a leading ‘+’ as being part of a symbolic mode, and so we switched to using ‘/’ instead.

(已弃用)

Example

find . -perm 664

Search for files which have read and write permission for their owner, and group, but which other users can read but not write to. Files which meet these criteria but have other permissions bits set (for example if someone can execute the file) will not be matched.

(查找owner和group有读写权限,且其他users有只读权限的文件,权限非严格664的文件不会被检索到,可以理解为权限是 rw-rw-r–的文件

find . -perm -664

Search for files which have read and write permission for their owner and group, and which other users can read, without regard to the presence of any extra permission bits (for example the executable bit). This will match a file which has mode 0777, for example.

(查找权限“子集”为664的权限,即满足owner和group有读写权限,且其他users有只读权限,而不管除此以外其他权限位,比如执行权限,是否有设置。因此查找结果中也会包括权限为777的文件。可以理解为权限是rw*rw*r**的文件,*代表任意,可以是x也可以是-

find . -perm /222

Search for files which are writable by somebody (their owner, or their group, or anybody else).

(查找可以被任何人可写的文件,任何人可以是owner,或者可以是group,或者可以是others,满足一组有w权限即可

find . -perm /220
find . -perm /u+w,g+w
find . -perm /u=w,g=w

All three of these commands do the same thing, but the first one uses the octal representation of the file mode, and the other two use the symbolic form. These commands all search for files which are writable by either their owner or their group. The files don’t have to be writable by both the owner and group to be matched; either will do.

(以上三个命令效果相同,都是查找可以被owner或者group写入的文件,owner和group的写权限满足一个即可查到,不需要同时满足)

find . -perm -220
find . -perm -g+w,u+w

Both these commands do the same thing; search for files which are writable by both their owner and their group.

(以上两个命令效果相同,都是查找同时可以被owner和group写入的文件)

find . -perm -444 -perm /222 ! -perm /111
find . -perm -a+r -perm /a+w ! -perm /a+x

These two commands both search for files that are readable for everybody ( -perm -444 or -perm -a+r), have at least one write bit set ( -perm /222 or -perm /a+w) but are not executable for anybody ( ! -perm /111 and ! -perm /a+x respectively).

(以上两个命令效果相同,都是查找可以被任何人读取,且至少可以被人(owner或group或others)写入,但任何人都不可以执行的文件…)

补充内容

看到一个手册里没有的内容,就是关于SUID和SGID的查找,关于SUID和SGID的概念一两句话说不清,直接上命令。

查找当前路径下全部有SUID的文件(u+s)

find . -perm /4000
find . -perm /u=s

查找当前路径下全部有SGID的文件(g+s)

find . -perm /2000
find . -perm /g=s

查找当前路径下同时有SUID和SGID的文件

find . -perm /6000
find . -perm /u=s,g=s
linux find -perm命令用于按照权限搜索文件。这个命令能够在特定的目录树下查找文件,并可以按照各种条件进行搜索,不仅包括权限,还包括时间、文件类型、文件大小等等。下面我们来详细解析一下这个命令。 1. 语法 find [path] -perm [mode] [options] 其中[path]代表搜索的目标路径,-perm表示按照权限搜索,[mode]为权限值,[options]为其他选项。 2. 权限权限值可以用数字表示,也可以用字符表示。数字表示中,每一个数字代表着不同的文件权限。1代表执行权限,2代表写权限,4代表读权限。将这三个数字相加,就可以得到该文件的所有权限。例如,755表示文件所有者拥有读、写、执行权限,而其他用户只拥有读、执行权限。 字符表示中,r代表读权限,w代表写权限,x代表执行权限。在符号表示中,rwx三个字母代表了文件所有者、所属组和其他用户的三种不同的权限。用+号表示添加权限,用-号表示取消权限。 3. 示例 (1)查找当前目录下所有用户都可执行的.sh文件。 find . -type f -perm /a=x (2)查找文件所属用户和组都是root,并且其他用户没有任何权限文件。 find . -type f -user root -group root -perm 0600 4. 注意事项 由于不小心匹配了系统文件或者权限不正确导致文件被损坏,linux find -perm命令尤其需要小心谨慎地使用。在操作系统方面,权限可能是很重要的因素,一条错误的命令就有可能可能导致系统性能下降、安全漏洞,甚至导致文件系统崩溃。 5. 总结 Linux find -perm命令提供了一种很方便、很灵活的搜索文件的方式。使用合适的选项和参数,我们可以根据自己的需求来搜索特定类型、特定时间、特定大小、特定权限文件。不过在使用的时候,我们也要注意安全性的问题,小心谨慎地使用这个命令
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值