如何找到大于/小于 x 字节的文件?

cei*_*cat 282 unix find

在终端中,如何找到大于或小于 x 字节的文件?

我想我可以做类似的事情

find . -exec ls -l {} \;
Run Code Online (Sandbox Code Playgroud)

然后将结果awk通过管道传输到按文件大小过滤。但不应该有比这更简单的方法吗?

Joh*_*n T 437

用:

find . -type f -size +4096c
Run Code Online (Sandbox Code Playgroud)

查找大于 4096 字节的文件。

和 :

find . -type f -size -4096c
Run Code Online (Sandbox Code Playgroud)

查找小于 4096 字节的文件。

注意大小切换后的 + 和 - 差异。

-size开关解释说:

-size n[cwbkMG]

    File uses n units of space. The following suffixes can be used:

    `b'    for 512-byte blocks (this is the default if no suffix  is
                                used)

    `c'    for bytes

    `w'    for two-byte words

    `k'    for Kilobytes       (units of 1024 bytes)

    `M'    for Megabytes    (units of 1048576 bytes)

    `G'    for Gigabytes (units of 1073741824 bytes)

    The size does not count indirect blocks, but it does count
    blocks in sparse files that are not actually allocated. Bear in
    mind that the `%k' and `%b' format specifiers of -printf handle
    sparse files differently. The `b' suffix always denotes
    512-byte blocks and never 1 Kilobyte blocks, which is different
    to the behaviour of -ls.
Run Code Online (Sandbox Code Playgroud)

  • @Jay:来自“测试”部分开头的 [man find](http://linux.die.net/man/1/find):“对于大于 n,可以将数字参数指定为 +n,- n 表示小于 n,n 表示正好 n。” (11认同)
  • 我刚刚发现 BSD 手册页确实描述了 +/- 的事情。它在“Primaries”部分的末尾。- 所有采用数字参数的原色都允许数字前面带有加号 (“+”) 或减号 (“-”)。前面的加号表示“大于 n”,前面的减号表示“小于 n”,两者都不表示“正好 n” (8认同)
  • 手册页在顶部提到了它,并描述了 + 和 - 可以应用于所有采用数字 ('n') 参数的开关,包括 + 和 - 的含义。(在手册页中搜索 TESTS 以找到描述这一点的部分的开头) (4认同)

Tol*_*ani 14

我认为find在没有管道到 AWK 的情况下单独可能有用。例如,

find ~ -type f -size +2k  -exec ls -sh {} \;
Run Code Online (Sandbox Code Playgroud)

波浪号表示您希望从何处开始搜索,结果应仅显示大于 2 KB 的文件。

为了让它看起来更漂亮,您可以使用该-exec选项来执行另一个命令,该命令将列出这些目录及其大小。

有关更多信息,请阅读 的手册页find


MaQ*_*eod 6

对于这种事情,AWK 真的很容易。这里有一些与文件大小检查相关的事情,你可以用它来做,就像你问的那样:

列出大于 200 字节的文件:

ls -l | awk '{if ($5 > 200) print $8}'
Run Code Online (Sandbox Code Playgroud)

列出小于 200 字节的文件并将列表写入文件:

ls -l | awk '{if ($5 < 200) print $8}' | tee -a filelog
Run Code Online (Sandbox Code Playgroud)

列出 0 字节的文件,将列表记录到文件中并删除空文件:

ls -l | awk '{if ($5 == 0) print $8}' | tee -a deletelog | xargs rm
Run Code Online (Sandbox Code Playgroud)

  • [解析`ls`不好](http://unix.stackexchange.com/q/128985/44425) (3认同)

Jay*_*Jay 5

大于 2000 字节:

du -a . | awk '$1*512 > 2000 {print $2}'
Run Code Online (Sandbox Code Playgroud)

小于 2000 字节:

du -a . | awk '$1*512 < 2000 {print $2} '
Run Code Online (Sandbox Code Playgroud)