如何将 ls 的输出限制为仅显示文件名、日期和大小?

Pin*_*kie 28 linux unix

如何ls在 linux 中使用仅获取文件名日期和大小的列表?我不需要查看其他信息,例如所有者、权限。

Jin*_*Jin 29

ls -l | awk '{print $5, $6, $7, $9}'

这将以字节、月份、日期和文件名打印文件大小。

jin@encrypt /tmp/foo % ls -l
total 0
drwxr-xr-x  2 jin  wheel  68 Oct  4 12:43 bar
drwxr-xr-x  2 jin  wheel  68 Oct  4 12:43 baz
drwxr-xr-x  2 jin  wheel  68 Oct  4 12:43 quux

jin@encrypt /tmp/foo % ls -l | awk '{print $5, $6, $7, $9}'
68 Oct 4 bar
68 Oct 4 baz
68 Oct 4 quux
Run Code Online (Sandbox Code Playgroud)

  • 不支持带有多个空格的文件名 (6认同)

thi*_*ton 14

从技术上讲,这是不可能的ls,但find可以用它的-printf开关做同样的工作:

find -maxdepth 1 -printf '%t %s %p\n'
Run Code Online (Sandbox Code Playgroud)


小智 5

你总是可以这样做:

$ ls -l
total 0
-rw-r--r--  1 user  staff  0 Oct  6 23:29 file1
-rw-r--r--  1 user  staff  0 Oct  6 23:29 file2
-rw-r--r--  1 user  staff  0 Oct  6 23:30 file3
-rw-r--r--  1 user  staff  0 Oct  6 23:30 file4
-rw-r--r--  1 user  staff  0 Oct  6 23:30 file5
-rw-r--r--  1 user  staff  0 Oct  6 23:30 file6
-rw-r--r--  1 user  staff  0 Oct  6 23:30 file7
Run Code Online (Sandbox Code Playgroud)

cut 它:

$ ls -l | cut -f 8-13 -d ' '

0 Oct  6 23:29 file1
0 Oct  6 23:29 file2
0 Oct  6 23:30 file3
0 Oct  6 23:30 file4
0 Oct  6 23:30 file5
0 Oct  6 23:30 file6
0 Oct  6 23:30 file7

$ 
Run Code Online (Sandbox Code Playgroud)

  • 不适用于:可变所有权、组、文件大小 (4认同)

End*_*ife 5

另一种非ls方式:

> stat --printf='%y\t%12s\t%-16n|\n' tmp.*
2017-06-15 10:42:07.252853000 +0200         10485760    tmp.1           |
2017-06-15 10:41:25.659570000 +0200              666    tmp.TKPzm3BfRw  |
Run Code Online (Sandbox Code Playgroud)

说明:%y= 人类可读的修改日期; %s= 字节大小(%12s右对齐,长度 12); %n= 文件名(%-16n左对齐,长度 16);\t= 制表符,\n= 换行。|= 字面管道字符,只是为了显示文件名的结尾。

就像lsstat没有选择要显示哪些文件的选项。(例如,这可以通过如上所示的 shell globbing 或 some 来完成find ... -print0 | xargs -r0 stat ...。)