Angesichts einer Liste von Dateien in files.txt
kann ich eine Liste ihrer Größen wie folgt erhalten:
cat files.txt | xargs ls -l | cut -c 23-30
was produziert so etwas:
151552
319488
1536000
225280
Wie bekomme ich die Summe aller Zahlen?
... | paste -sd+ - | bc
ist der kürzeste, den ich gefunden habe (vom UNIX Command Line blog).
Edit: fügte das -
-Argument für die Portabilität hinzu, danke @Dogbert und @Owen.
Hier geht
cat files.txt | xargs ls -l | cut -c 23-30 |
awk '{total = total + $1}END{print total}'
cat funktioniert nicht, wenn Dateinamen Leerzeichen enthalten. Hier ist stattdessen ein Perl-Einzeiler.
Perl -nle 'chomp; $x+=(stat($_))[7]; END{print $x}' files.txt
Anstatt cut zu verwenden, um die Dateigröße aus der Ausgabe von ls -l zu erhalten, können Sie direkt Folgendes verwenden:
$ cat files.txt | xargs ls -l | awk '{total += $5} END {print "Total:", total, "bytes"}'
Awk interpretiert "5 $" als fünfte Spalte. Dies ist die Spalte aus ls -l , die die Dateigröße angibt.
cat files.txt | xargs ls -l | cut -c 23-30 | python -c'import sys; print sum(int(x) for x in sys.stdin)'
TMTWWTDI : Perl hat einen Operator für die Dateigröße (-s)
Perl -lne '$t+=-s;END{print $t}' files.txt
Das ganze ls -l und dann der Schnitt ist ziemlich gewunden, wenn Sie stat haben. Es ist auch anfällig für das genaue Format von ls -l (es funktionierte nicht, bis ich die Spaltennummern für cut geändert habe).
Auch der nutzlose Einsatz von cat wurde behoben.
<files.txt xargs stat -c %s | paste -sd+ - | bc
wenn Sie bc nicht installiert haben, versuchen Sie es
echo $(( $(... | paste -sd+ -) ))
anstatt
... | paste -sd+ - | bc
$( )
<- gibt den Wert der Befehlsausführung zurück
$(( 1+2 ))
<- gibt die ausgewerteten Ergebnisse zurück
echo
<- Echo auf dem Bildschirm
In ksh:
echo " 0 $(ls -l $(<files.txt) | awk '{print $5}' | tr '\n' '+') 0" | bc
Ich würde stattdessen "du" verwenden.
$ cat files.txt | xargs du -c | tail -1
4480 total
Wenn Sie nur die Nummer wünschen:
cat files.txt | xargs du -c | tail -1 | awk '{print $1}'
Sie können das folgende Skript verwenden, wenn Sie Shell-Scripting ohne awk oder andere Interpreter verwenden möchten:
#!/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
Hier ist meins
cat files.txt | xargs ls -l | cut -c 23-30 | sed -e :a -e '$!N;s/\n/+/;ta' | bc
Ich benutze gerne ....
echo "
1
2
3 " | sed -e 's,$, + p,g' | dc
sie zeigen die Summe jeder Zeile ...
anwendung in dieser Situation:
ls -ld $(< file.txt) | awk '{print $5}' | sed -e 's,$, + p,g' | dc
Summe ist der letzte Wert ...
#
# @(#) 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. *
Rohr zu gawk:
cat files.txt | xargs ls -l | cut -c 23-30 | gawk 'BEGIN { sum = 0 } // { sum = sum + $0 } END { print sum }'
sizes=( $(cat files.txt | xargs ls -l | cut -c 23-30) )
total=$(( $(IFS="+"; echo "${sizes[*]}") ))
Oder Sie können sie einfach zusammenfassen, wenn Sie die Größen lesen
declare -i total=0
while read x; total+=x; done < <( cat files.txt | xargs ls -l | cut -c 23-30 )
Wenn Sie sich nicht für Bissgrößen und -blöcke interessieren, dann ist es in Ordnung
declare -i total=0
while read s junk; total+=s; done < <( cat files.txt | xargs ls -s )
Wenn Sie R haben, können Sie verwenden:
> ... | Rscript -e 'print(sum(scan("stdin")));'
Read 4 items
[1] 2232320
Da ich mit R vertraut bin, habe ich tatsächlich mehrere Aliase für solche Dinge, damit ich sie in bash
verwenden kann, ohne mich an diese Syntax erinnern zu müssen. Zum Beispiel:
alias Rsum=$'Rscript -e \'print(sum(scan("stdin")));\''
was lass mich machen
> ... | Rsum
Read 4 items
[1] 2232320
Pure bash
total=0; for i in $(cat files.txt | xargs ls -l | cut -c 23-30); do
total=$(( $total + $i )); done; echo $total
cat files.txt | awk '{ total += $1} END {print total}'
Sie können mit der Awk dasselbe tun, es werden sogar die ganzen Zahlen übersprungen
$ cat files.txt
1
2.3
3.4
ew
1
$ cat files.txt | awk '{ total += $1} END {print total}'
7.7
oder Sie können den Befehl ls verwenden und die vom Menschen lesbare Ausgabe berechnen
$ 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
Meiner Meinung nach ist die einfachste Lösung der Befehl "expr" unix:
s=0;
for i in `cat files.txt | xargs ls -l | cut -c 23-30`
do
s=`expr $s + $i`
done
echo $s