【问题标题】:How to calculate the total size of all files from ls- l by using cut, eval, head, tr, tail, ls and echo如何使用 cut、eval、head、tr、tail、ls 和 echo 从 ls-l 计算所有文件的总大小
【发布时间】:2018-09-29 11:33:21
【问题描述】:
 ls -l | tr -s " " | cut -d " " -f5

我尝试了上面的代码并得到了以下输出。

158416 757249 574994 144436 520739 210444 398630 1219080 256965 684782 393445 157957 273642 178980 339245

如何添加这些数字。我被困在这里。请不要使用 awk、perl、shell 脚本等。

【问题讨论】:

标签: bash shell unix


【解决方案1】:

使用du 是最简单的。比如:

du -h -a -c | tail -n1

会给你总和。您还可以使用-d 参数来指定遍历的深度:

du -d 1 -h -a -c | tail -n1

您必须澄清“不使用 shell 脚本”的意思,以便任何人提出更有意义的答案。

【讨论】:

    【解决方案2】:

    您可以尝试这种方式,但 $((...)) 是 shell 脚本

    eval echo $(( $(ls -l | tr -s ' ' | cut -d ' ' -f5 | tr '\n' '+')  0 ))
    

    【讨论】:

      【解决方案3】:

      Don't parse the output of ls。它不是用来解析的。将duMartin Gergov's answer 一样使用。或du and find,或just du

      但是如果只是添加数字是唯一的焦点,(即使输入是不确定的),这是最懒惰的方法(首先安装num-utils):

      ls -l | tr -s " " | cut -d " " -f5 | numsum
      

      还有其他方法,另见:How can I quickly sum all numbers in a file?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-07
        • 2021-11-23
        • 2011-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-11
        • 1970-01-01
        相关资源
        最近更新 更多