Ric*_*dle 185 unix linux shell
给定一个文件列表files.txt,我可以得到这样的大小列表:
cat files.txt | xargs ls -l | cut -c 23-30
Run Code Online (Sandbox Code Playgroud)
产生这样的东西:
151552
319488
1536000
225280
Run Code Online (Sandbox Code Playgroud)
如何获得所有这些数字的总和?
Tod*_*wen 369
... | paste -sd+ - | bc
Run Code Online (Sandbox Code Playgroud)
是我发现的最短的(来自UNIX命令行博客).
编辑:添加了-可移植性的参数,感谢@Dogbert和@Owen.
Gre*_*lds 147
开始
cat files.txt | xargs ls -l | cut -c 23-30 |
awk '{total = total + $1}END{print total}'
Run Code Online (Sandbox Code Playgroud)
如果文件名中有空格,cat将无法工作.这是一个perl单行代替.
perl -nle 'chomp; $x+=(stat($_))[7]; END{print $x}' files.txt
Run Code Online (Sandbox Code Playgroud)
您可以直接使用而不是使用cut从ls -l的输出中获取文件大小:
$ cat files.txt | xargs ls -l | awk '{total += $5} END {print "Total:", total, "bytes"}'
Run Code Online (Sandbox Code Playgroud)
Awk将"5美元"解释为第五列.这是来自ls -l的列,它为您提供文件大小.
python3 -c"import os; print(sum(os.path.getsize(f) for f in open('files.txt').read().split()))"
Run Code Online (Sandbox Code Playgroud)
如果您没有安装 bc,请尝试
echo $(( $(... | paste -sd+ -) ))
Run Code Online (Sandbox Code Playgroud)
代替
... | paste -sd+ - | bc
Run Code Online (Sandbox Code Playgroud)
$( ) <-- 返回执行命令的值
$(( 1+2 )) <-- 返回评估结果
echo <-- 将其回显到屏幕上
cat files.txt | awk '{ total += $1} END {print total}'
Run Code Online (Sandbox Code Playgroud)
您可以使用 awk 执行相同的操作,它甚至会跳过非整数
$ cat files.txt
1
2.3
3.4
ew
1
$ cat files.txt | awk '{ total += $1} END {print total}'
7.7
Run Code Online (Sandbox Code Playgroud)
或者您可以使用 ls 命令并计算人类可读的输出
$ ls -l | awk '{ sum += $5} END {hum[1024^3]="Gb"; hum[1024^2]="Mb"; hum[1024]="Kb"; for (x=1024^3; x>=1024; x/=1024) { if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x]; break; } } if (sum<1024) print "1kb"; }'
15.69 Mb
$ ls -l *.txt | awk '{ sum += $5} END {hum[1024^3]="Gb"; hum[1024^2]="Mb"; hum[1024]="Kb"; for (x=1024^3; x>=1024; x/=1024) { if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x]; break; } } if (sum<1024) print "1kb"; }'
2.10 Mb
Run Code Online (Sandbox Code Playgroud)
如果您只想在没有 awk 或其他解释器的情况下使用 shell 脚本,您可以使用以下脚本:
#!/bin/bash
total=0
for number in `cat files.txt | xargs ls -l | cut -c 23-30`; do
let total=$total+$number
done
echo $total
Run Code Online (Sandbox Code Playgroud)
小智 5
TMTWWTDI:Perl有一个文件大小运算符(-s)
perl -lne '$t+=-s;END{print $t}' files.txt
Run Code Online (Sandbox Code Playgroud)
小智 5
当你有stat时,整个 ls -l 然后 cut 是相当复杂的。它也容易受到ls -l的确切格式的影响(直到我更改了cut的列号才起作用)
<files.txt xargs stat -c %s | paste -sd+ - | bc
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
168750 次 |
| 最近记录: |