使用 stat 获取八进制文件模式(BSD 与 GNU)
我试图获取八进制的文件位,以评估路径是常规文件还是目录。
这在 BSD
stat
中效果很好,它是我的 macOS
的一部分:
$ stat -f %Op /tmp/test.txt
100644
如何在 中获得相同(或相似)的结果来自
。我得到的最好的结果是:GNU
的 stat
$ stat -c "%a" /tmp/test.txt
644
$ stat -c "%A" /tmp/test.txt
-rw-r--r--
现在,我看到的唯一方法是解析字符串结果并检查第一个字符是 -
还是 d
。
I am trying to get the file bits in octal, to assess whether a path is a regular file or a directory.
This works pretty well in BSD
stat
which is part of my macOS
:
$ stat -f %Op /tmp/test.txt
100644
How can I get the same (or similar) result in stat
from GNU
. The best I got is:
$ stat -c "%a" /tmp/test.txt
644
$ stat -c "%A" /tmp/test.txt
-rw-r--r--
Right now, the only way I am seeing is parsing the string result and check whether first character is -
or is d
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GNU
stat
为此提供了%f
格式代码。不幸的是,它以十六进制显示原始模式,因此您必须使用 shell 的工具来转换它。另请注意,格式是用
-c
指定的。有关更多详细信息,请参阅手册页。
GNU
stat
has the%f
format code for this. Unfortunately, it displays the raw mode in hex, so you have to use e.g. the shell's facilities to convert it.Notice also that the format is specified with
-c
.For more details, refer to the man page.