有 ls -l 数字格式吗?

j0h*_*j0h 7 permissions command-line bash ls

考虑这个 ls -l 命令的输出:

$ ls -l /usr/bin | tail
-rwxr-xr-x 1 root root     105696 Oct 25  2014 zenity
-rwxr-xr-x 1 root root     188296 Oct 21  2013 zip
-rwxr-xr-x 1 root root      86096 Oct 21  2013 zipcloak
-rwxr-xr-x 1 root root      48459 Mar  3  2015 zipdetails
-rwxr-xr-x 1 root root       2953 Oct 29 10:45 zipgrep
-rwxr-xr-x 2 root root     166584 Oct 29 10:45 zipinfo
Run Code Online (Sandbox Code Playgroud)

权限以漂亮的、人类可读的字符列出。有没有办法ls 输出与这些权限等效的数字?

Ser*_*nyy 10

ls does not have such flag, but stat command allows showing octal permissions. Consider sample output for a test file,

testdir:$ stat TESTER
  File: ‘TESTER’
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d  Inode: 1197        Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/ xieerqi)   Gid: ( 1000/ xieerqi)
Access: 2015-11-05 17:42:01.914917433 -0700
Modify: 2015-11-05 17:42:01.914917433 -0700
Change: 2015-11-05 18:41:04.463776180 -0700
 Birth: -
Run Code Online (Sandbox Code Playgroud)

stat also has --format flag , which allows you to "simulate" ls -l :

testdir:$ stat --format="%a %h %U %G %s %y %n" * | head -n 3                   
664 1 xieerqi xieerqi 0 2015-11-05 17:42:01.918917452 -0700 Aaaaaaa.bbb - 0000003 tag tag_tag 9tag
664 1 xieerqi xieerqi 0 2015-11-05 17:42:01.922917471 -0700 Aaaaaaa.bbb - 0000004 tag tag_tag 9tag
664 1 xieerqi xieerqi 0 2015-11-05 17:42:01.930917509 -0700 Aaaaaaa.bbb - 0000005 tag tag_tag 9tag
Run Code Online (Sandbox Code Playgroud)

The only limitation here is the time format cannot be altered and color output cannot be added .

You could always alias that command in .bashrc to some shorter command, e.g. alias lsl2='stat --format="%a %h %U %G %s %y %n" *'

Alternatively, I've put together a small function that uses find and awk for nicer formatting

 function lsl2
   {
     find . -maxdepth 1 | sort |  xargs -I{} stat --format="%a %h %U %G %s %y %n" {}  | awk '{$7=substr($7,1,5);$8    =" ";print}'
  }
Run Code Online (Sandbox Code Playgroud)

Sample output

testdir:$ lsl2 | head -n 7
775 3 xieerqi xieerqi 4096 2015-11-05 22:20   .
664 1 xieerqi xieerqi 0 2015-11-05 17:42   ./Aaaaaaa.bbb - 0000003 tag tag_tag 9tag
664 1 xieerqi xieerqi 0 2015-11-05 17:42   ./Aaaaaaa.bbb - 0000004 tag tag_tag 9tag
664 1 xieerqi xieerqi 0 2015-11-05 17:42   ./Aaaaaaa.bbb - 0000005 tag tag_tag 9tag
664 1 xieerqi xieerqi 0 2015-11-05 17:42   ./Aaaaaaa.bbb - 0000006 tag 9tag
664 1 xieerqi xieerqi 0 2015-11-05 17:42   ./Aaaaaaa.bbb - 0000006 tag tag_tag 9tag
664 1 xieerqi xieerqi 0 2015-11-05 17:42   ./Aaaaaaa.bbb - 0000007 tag 9tag

testdir:$ type lsl2
lsl2 is a function
Run Code Online (Sandbox Code Playgroud)

  • 呃.. `find` 可以打印权限,以及大多数这些值(如果不是全部的话):`find 。-printf "%m %n %u %g %s %t %f\n" | 列-t` (2认同)