查找不排除文件的统计信息

时间:2013-05-21 14:57:47

标签: linux bash

在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"

2 个答案:

答案 0 :(得分:1)

原始问题 - 仅限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]')

如果文件列表很大,则更有可能遇到问题。您可以根据需要在任何-prune命令上恢复find选项。但是,运行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!运算符似乎完全搞砸了,我没想到。从表面上看,这看起来像一个错误,但我必须有更多的证据,并且在我声称'bug'之前必须对find规范进行大量非常仔细的审查。

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

(你在问题中使用术语'octet'是令人费解的;它通常用于表示网络通信中的一个字节,具有更严格的含义,它恰好是8位,即a字节不必是。权限以八进制表示,并且基于inode中的16位,2个八位字节。)

答案 1 :(得分:1)

使用-perm选项检查权限,并结合检查文件名。

find /example/dir -type f -not \( -name 'file1' -o -name 'file2' -o -name 'file3' -o -name 'file4' \) -perm 777

您不需要-prune。这用于防止下降直到某些子目录,它不对文件做任何事情。它适用于匹配规范的目录,因此在您的情况下将其与-not一起使用将与您想要的相反。