Ed *_*yer 41 shell scripting grep filesize find
例如,我有一个比我预期的更快填充的大型文件系统.所以我寻找正在添加的内容:
find /rapidly_shrinking_drive/ -type f -mtime -1 -ls | less
Run Code Online (Sandbox Code Playgroud)
而且我发现很多东西.成千上万的六七种文件.我可以挑出一个类型并计算它们:
find /rapidly_shrinking_drive/ -name "*offender1*" -mtime -1 -ls | wc -l
Run Code Online (Sandbox Code Playgroud)
但我真正喜欢的是能够获得这些文件的磁盘总大小:
find /rapidly_shrinking_drive/ -name "*offender1*" -mtime -1 | howmuchspace
Run Code Online (Sandbox Code Playgroud)
如果有人有,我会对Perl单行开放,但是我不打算使用任何涉及多行脚本或File :: Find的解决方案.
Ste*_*202 65
该命令du告诉您磁盘使用情况.特定案例的用法示例:
find rapidly_shrinking_drive/ -name "offender1" -mtime -1 -print0 | du --files0-from=- -hc | tail -n1
Run Code Online (Sandbox Code Playgroud)
(以前我写过du -hs,但在我的机器上似乎忽略了find输入,而是总结了cwd的大小.)
pix*_*x0r 14
Darn,Stephan202是对的.我没有考虑du -s(总结),所以我使用了awk:
find rapidly_shrinking_drive/ -name "offender1" -mtime -1 | du | awk '{total+=$1} END{print total}'
Run Code Online (Sandbox Code Playgroud)
我更喜欢其他答案,而且几乎肯定更有效率.
用GNU查找,
find /path -name "offender" -printf "%s\n" | awk '{t+=$1}END{print t}'
Run Code Online (Sandbox Code Playgroud)
最近我遇到了同样的(几乎)问题,我想出了这个解决方案。
\nfind $path -type f -printf '%s '\nRun Code Online (Sandbox Code Playgroud)\n它将显示文件大小(以字节为单位),来自man find:
-printf format\n True; print format on the standard output, interpreting `\\' escapes and `%' directives. Field widths and precisions can be spec\xe2\x80\x90\n ified as with the `printf' C function. Please note that many of the fields are printed as %s rather than %d, and this may mean\n that flags don't work as you might expect. This also means that the `-' flag does work (it forces fields to be left-aligned).\n Unlike -print, -printf does not add a newline at the end of the string.\n ...\n %s File's size in bytes.\n ...\nRun Code Online (Sandbox Code Playgroud)\n为了获得总数,我使用了这个:
\necho $[ $(find $path -type f -printf %s+)0] #b\necho $[($(find $path -type f -printf %s+)0)/1024] #Kb\necho $[($(find $path -type f -printf %s+)0)/1024/1024] #Mb\necho $[($(find $path -type f -printf %s+)0)/1024/1024/1024] #Gb\nRun Code Online (Sandbox Code Playgroud)\n
我想将上面杰森的评论提升为答案的状态,因为我相信它是最容易记忆的(虽然不是最通用的,如果你真的需要指定的文件列表find):
$ du -hs *.nc
6.1M foo.nc
280K foo_region_N2O.nc
8.0K foo_region_PS.nc
844K foo_region_xyz.nc
844K foo_region_z.nc
37M ETOPO1_Ice_g_gmt4.grd_region_zS.nc
$ du -ch *.nc | tail -n 1
45M total
$ du -cb *.nc | tail -n 1
47033368 total
Run Code Online (Sandbox Code Playgroud)
由于OP明确表示:
如果有人有的话,我愿意使用 Perl 单行代码,但我不会使用任何涉及多行脚本或 File::Find 的解决方案。
...还没有,这是 perl 一行:
find . -name "*offender1*" | perl -lne '$total += -s $_; END { print $total }'
Run Code Online (Sandbox Code Playgroud)