有没有办法从命令输出中找出行数?

Red*_*son 1 shell scripting text-processing

我正在尝试比较两台服务器之间的命令输出。我想从诸如ls -l. 有没有办法这样做?到目前为止,我还没有发现任何东西。

Put*_*nik 6

对于某些人来说,这可能更容易记住:

ls -l | grep . -c

请注意 grep 和 wc 都会计算辅助行,例如total 32044or 或./dirname:。为了避免这种情况,尤其是在递归输出中,请ls -lR尝试以下操作:

find . -type f | grep . -c

其中 first.是目录路径,-type f表示find仅列出文件。如果您需要所有类型(包括目录、套接字等),则只需省略 -type f.

请注意:不wc -l计算空行(仅 '\n')而grep . -c- 不计算。

  • `grep -c '^'` 计算空行。 (2认同)

cuo*_*glm 5

你应该使用wc

$ wc -l .emacs.d/init.el 
73 .emacs.d/init.el
Run Code Online (Sandbox Code Playgroud)

来自man wc

NAME
       wc - print newline, word, and byte counts for each file

SYNOPSIS
       wc [OPTION]... [FILE]...
       wc [OPTION]... --files0-from=F
       ....
       -l, --lines
              print the newline counts
Run Code Online (Sandbox Code Playgroud)

wc是 的一部分GNU coreutils,您可以在大多数Unix-like系统中使用它。