gpt4 book ai didi

linux - Stat with find 不排除文件

转载 作者:太空宇宙 更新时间:2023-11-04 09:45:33 25 4
gpt4 key购买 nike

在 bash 脚本中,我尝试结合使用 stat 和 find 根据文件的八位字节来查找文件,但是我想跳过一些文件(file1、file2 等)。但是,这似乎不起作用。为什么会这样,我该如何解决?这是最好的方法吗?

$(stat --format %a 2>&1 $(find /example/dir -type f -not \( -name 'file1' -o \
-name 'file2' -o -name 'file3' -o -name 'file4' \) -prune) | egrep "777|755"

最佳答案

原始问题——仅 777 权限

如果您要查找具有 777 权限的文件,请使用 find 来执行此操作:

find /example/dir -type f -perm 777

如果您不想在输出中包含 file1file2file3file4,也使用 grep:

find /example/dir -type f -perm 777 | grep -Ev 'file[1234]'

如果您想要这些文件的 stat 输出,那么:

find /example/dir -type f -perm 777 | grep -Ev 'file[1234]' | xargs stat --format %a

或:

stat --format %a $(find /example/dir -type f -perm 777 | grep -Ev 'file[1234]')

如果文件列表很大,这更有可能遇到问题。您可以根据需要在任何 find 命令上恢复 -prune 选项。但是,运行 find example/dir -type ffind example/dir -type f -prune 对我看到的结果没有影响。

修改后的问题——777 和 775 权限

如果您正在寻找 777 或 775 许可,那么您需要:

find /example/dir -type f -perm +775

这恰好有效,因为 777 和 775 权限之间只有一点点不同。更通用和可扩展的解决方案将使用 -or 操作:

find /example/dir -type f \( -perm 777 -or -perm 775 \)

随着数字的变化,这可以寻找 664 或 646 权限而不获取可执行文件,-perm +622 将获取。

问题代码中的问题

至于问题中的代码出了什么问题——我不完全确定。

$ find example/dir -type f
example/dir/a/filea
example/dir/a/fileb
example/dir/b/filea
example/dir/b/fileb
example/dir/c/filea
example/dir/c/fileb
example/dir/filea
example/dir/fileb
$ find example/dir -type f -not \( -name filea -o -name fileb \)
$ find example/dir -type f -not \( -name filea -or -name fileb \)
$ find example/dir -type f \( -name filea -or -name fileb \)
example/dir/a/filea
example/dir/a/fileb
example/dir/b/filea
example/dir/b/fileb
example/dir/c/filea
example/dir/c/fileb
example/dir/filea
example/dir/fileb
$ find example/dir -type f ! \( -name filea -or -name fileb \)
$ find example/dir -type f \( -not -name filea -and -not -name fileb \)
$

-not! 运算符似乎完全把事情搞砸了,这是我没有预料到的。从表面上看,这看起来像是一个错误,但在我声称“错误”之前,我必须有更多的证据并且必须对 find 规范进行大量非常仔细的审查。

此测试是在 Mac OS X 10.8.3 (BSD) 上使用 find 完成的,没有 GNU find

(您在问题中使用术语'octet' 令人费解;它通常用于指示网络通信中的一个字节,更严格的意思是它恰好是 8 位字节不需要。权限以八进制表示,并基于 inode 中的 16 位,2 个八位字节。)

关于linux - Stat with find 不排除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16673158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com