如何测试权限为 000 的文件是否存在?

How can I test existence of a file with permissions 000?

出于某种我不知道的原因,我有一个权限设置为“000”的文件。

我想 chmod u+r 它,因为我需要压缩它的整个文件夹。对于 000,zip 会警告读取此文件时出现问题。

我想测试这个文件是否存在,因为不是每次我都把它放到文件夹中。

但是我验证了使用下面的结果是布尔值 false

if [ -f $file_path ]; then 
...

还有 -e 开关 return false。

如何测试 bash 脚本中的文件是否存在?

照你说的做:

$> touch none_foo
$> chmod 000 none_foo
$> if [ -f "none_foo" ]; then printf "yes\n"; else printf "no\n"; fi
yes

但是,如果封闭文件夹中有 000,那么你就 注定要失败 ;-)

示例(从上述状态继续):

$> mkdir disclosed
$> mv none_foo disclosed/
# still ok:
$> if [ -f "disclosed/none_foo" ]; then printf "yes\n"; else printf "no\n"; fi
yes
# now a non-disclosure:
$> chmod 000 disclosed
$> if [ -f "disclosed/none_foo" ]; then printf "yes\n"; else printf "no\n"; fi
no

因此,对于禁止 "entering" 的文件路径中的文件夹,您必须先更正它。 在任何情况下(正如另一位评论者已经建议的那样):在 shell ...

中对文字和变量使用引号

正如@JeremyJStarcher 在他的回答中发布的那样,您还可以使用:

$> find ./ -perm 000
.//disclosed
find: .//disclosed: Permission denied

但如您所见,文件夹陷阱将由具有足够权限的用户将 000 增加为更易于访问的内容来缓解...

您可以使用以下语句在目录中搜索具有 000 权限的文件:

find /tmp  -perm 000

其中 /tmp 是您的目录。