How to Use the wc Command in Linux

Linux offers a large number of command line tools to simplify your daily tasks. One such tool is the wc command.


wc is your go-to command when you need to know the number of words in a file, or even how many files are in a given directory. But that’s not all the wc command does. Read on to learn what the wc command is and how to use it effectively on Linux.


What is the WC command?

The wc command stands for word count. It is a command line tool that can be used to count the number of words, lines, characters, and bytes in an output. It comes pre-installed in every Unix and Linux-based operating system, so you don’t have to install it manually.

The wc command syntax

To use wc, you must provide file or text output and the command options you want to use. The basic syntax of the wc command is:

 wc [OPTION] [FILE]

There are many options available alongside the command, all of which we will discuss later. For command line help on the wc command, check the manual page by running:

man wc

How to use the wc command

Create a file for this example: zen.txt. In this file, paste the following text:

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one and preferably only one obvious way to do it.[a]
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than right now.[b]
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea – let's do more of those!

This is the Zen of Python, and it’s a set of 19 guiding principles written by Tim Peter for writing simple, elegant, and concise Python code.

If you create the file with the cat command, leave a blank line before pasting the text.

Using the standard wc command

By default, when you use the wc command with a file or output, it returns the number of lines, words, and bytes present in the output.

Try it out with zen.txt by running this command in your terminal:

wc zen.txt

The result:

19 137 824 zen.txt

You would see that it outputs four columns containing the number of lines, words, bytes, and the name of the file, respectively.

To count the number of lines present in a file or output, use the -l or –lines Possibility. The syntax looks like this:

wc -l zen.txt

The result:

19 zen.txt

It shows you have 19 lines in the file and also prints the name of the text file.

To count the number of words in a file, use the -w or –Words Possibility. Try it:

wc -w zen.txt

The result:

137 zen.txt

Displays the number of bytes

You can find out the exact number of bytes in a file by using the wc command alongside the -c or – bytes Possibility. Run this command to try it out:

wc -c zen.txt

The result:

824 zen.txt

To print out the number of characters in a file, use the -m or – Sign Possibility. The syntax looks like this:

wc -m zen.txt

The result:

818 zen.txt

If you need to know the length of the longest line – the number of characters on that line – in a file, use the -L or the –max line length option with the wc command. It looks like this:

wc -L zen.txt

The result:

67 zen.txt

Using the wc command with multiple files

You can use the wc command with more than one file or input. To do this, you need to create two more files. The first file is letters.txtwhich contains a list of the alphabet, while the second file is number.txtcontaining a list of numbers from one to ten.

Alternatively, you can use any two text files. Let’s try it:

wc zen.txt letters.txt num.txt

The result:

 19 137 824 zen.txt
26 26 52 letters.txt
10 10 21 num.txt
55 173 897 total

The first three lines contain the number of lines, words and bytes of each file and the last line contains the grand total of each column.

Using the wc command with other Linux commands

You can use wc with other commands via the pipe command. The pipe symbol redirects the output of one command as input for another.

Count the number of files or folders in a directory

To do this, you use the ls command to list the number of files in a directory, and then pipe the input to the wc command. For example, to print the number of files on your desktop, run the following command:

ls Desktop | wc -l

Count the number of running processes on your system

Processes are tasks or programs that your computer is working on or is currently running. When you run a command or open an application, it is registered as a process.

To count the number of processes, use the ps command with wc. Here, try it:

ps | wc -l

Try other Linux commands with wc

There are many commands available on Linux that have very unique features and make the overall Linux experience seamless. You just have to know what they are and how to use them! Start your adventure today!

Leave a Reply

Your email address will not be published. Required fields are marked *