鉴于files.txt
中的文件列表,我可以得到这样的大小列表:
cat files.txt | xargs ls -l | cut -c 23-30
产生类似这样的东西:
151552
319488
1536000
225280
如何获得所有这些数字的总数?
答案 0 :(得分:360)
答案 1 :(得分:147)
这是
cat files.txt | xargs ls -l | cut -c 23-30 |
awk '{total = total + $1}END{print total}'
答案 2 :(得分:9)
cat将无效。这是一个perl单行代替。
perl -nle 'chomp; $x+=(stat($_))[7]; END{print $x}' files.txt
答案 3 :(得分:8)
而不是使用 cut 从 ls -l </ em>的输出中获取文件大小,您可以直接使用:
$ cat files.txt | xargs ls -l | awk '{total += $5} END {print "Total:", total, "bytes"}'
Awk将“$ 5”解释为第五列。这是来自 ls -l </ em>的列,它为您提供文件大小。
答案 4 :(得分:7)
python3 -c"import os; print(sum(os.path.getsize(f) for f in open('files.txt').read().split()))"
或者,如果您只想将数字相加,请输入:
python3 -c"import sys; print(sum(int(x) for x in sys.stdin))"
答案 5 :(得分:5)
TMTWWTDI:Perl有一个文件大小运算符(-s)
perl -lne '$t+=-s;END{print $t}' files.txt
答案 6 :(得分:4)
如果您没有安装bc,请尝试
echo $(( $(... | paste -sd+ -) ))
而不是
... | paste -sd+ - | bc
$( )
&lt; - 返回执行命令的值
$(( 1+2 ))
&lt; - 返回评估结果
echo
&lt; - 将其回显到屏幕
答案 7 :(得分:3)
在ksh:
echo " 0 $(ls -l $(<files.txt) | awk '{print $5}' | tr '\n' '+') 0" | bc
答案 8 :(得分:3)
如果您只想在没有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
答案 9 :(得分:3)
当你有 stat 时,整个ls -l然后切割是相当复杂的。它也容易受到 ls -l </ strong>的确切格式的影响(在我更改 cut 的列数之前,它没有工作)
另外,修正了useless use of cat。
<files.txt xargs stat -c %s | paste -sd+ - | bc
答案 10 :(得分:3)
我会改用“du”。
$ cat files.txt | xargs du -c | tail -1
4480 total
如果您只想要号码:
cat files.txt | xargs du -c | tail -1 | awk '{print $1}'
答案 11 :(得分:1)
这是我的
cat files.txt | xargs ls -l | cut -c 23-30 | sed -e :a -e '$!N;s/\n/+/;ta' | bc
答案 12 :(得分:1)
管道gawk:
cat files.txt | xargs ls -l | cut -c 23-30 | gawk 'BEGIN { sum = 0 } // { sum = sum + $0 } END { print sum }'
答案 13 :(得分:1)
我喜欢用......
echo "
1
2
3 " | sed -e 's,$, + p,g' | dc
他们将显示每一行的总和......
申请这种情况:
ls -ld $(< file.txt) | awk '{print $5}' | sed -e 's,$, + p,g' | dc
总计是最后一个值......
答案 14 :(得分:1)
#
# @(#) addup.sh 1.0 90/07/19
#
# Copyright (C) <heh> SjB, 1990
# Adds up a column (default=last) of numbers in a file.
# 95/05/16 updated to allow (999) negative style numbers.
case $1 in
-[0-9])
COLUMN=`echo $1 | tr -d -`
shift
;;
*)
COLUMN="NF"
;;
esac
echo "Adding up column .. $COLUMN .. of file(s) .. $*"
nawk ' OFMT="%.2f" # 1 "%12.2f"
{ x = '$COLUMN' # 2
neg = index($x, "$") # 3
if (neg > 0) X = gsub("\\$", "", $x)
neg = index($x, ",") # 4
if (neg > 1) X = gsub(",", "", $x)
neg = index($x, "(") # 8 neg (123 & change
if (neg > 0) X = gsub("\\(", "", $x)
if (neg > 0) $x = (-1 * $x) # it to "-123.00"
neg = index($x, "-") # 5
if (neg > 1) $x = (-1 * $x) # 6
t += $x # 7
print "x is <<<", $x+0, ">>> running balance:", t
} ' $*
# 1. set numeric format to eliminate rounding errors
# 1.1 had to reset numeric format from 12.2f to .2f 95/05/16
# when a computed number is assigned to a variable ( $x = (-1 * $x) )
# it causes $x to use the OFMT so -1.23 = "________-1.23" vs "-1.23"
# and that causes my #5 (negative check) to not work correctly because
# the index returns a number >1 and to the neg neg than becomes a positive
# this only occurs if the number happened to b a "(" neg number
# 2. find the field we want to add up (comes from the shell or defaults
# to the last field "NF") in the file
# 3. check for a dollar sign ($) in the number - if there get rid of it
# so we may add it correctly - $12 $1$2 $1$2$ $$1$$2$$ all = 12
# 4. check for a comma (,) in the number - if there get rid of it so we
# may add it correctly - 1,2 12, 1,,2 1,,2,, all = 12 (,12=0)
# 5. check for negative numbers
# 6. if x is a negative number in the form 999- "make" it a recognized
# number like -999 - if x is a negative number like -999 already
# the test fails (y is not >1) and this "true" negative is not made
# positive
# 7. accumulate the total
# 8. if x is a negative number in the form (999) "make it a recognized
# number like -999
# * Note that a (-9) (neg neg number) returns a postive
# * Mite not work rite with all forms of all numbers using $-,+. etc. *
答案 15 :(得分:0)
在我看来,最简单的解决方案是&#34; expr&#34; unix命令:
s=0;
for i in `cat files.txt | xargs ls -l | cut -c 23-30`
do
s=`expr $s + $i`
done
echo $s
答案 16 :(得分:0)
Pure bash
total=0; for i in $(cat files.txt | xargs ls -l | cut -c 23-30); do
total=$(( $total + $i )); done; echo $total
答案 17 :(得分:0)
sizes=( $(cat files.txt | xargs ls -l | cut -c 23-30) )
total=$(( $(IFS="+"; echo "${sizes[*]}") ))
或者您可以在阅读尺寸时将它们相加
declare -i total=0
while read x; total+=x; done < <( cat files.txt | xargs ls -l | cut -c 23-30 )
如果你不关心咬合大小和块是可以的,那么只需
declare -i total=0
while read s junk; total+=s; done < <( cat files.txt | xargs ls -s )
答案 18 :(得分:0)
cat files.txt | awk '{ total += $1} END {print total}'
您甚至可以跳过非整数使用awk进行操作
$ cat files.txt
1
2.3
3.4
ew
1
$ cat files.txt | awk '{ total += $1} END {print total}'
7.7
或者您可以使用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
答案 19 :(得分:0)
如果您有R,则可以使用:
> ... | Rscript -e 'print(sum(scan("stdin")));'
Read 4 items
[1] 2232320
由于我对R感到满意,所以实际上我对这样的东西有几个别名,因此我可以在bash
中使用它们而不必记住该语法。例如:
alias Rsum=$'Rscript -e \'print(sum(scan("stdin")));\''
让我做
> ... | Rsum
Read 4 items
[1] 2232320
灵感:Is there a way to get the min, max, median, and average of a list of numbers in a single command?
答案 20 :(得分:0)
当管道的开头可以产生 0 行时,最流行的答案不起作用,因为它最终只输出 0 行。您可以通过始终添加 0 来获得正确的行为:
... | (cat && echo 0) | paste -sd+ - | bc
答案 21 :(得分:0)
粘贴不需要 -。只要 files.txt 包含一个或多个有效文件名,就会执行以下操作:
<files.txt xargs stat -c %s | paste -sd+ | bc
cat 不需要在没有文件的情况下插入 0。没有管道,也许在脚本中更方便,您可以使用:
(xargs -a files.txt stat -c %s || echo 0) | paste -sd+ | bc