如何使用 ls 仅列出非空文件?

Dav*_*d B 43 linux bash shell command-line ls

如何ls使用 linux列出(使用)所有非空文件(大小 > 0)?

Dae*_*yth 60

find dirname -not -empty -ls假设 GNU 找到,我会使用。

  • 如果您使用“find . -not -empty -ls”,它还将包含当前目录(即“.”在它的输出中),仅包含当前文件使用“find . -type f -not -empty -ls” (2认同)

Nif*_*fle 20

这对于find ls 来说是不够强大的。

find -maxdepth 1 -size +0 -print
Run Code Online (Sandbox Code Playgroud)

-maxdepth 1 - 这告诉 find 仅搜索当前目录,删除以查看所有子目录或更改数字以下降 2、3 或更多级别。

-size +0这告诉 find 查找大小大于0字节的文件。0可以更改为您想要的任何尺寸。

-print 告诉 find 打印出它找到的文件的完整路径

编辑:
后期添加:您可能还应该添加-type f上面的开关。这告诉 find 只查找文件。正如下面的评论中所指出的,-print实际上并不需要切换。


MaQ*_*eod 12

ls -l | awk '{if ($5 != 0) print $9}'
Run Code Online (Sandbox Code Playgroud)

如果您打算使用ls,则需要来自awk.


Jor*_*ril 9

find dirname -type f ! -empty


Gil*_*il' 8

ls 几乎没有过滤文件的选项:这不是它的工作。过滤文件是 shell 在简单情况下的工作(通过 globbing),而在复杂情况下则是 find 的工作。

在 zsh 中,您可以使用Lglobbing 限定符仅保留大小大于 0 的文件(.限定符限制为常规文件):

ls *(.L+0)
Run Code Online (Sandbox Code Playgroud)

其他 shell 的用户必须使用 find。使用 GNU find(主要在 Linux 上找到):

find -maxdepth 1 -type f ! -empty -exec ls {} +
Run Code Online (Sandbox Code Playgroud)

符合 POSIX 的方式是:

find . -type f -size +0c -exec ls {} + -o -name . -o -prune
Run Code Online (Sandbox Code Playgroud)

如果ls不只是一个例子,而您只是想进行目视检查,您可以按大小排序:ls -S