1. Introduction

This tutorial will discuss the Linux wc command. We’ll present some basic usage as well as a couple of more advanced examples.

2. Basic Usage

wc is a handy utility to count words, lines, or bytes of the provided input, either a list of files or standard input. Let’s take a look at some basic usage.

We’ll start with a sample file:

% cat colors.txt 
Red Orange

First, let’s get the number of lines on the file:

% wc -l colors.txt 
       6 colors.txt

We can also do a word count:

% wc -w colors.txt 
       7 colors.txt

We can also count the number of characters in the file:

% wc -m colors.txt 
      41 colors.txt

And since we are in a text file, a count of bytes yields the same number:

% wc -c colors.txt 
      41 colors.txt

3. Combining wc With grep

Combining wc with other Linux commands allows us to perform some useful tasks.

In conjunction with grep, wc gives a count of occurrences in a set of files.

For example, let’s say we need to figure out how many errors of a certain type occurred over several log files.

Let’s look at our first sample log file:

% cat log_mon.txt 
12:31: error - broken pipe
12:33: warn - missing input
12:35: error - broken pipe
12:37: error - service down
12:39: warn - retry failed

And now, our second:

% cat log_tues.txt 
12:41: error - broken pipe
12:43: warn - missing input
12:45: error - broken pipe
12:49: warn - retry failed

If we did grep with the -c flag, we would get a breakdown by file:

% grep -c "broken pipe" log*.txt

But to get a total count, we use wc with the -l (line count) option:

% grep "broken pipe" log*.txt | wc -l

grep sends all the results to standard input, and wc performs a line count of that input.

4. Combining wc With find

For another example, let’s combine wc with some commands to count the lines of source code in a project. Let’s say we’re looking at the Baeldung Lombok project demo. We can put findxargs, and wc together to get a count of lines of source code in the project:

% find ./src/main -name "*.java" | xargs wc -l
      18 ./src/main/java/com/baeldung/lombok/getter/GetterBooleanPrimitiveSameAccessor.java
      16 ./src/main/java/com/baeldung/lombok/getter/GetterBooleanPrimitive.java
      13 ./src/main/java/com/baeldung/lombok/getter/GetterBooleanSameAccessor.java
      15 ./src/main/java/com/baeldung/lombok/getter/GetterBoolean.java
      15 ./src/main/java/com/baeldung/lombok/getter/GetterBooleanType.java
      36 ./src/main/java/com/baeldung/lombok/intro/GetterLazy.java
      16 ./src/main/java/com/baeldung/lombok/intro/HasContactInformation.java
      43 ./src/main/java/com/baeldung/lombok/intro/User.java
      22 ./src/main/java/com/baeldung/lombok/intro/ApiClientConfiguration.java
      11 ./src/main/java/com/baeldung/lombok/intro/ClientBuilder.java
      25 ./src/main/java/com/baeldung/lombok/intro/LoginResult.java
      29 ./src/main/java/com/baeldung/lombok/intro/UserEvent.java
      26 ./src/main/java/com/baeldung/lombok/intro/Utility.java
      17 ./src/main/java/com/baeldung/lombok/intro/ContactInformationSupport.java
      11 ./src/main/java/com/baeldung/lombok/intro/ImmutableClient.java
      39 ./src/main/java/com/baeldung/lombok/builder/customsetter/Message.java
      25 ./src/main/java/com/baeldung/lombok/builder/singular/Person.java
      14 ./src/main/java/com/baeldung/lombok/builder/singular/Sea.java
      17 ./src/main/java/com/baeldung/lombok/builder/defaultvalue/Pojo.java
      19 ./src/main/java/com/baeldung/lombok/builder/inheritance/buildermethodname/Child.java
      10 ./src/main/java/com/baeldung/lombok/builder/inheritance/buildermethodname/Parent.java
      16 ./src/main/java/com/baeldung/lombok/builder/inheritance/buildermethodname/Student.java
      11 ./src/main/java/com/baeldung/lombok/builder/inheritance/superbuilder/Child.java
      11 ./src/main/java/com/baeldung/lombok/builder/inheritance/superbuilder/Parent.java
      10 ./src/main/java/com/baeldung/lombok/builder/inheritance/superbuilder/Student.java
      11 ./src/main/java/com/baeldung/lombok/builder/ClientBuilder.java
      11 ./src/main/java/com/baeldung/lombok/builder/ImmutableClient.java
      13 ./src/main/java/com/baeldung/lombok/builder/Widget.java
     520 total

5. Conclusion

In this article, we’ve seen how to use the Linux wc command. We saw some basic options as well as some ways to combine wc with other Linux commands to create some neat one-liners.

Notify of
Inline Feedbacks
View all comments