【问题标题】:Using one command line count the lines in the last file located in /etc in ubuntu使用一个命令行计算 ubuntu 中位于 /etc 的最后一个文件中的行数
【发布时间】:2021-02-15 04:08:24
【问题描述】:

ls /etc |尾-1 | wc -l

所以基本上我使用了这个命令,但它计算了我从 tail 命令获得的文件数(这是目录中的最后一个文件=1),但我没有得到其中的行数文件。 我使用 cat 命令打开文件并计算行数,但它不起作用。 ls /等 |猫尾巴-1 | wc -l ls /等 |尾-1 |猫 |wc -l

【问题讨论】:

  • 你应该奖励回答者

标签: linux ubuntu count command wc


【解决方案1】:

您可以使用xargs 将tail 的结果用作wc 的参数,尽管我建议使用find 而不是ls,这样您就可以得到完整的路径并且不要需要弄乱相对路径:

$ find /etc -type f | tail -1 | xargs wc -l

【讨论】:

    【解决方案2】:

    你永远不应该解析ls(而是解析/etc/*)

    $ wc -l < `find /etc -maxdepth 1 -type f | tail -n 1`
    

    或

    $ find /etc -maxdepth 1 -type f | tail -n 1 | wc -l
    

    它的作用是找到 /etc 的最后一个文件

    并将其内容放入wc -l

    【讨论】:

      猜你喜欢
      • 2014-10-16
      • 1970-01-01
      • 1970-01-01
      • 2011-09-18
      • 1970-01-01
      • 1970-01-01
      • 2011-05-19
      • 1970-01-01
      • 2014-06-30
      相关资源
      最近更新 更多