将 `ls -l` 输出中的权限转换为八进制

use*_*406 6 bash permissions

我想将 的输出转换ls为八进制权限位。

我想到了实现该练习的最短和最清晰的方法:假设我们有输入:

total 1
drwxr----x 1 user2 workers 1024 May 26 22:22 dir
-rwx-wxrw- 2 user2 workers 1024 May 26 22:22 file.txt
Run Code Online (Sandbox Code Playgroud)

我们的输出应该是:

741 dir
736 file.txt
Run Code Online (Sandbox Code Playgroud)

dev*_*ull 8

你可以使用 GNU find

find . -type f -printf "%m\t%f\n"
Run Code Online (Sandbox Code Playgroud)

为了获得文件的完整路径,请使用指令p而不是f

find . -type f -printf "%m\t%p\n"
Run Code Online (Sandbox Code Playgroud)

要将结果限制在当前目录中,请指定-maxdepth

find . -maxdepth 1 -type f -printf "%m\t%f\n"
Run Code Online (Sandbox Code Playgroud)

如果您想要文件和目录的结果,请删除-type谓词:

find . -printf "%m\t%p\n"
Run Code Online (Sandbox Code Playgroud)


Jos*_* R. 7

你不想用ls这个。在 GNU 系统上,您可以使用:

stat -c'%a %n' *
Run Code Online (Sandbox Code Playgroud)