Add up a column of numbers at the Unix shell

后端 未结 20 2488
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条回答
  •  轮回少年
    2021-01-29 18:23

    python3 -c"import os; print(sum(os.path.getsize(f) for f in open('files.txt').read().split()))"
    

    Or if you just want to sum the numbers, pipe into:

    python3 -c"import sys; print(sum(int(x) for x in sys.stdin))"
    

提交回复
热议问题