使用 stat 获取八进制文件模式(BSD 与 GNU)

发布于 2025-01-16 19:18:22 字数 496 浏览 3 评论 0原文

我试图获取八进制的文件位,以评估路径是常规文件还是目录。

这在 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对你而言 2025-01-23 19:18:22

GNU stat 为此提供了 %f 格式代码。不幸的是,它以十六进制显示原始模式,因此您必须使用 shell 的工具来转换它。

另请注意,格式是用 -c 指定的。

printf '%06o\n' 0x$(stat -c '%f' /tmp/test.txt)

有关更多详细信息,请参阅手册页

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.

printf '%06o\n' 0x$(stat -c '%f' /tmp/test.txt)

For more details, refer to the man page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文