wc Commandwc命令

Count lines, words, and bytes统计行数、单词数和字节数

What is wc?什么是wc?

wc stands for "word count". It prints newline, word, and byte counts for each file. It's essential for quickly assessing file sizes, counting lines of code, or analyzing text data.

wc代表"word count"(字数统计)。它打印每个文件的换行符、字数和字节计数。对于快速评估文件大小、统计代码行数或分析文本数据至关重要。

Basic Usage基本用法

$ wc file.txt
42 168 1024 file.txt
# 42 lines, 168 words, 1024 bytes
$ wc -l *.py
# Count lines in all Python files
$ wc file.txt
42 168 1024 file.txt
# 42行、168字、1024字节
$ wc -l *.py
# 统计所有Python文件的行数

Common Options常用选项

Option选项 Full Name完整名称 Description描述 Example示例
-l lines行数 Count newline characters计算换行符数量 wc -l file.txt
-w words字数 Count words计算字数 wc -w file.txt
-c bytes字节 Count bytes计算字节数 wc -c file.txt
-m chars字符 Count characters计算字符数 wc -m file.txt
-L max-line-length最大行长度 Display length of longest line显示最长行的长度 wc -L file.txt

Practical Scenarios实际场景

Count Lines of Code统计代码行数

$ find . -name "*.py" | xargs wc -l
# Count total Python lines in project
$ find . -name "*.py" | xargs wc -l
# 统计项目中Python的总行数

Find Longest Line查找最长行

$ wc -L file.txt
# Shows length of longest line
$ wc -L file.txt
# 显示最长行的长度

Memory Tricks记忆技巧

📊 "wc" = Word Count - Count words and more!

📊 "wc" = Word Count(字数统计) - 统计字数等!

📏 -l = Lines - Count lines (newlines)

📏 -l = Lines(行) - 计算行数(换行符)

📝 -w = Words - Count words (whitespace-separated)

📝 -w = Words(字) - 计算字数(空白符分隔)

🦠 -c = Characters/bytes - Count bytes

🦠 -c = Characters/bytes(字符/字节) - 计算字节数