Add up a column of numbers at the Unix shell

后端 未结 20 2269
Happy的楠姐
Happy的楠姐 2021-01-29 18:10

Given a list of files in files.txt, I can get a list of their sizes like this:

cat files.txt | xargs ls -l | cut -c 23-30

which pr

20条回答
  •  -上瘾入骨i
    2021-01-29 18:38

    Instead of using cut to get the file size from output of ls -l, you can use directly:

    $ cat files.txt | xargs ls -l | awk '{total += $5} END {print "Total:", total, "bytes"}'
    

    Awk interprets "$5" as the fifth column. This is the column from ls -l that gives you the file size.

提交回复
热议问题