[英]Stat with find not excluding files
Within a bash script, I am trying to find files based on their octet using stat in combination with find, however I want to skip some files (file1, file2, etc.). 在bash脚本中,我试图使用stat结合find查找基于其八位字节的文件,但是我想跳过一些文件(file1,file2等)。 However this doesn't appear to be working.
然而,这似乎不起作用。 Why is this and how I can fix it?
为什么这样以及我如何解决它? Is this the best way of doing this?
这是最好的方法吗?
$(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"
If you are looking for files with 777 permission, use find
to do that: 如果您要查找具有777权限的文件,请使用
find
来执行此操作:
find /example/dir -type f -perm 777
If you don't want to include file1
, file2
, file3
or file4
in the output, use grep
too: 如果您不想在输出中包含
file1
, file2
, file3
或file4
,请使用grep
:
find /example/dir -type f -perm 777 | grep -Ev 'file[1234]'
If you want the output from stat
for those files, then: 如果您想要这些文件的
stat
输出,那么:
find /example/dir -type f -perm 777 | grep -Ev 'file[1234]' | xargs stat --format %a
or: 要么:
stat --format %a $(find /example/dir -type f -perm 777 | grep -Ev 'file[1234]')
This is more likely to run into problems if the list of files is huge. 如果文件列表很大,则更有可能遇到问题。 You can reinstate the
-prune
option on any of the find
commands as you require. 您可以根据需要在任何
find
命令上恢复-prune
选项。 However, running find example/dir -type f
and find example/dir -type f -prune
made no difference to the result I saw. 但是,运行
find example/dir -type f
并find example/dir -type f -prune
对我看到的结果没有任何影响。
If you're looking for 777 or 775 permission, then you need: 如果您正在寻找777或775许可,那么您需要:
find /example/dir -type f -perm +775
This happens to work because there's only one bit different between the 777 and 775 permissions. 这恰好有效,因为777和775权限之间只有一点不同。 A more general and extensible solution would use
-or
operations: 更通用和可扩展的解决方案将使用
-or
操作:
find /example/dir -type f \( -perm 777 -or -perm 775 \)
With changes in the numbers, this could look for 664 or 646 permission without picking up executable files, which -perm +622
would pick up. 随着数字的变化,这可能会寻找664或646权限,而无需获取可执行文件,
-perm +622
会接收。
As to what is going wrong with the code in the question — I am not completely sure. 至于问题中的代码出了什么问题 - 我不完全确定。
$ 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 \)
$
The -not
or !
-not
或!
operator seems to completely mess things up, which I'd not expect. 操作员似乎完全搞砸了,我没想到。 Superficially, this looks like a bug, but I'd have to have a lot more evidence and have to do a lot of very careful scrutiny of the
find
specification before I claimed 'bug'. 从表面上看,这看起来像一个bug,但我必须有更多的证据,并且在我声称'bug'之前必须对
find
规范进行大量非常仔细的审查。
This testing was done with the find
on Mac OS X 10.8.3 (BSD), no GNU find
. 这个测试是在Mac OS X 10.8.3(BSD)上使用
find
完成的,没有find
GNU。
(Your use of the term 'octet' in the question is puzzling; it is normally used to indicate a byte in network communications, with the more stringent meaning that it is precisely 8 bits which a byte need not be. The permissions are presented in octal and are based on 16 bits, 2 octets, in the inode.) (你在问题中使用术语'八位字节'是令人费解的;它通常用于表示网络通信中的一个字节,具有更严格的含义,它恰好是一个字节不需要的8位。权限显示在八进制并且基于inode中的16位,2个八位字节。)
Use the -perm
option to check permissions, in combination with checking filenames. 使用
-perm
选项检查权限,并结合检查文件名。
find /example/dir -type f -not \( -name 'file1' -o -name 'file2' -o -name 'file3' -o -name 'file4' \) -perm 777
You don't need -prune
. 你不需要
-prune
。 This is used to prevent descending until certain subdirectories, it doesn't do anything with files. 这用于防止下降直到某些子目录,它不对文件做任何事情。 And it applies to directories that match the specification, so using it with
-not
in your case would be the opposite of what you wanted. 并且它适用于与规范匹配的目录,因此在您的情况下使用
-not
将与您想要的相反。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.