Skip to Content

wc Command in Linux

The wc command in Linux is a powerful tool that displays the number of lines, words, characters, bytes, and maximum line length in text files or command output. For DevOps engineers, it’s especially useful when analyzing logs, validating configuration file sizes and automating shell scripts.


what to expect in wc tutorial for DevOps ?

In this tutorial, we'll learn how to use the wc command in Linux to count lines, words and characters to explore its most practical cases and apply it effectively in real-world DevOps tasks such as log analysis, file auditing, and automation scripts.

  

What is the wc Command in Linux?

The wc command stands for word count but provides much more than just counting words. It returns:

  • Lines
  • Words
  • Characters (bytes)
  • Maximum line length (optional)

It can be used alone or in combination with other commands in pipelines.

Basic Syntax of wc command :

wc [options] [file...]


Commonly Used wc Options

OptionDescription
-lCount lines
-wCount words
-cCount bytes
-mCount characters
-LDisplay length of longest line


Example of wc Command

1. Count Lines, Words, and Characters

wc file.txt

🔹 Outputs three numbers: the line countword count, and character count.

2. Count Lines Only

wc -l file.txt

🔹 Displays only the line count.

3. Count Words Only

wc -w file.txt

🔹 Displays only the word count.

4. Count Characters Only

wc -m file.txt

🔹 Displays only the character count (similar to byte count, but counts multi-byte characters properly).

5. Count Byte Size

wc -c file.txt

🔹 Displays the byte size of the file.

6. Count Number of Running Processes

A common DevOps use case is counting how many processes are running:

ps aux | wc -l

Output:

125

🔹 Shows number of running processes.


DevOps-Related Use Cases for wc command

Count the Number of Files in a Directory

ls /etc | wc -l
  • ls /etc: This lists all files and directories in /etc, one per line (no spaces in between).
  • wc -l : This counts the number of lines.

Since ls lists each file or directory on a new line, using wc -l will count how many lines were output by ls. This will give us the exact number of files and directories in the /etc directory.

Example :

$ ls /etc hostname hosts network passwd shadow systemd

Running ls /etc | wc -l will count the lines and return the number of files and directories:

$ ls /etc | wc -l 6

or

find /etc -type f | wc -l

🔹 Using Find Command to Count Number of Files in a Directory


Using wc with Logs and Pipelines

Sample Log File: /var/log/nginx/access.log

192.168.1.10 - - [20/Jul/2025:10:01:15 +0000] "GET /index.html HTTP/1.1" 200 612 192.168.1.11 - - [20/Jul/2025:10:01:17 +0000] "GET /admin HTTP/1.1" 403 219 192.168.1.10 - - [20/Jul/2025:10:01:19 +0000] "GET /index.html HTTP/1.1" 200 612

  • Count Number of Requests in a Log

cat /var/log/nginx/access.log | wc -l

Output:

3

Shows total HTTP requests. In real usage, logs could have thousands of entries.

  • Count 403 Errors in Access Logs

grep '403' /var/log/nginx/access.log | wc -l

Output:

1

Ideal for detecting failed admin login attempts or permission errors.

  • Count Unique IPs Accessing a Server

cut -d ' ' -f 1 /var/log/nginx/access.log | sort | uniq | wc -l

Output:

2

Extracts the first field (IP), sorts, removes duplicates, and counts them — useful for basic traffic analytics.


Using wc in Automation and Reporting Scripts

wc is often used in combination with other commands and tools for automation and reporting. Here’s we create a script that tracks log file size and sends alerts if the file grows too large.

Script to Alert on Large Log Files

#!/bin/bash # Set the log file and maximum allowed size LOG_FILE="/var/log/syslog" MAX_SIZE=10000 # Get the line count of the log file LINE_COUNT=$(wc -l < $LOG_FILE) # Check if the line count exceeds the threshold if [ $LINE_COUNT -gt $MAX_SIZE ]; then echo "Alert: $LOG_FILE has grown to $LINE_COUNT lines. Consider rotating logs." fi

Explanation:

  • The script checks the number of lines in the /var/log/syslog file.
  • If the file exceeds the defined threshold of 10,000 lines, it triggers an alert.

This is a simple but effective use of wc in system maintenance and automation.


 wc Command Important Points :

  • Automate text data counting in pipelines
  • Integrate with grep, cut, and awk for dynamic data inspection
  • Quickly evaluate logs and system reports
  • Lightweight and script-friendly


Back

Check out these tutorials :