wc — count lines, words, and bytes
💬 "wc command"
wc counts lines, words, and bytes. In practice, the line count (-l) is used overwhelmingly often.
It is often used with pipes to count the number of items that match a specific condition.
- Concept: Prints the number of lines, words, and bytes in the input.
- -l: Counts only lines (most commonly used).
- -w / -c: Word count / byte count.
- Multiple files: Shows per-file results along with the total.
Frequently Used Options
| Option | Description |
|---|---|
| -l | Output only line count |
| -w | Output only word count |
| -c | Output only byte count |
| -m | Output character count (multibyte-aware) |
Three Essential Command Combinations
wc -l /var/log/nginx/access.log— Check the total request line count in the loggrep -c ' 500 ' /var/log/nginx/access.log— Count 500 responses (grep -c is faster)tail -n 1000 /var/log/app.log | grep -c ERROR— Count ERROR occurrences in the last 1000 lines
💬 "Create an example based on a real incident recovery procedure"
This summarizes common situations encountered on live production servers in order.
Step 1 · Assess the Scale
Check how much log has accumulated by line count.
wc -l /var/log/nginx/access.log
Step 2 · Aggregate by Condition
Count occurrences by status code.
awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head
Step 3 · Compare Trends
Compare counts by time period.
grep '18/Oct/2026:02' /var/log/nginx/access.log | wc -l
How It Is Used in Practice
1. Calculate Error Rate
Scenario — You need to check the 5xx ratio against all requests.
total=$(wc -l < /var/log/nginx/access.log); err=$(grep -c ' 5[0-9][0-9] ' /var/log/nginx/access.log); echo "$err / $total"
Result — Compare the error count with the total count to determine the ratio.
Point — The same calculation is used when creating a monitoring metric (error rate).
2. Check Batch Processing Count
Scenario — Use logs to check how many items a batch processed.
grep 'processed' /var/log/app/batch.log | wc -l
Result — Verify that the processed count matches the number of log entries.
Point — Comparing the start and end log entry counts can reveal omissions.
Things to Watch Out For
- Line counting is more expensive than byte counting. For large files, grep -c is faster.
- If the last line does not end with a newline, wc -l does not count that line.
- Piping lets you count without temporary files, but re-reading the source can be expensive.
Real Output
This is the result of checking the total log line count and the number of 500 responses.
!wc command execution result: log line count and error count
Actual production server
Source (HTML): https://games.aidebatehub.com/programs/linux/logs/wc.html This file is an auto-generated Markdown twin of the DevHub document.
AI Knowledge Hub
Visitor Comments