Learn Linux

Linux WC command with examples

Linux WC command with examples

Previously, we have written articles on various Linux commands like ls, ps, scp, history, and many more present on our website. Today, we will take a look at the WC command.

WC stands for “Word Count.” And as the name implies, the wc command is used for counting purposes. It prints out the number of lines, word count, byte, and characters count present in the file passed to the command. When you run this command on a file without any additional parameters/ arguments, we will print the result in four columns:

WC command

Syntax:

wc [options] [file_name]

Consider the two files below, sample_one and sample_two. Sample_one contains a list of Linux distributions, while Sample_two includes a list of programming languages.

Sample Files

By parsing only the filename in the wc command, we get the results below:

wc sample_one.txt wc sample_two.txt

Sample Output

A fantastic feature with the WC command is that you can also pass multiple files. Check out the command below:

wc sample_one.txt sample_two.txt

Multiple files output

From the output shown image above, the first line shows the results for sample_one.txt, and the second line shows results for sample_two.txt. However, you will notice there is a third line which we will call Total. This one shows the sum of the results from the two files. It displays the total number of lines, words, and characters of all the files passed.

WC command options

WC is a simple command to work with and comes with only a handful of options to select from:

-l, --lines - Prints the number of lines present in the file\
-w, --words - Print the total number of words in the file (word count).
-m, --chars -Prints the number of characters in the file.
-L, --max-line-Length - Prints the longest line in the file.
-c, --bytes - Prints the total number of bytes in the file.

Let's now look at every option and how we can use it on various files.

1. -l, -lines Option

This option prints the total number of lines in the file. The information is printed in two columns. The first column shows the number of lines present, and the second column showing the name of the file passed.

wc -l sample_one.txt

wc -l command_1

2. -w, -words Option

The -w or --words option shows the total number of words present in a file. It prints the results in two columns. The first column shows the total number of words, and the second column showing the name of the file.

wc -w sample_one.txt

wc -w command

3. -m, -chars option

The -m or --chars option shows the total number of characters in the file. It prints the results in two columns. The first column shows the total number of characters in the file, and the second column shows the name of the file.

wc -m sample_one.txt

Count Characters

4. -L, -max-line-Length Option

The -L (uppercase) option prints the length (number of characters) of the longest line in the file. In the file sample_one.txt, the longest line is “Elementary OperatingSystem,” and in sample_two.txt, the longest line is “Javascript.” When you pass more than one filename, unlike the other options, which would show the total, this option indicates the number of characters of the two files' longest line.

wc -L sample_one.txt wc -L sample_one.txt sample_two.txt

wc -L command

5. -c, -bytes Option

This option shows the count of bytes present in the file. It prints the results in two columns. The first column shows the total number of bytes in the file, and the second column showing the name of the file passed.

wc -c sample_one.txt

Count bytes

6. -version Option

This option doesn't take any filename but only shows the version of wc currently running on your system.

wc --version

wc version

Application of WC Command

1. Count the Number of Files and Folders Present in a Directory

The ls command is used to list all the contents in a directory. When piped with the wc -l command, we could count the number of files and folders present in the directory. See the command below:

ls /home/tut | wc -l

Pipe wc with ls command

2. Count the Number of Users

To list the users in a system, we could use the commands cat /etc/passwd or getent passwd. If we just wanted a count of the total number of users, we could pipe the output of, say getent passwd to wc -l as shown below:

getent passwd | wc -l

Count number of users

Conclusion

I believe this post has given you a clear guide on using the WC (word count) command and the various options available. You can perform much more powerful tasks by combining the wc command with other Linux commands. If you come across any challenges, you can use the command wc --help. Please feel free to leave any queries or comments regarding this post.

Middle mouse button not working in Windows 10
The middle mouse button helps you scroll through long webpages and screens with a lot of data. If that stops, well you will end up using the keyboard ...
How to change Left & Right mouse buttons on Windows 10 PC
It's quite a norm that all computer mouse devices are ergonomically designed for right-handed users. But there are mouse devices available which are s...
Emulate Mouse clicks by hovering using Clickless Mouse in Windows 10
Using a mouse or keyboard in the wrong posture of excessive usage can result in a lot of health issues, including strain, carpal tunnel syndrome, and ...