我如何计算具有特定八进制代码但不显示在 shell 中的文件数

How can i count the number of files with a specific octal code without them showing in shell

我试过使用 tree 命令,但我不知道怎么做。(我想使用 tree 是因为我不想显示文件,只显示数字) 假设 c 是权限代码 例如我想知道有多少文件有权限 751

使用带有-perm标志的find,它只匹配具有指定权限位的文件。

例如,如果您在 $c 中有八进制,则 运行

find . -perm $c

通常的 find 选项适用——如果您只想查找当前级别的文件而不递归到目录,运行

find . -maxdepth 1 -perm $c

要找到匹配文件的数量,让find为每个文件打印一个点,然后使用wc计算点数。 (wc -l 将无法使用带有换行符的更奇特的文件名,正如@BenjaminW 在评论中指出的那样。使用 wc -c 的想法来源是 this answer。)

find . -maxdepth 1 -perm $c -printf '.' | wc -c

这将显示文件数量而不显示文件本身。

如果您使用 zsh 作为 shell,则无需任何外部程序即可在本地完成:

setopt EXTENDED_GLOB # Just in case it's not already set
c=0751
files=( **/*(#qf$c) )
echo "${#files[@]} files found"

将计算当前工作目录和子目录中具有这些权限的所有文件(并为您提供一个数组中的所有名称,以防您稍后要对它们执行某些操作)。在 the documentation.

中阅读有关 zsh glob 限定符的更多信息