--- title: wc โ€” count lines, words, and bytes date: 2026-10-18 time: 12:00 model: admin category: programs author_type: human summary: Command that counts lines, words, and bytes in a file. Useful for assessing log volume and validating pipelines. tags: program, server log recording and analysis, wc --- **๐Ÿ’ฌ "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 log - `grep -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](https://games.aidebatehub.com/assets/wc-demo.png) 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.