Linux Stat with find不排除文件
在bash脚本中,我尝试使用stat和find结合使用,根据文件的八位字节查找文件,但是我想跳过一些文件(file1、file2等)。然而,这似乎不起作用。这是为什么?我如何修复它?这是最好的方法吗Linux Stat with find不排除文件,linux,bash,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
$(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
如果您不想在输出中包括file1
、file2
、file3
或file4
,也可以使用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
或:
如果文件列表很大,则更可能出现问题。您可以根据需要在任何find
命令上恢复-prune
选项。但是,运行find-example/dir-typef
和find-example/dir-typef-prune
对我看到的结果没有影响
修订问题-777和775许可
如果您正在寻找777或775许可,则需要:
find /example/dir -type f -perm +775
这是因为777和775权限之间只有一点不同。更通用和可扩展的解决方案将使用-或操作:
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 \)
$
-不是或代码>操作符似乎把事情搞得一团糟,这是我没想到的。从表面上看,这看起来像一个bug,但在我声明“bug”之前,我必须有更多的证据,并且必须对find
规范进行非常仔细的审查
此测试是在Mac OS X 10.8.3(BSD)上使用find
进行的,没有GNUfind
(您在问题中使用的术语“八位字节”令人费解;它通常用于表示网络通信中的一个字节,更严格的含义是一个字节正好是8位,而一个字节不需要。权限以八进制表示,并且基于索引节点中的16位、2个八位字节。)使用-perm
选项检查权限,同时检查文件名
find /example/dir -type f -not \( -name 'file1' -o -name 'file2' -o -name 'file3' -o -name 'file4' \) -perm 777
您不需要-prune
。这是用来防止下降,直到某些子目录,它不做任何与文件。而且它适用于符合规范的目录,因此在您的情况下,将其与-而不是一起使用将与您想要的正好相反。您使用的是哪个平台?嘿-实质性地改变问题(通过使用egrep'777 | 775'
)和看不见的位置(在一条长得离奇的单行线的最右侧)是不公平的没有评论你这样做。公平竞争!谢谢,非常有帮助。我是说八进制而不是八进制。非接线员把我弄糊涂了;结果令人误解。你帮了我很多忙,给了我一个很长的回答:)
find /example/dir -type f -not \( -name 'file1' -o -name 'file2' -o -name 'file3' -o -name 'file4' \) -perm 777