在终端中,如何找到大于或小于 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)
Tol*_*ani 14
我认为find
在没有管道到 AWK 的情况下单独可能有用。例如,
find ~ -type f -size +2k -exec ls -sh {} \;
Run Code Online (Sandbox Code Playgroud)
波浪号表示您希望从何处开始搜索,结果应仅显示大于 2 KB 的文件。
为了让它看起来更漂亮,您可以使用该-exec
选项来执行另一个命令,该命令将列出这些目录及其大小。
有关更多信息,请阅读 的手册页find
。
对于这种事情,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)
大于 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)
归档时间: |
|
查看次数: |
313384 次 |
最近记录: |