查找组权限等于用户权限的文件

查找组权限等于用户权限的文件

是否可以做类似的事情find -perm g=u?我说“类似”是因为-perm mode需要模式来指定所有位,而不仅仅是g,并且因为我无法将u其放在右侧=,就像我可以使用以下chmod命令一样:

you can specify exactly one of the letters ugo: the permissions granted
to  the  user  who  owns the file (u), the permissions granted to other
users who are members of the file's  group  (g),  and  the  permissions
granted  to  users  that are in neither of the two preceding categories
(o).

目前,我所做的find | xargs -d \\n ls -lartd | egrep '^.(...)\1事情实在太丑陋了。

谢谢。

答案1

这好多了(产生换行符分隔的输出):

find . -type f -printf '%04m%p\n' | perl -ne 'print substr($_, 4) if /^.(.)\1/'

或者如果您的任何文件名可能包含换行符(产生空字节分隔的输出):

find . -type f -printf '%04m%p\0' | perl -n0e 'print substr($_, 4) if /^.(.)\1/'

本质上,find 命令产生如下输出:

0644./.config/banshee-1/banshee.db
0664./.config/gedit/gedit-print-settings
0664./.config/gedit/gedit-page-setup
0644./.config/gedit/accels

然后,perl 命令会过滤此输出,并在打印任何匹配的行之前去除文件的模式:

./.config/gedit/gedit-print-settings
./.config/gedit/gedit-page-setup

如果您想要包含目录、FIFO、套接字和设备节点而不仅仅是文件,请省略-type f。 然后所有符号链接都将出现在列表中(因为它们始终具有模式 0777),因此您可能需要使用 排除它们! -type l或在它们后面加上-L

答案2

试试这个:(必须编辑,忽略了处理带有空格的文件名)

find . -type d -or -type f -ls | \
 awk '{ perm=$3; fname=11; if (substr(perm,1,3) == substr(perm,4,3)) { printf "%s",perm; while (fname<=NF) printf " %s",$(fname++); printf "\n"; } }'

为了便于阅读,在管道处分割行,但功能上只有一行

使用 find 仅显示文件和目录,不包括符号链接(始终具有 lrwxrwxrwx 权限)、套接字和其他非想要的内容。

awk 脚本解析查找“ls”的输出,比较用户和组权限对应的子字符串,如果相等,则打印权限和文件名。

当然,在您测试满意之后,您可以删除打印语句的“perm”部分,只输出文件名。

相关内容