国际访客建议访问 Primers 编程伙伴 国际版站点 > Bash 教程 > wc 以获得更好的体验。

# Bash 的 wc 命令

wc [OPTION]... [FILE]...

!subtitle:功能

计算文件的行数、单词数和字节数。

!subtitle:类型

可执行文件(/usr/bin/wc),属于 coreutils

!subtitle:参数

  • OPTION 选项:

    • -c, --bytes - 打印字节数

    • -m, --chars - 打印字符数

    • -l, --lines - 打印行数

    • --files0-from=F - 从文件 F 中读取文件列表作为输入,,每个文件名以空字符(\0)结尾;如果 F- 则从标准输入读取文件列表

    • -L, --max-line-length - 打印的最大宽度

    • -w, --words - 打印单词数

    • --total=WHEN - 何时打印总数行:

      • auto - 自动

      • always - 总是打印总数行

      • only - 仅打印总数行,不打印各文件行

      • never - 从不打印总数行

    • --help - 显示帮助

    • --version - 显示版本

  • FILE - 文件列表;如果没有这个参数或指定为 -,则读取标准输入

# 示例

$ cat 1.txt
Primers 编程伙伴
https://xplanc.org/primers/
$ wc 1.txt
 2  3 49 1.txt
$ wc 1.txt 2.txt --total=always     # 打印总数行
 2  3 49 1.txt
 3  3  6 2.txt
 5  6 55 total
$ wc 1.txt 2.txt --total=only       # 仅打印总数行
 5  6 55 total

# 推荐阅读

# 手册

WC(1)                            User Commands                           WC(1)

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

SYNOPSIS
       wc [OPTION]... [FILE]...
       wc [OPTION]... --files0-from=F

DESCRIPTION
       Print newline, word, and byte counts for each FILE, and a total line if
       more  than one FILE is specified.  A word is a non-zero-length sequence
       of printable characters delimited by white space.

       With no FILE, or when FILE is -, read standard input.

       The options below may be used to select which counts are  printed,  al‐
       ways  in  the  following order: newline, word, character, byte, maximum
       line length.

       -c, --bytes
              print the byte counts

       -m, --chars
              print the character counts

       -l, --lines
              print the newline counts

       --files0-from=F
              read input from the files specified by NUL-terminated  names  in
              file F; If F is - then read names from standard input

       -L, --max-line-length
              print the maximum display width

       -w, --words
              print the word counts

       --total=WHEN
              when  to  print a line with total counts; WHEN can be: auto, al‐
              ways, only, never

       --help display this help and exit

       --version
              output version information and exit

AUTHOR
       Written by Paul Rubin and David MacKenzie.

REPORTING BUGS
       GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
       Report any translation bugs to <https://translationproject.org/team/>

COPYRIGHT
       Copyright © 2023 Free Software Foundation, Inc.   License  GPLv3+:  GNU
       GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
       This  is  free  software:  you  are free to change and redistribute it.
       There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       Full documentation <https://www.gnu.org/software/coreutils/wc>
       or available locally via: info '(coreutils) wc invocation'

GNU coreutils 9.4                 April 2024                             WC(1)
本文 更新于: 2026-03-06 09:52:36 创建于: 2026-03-06 09:52:36