Home Learn Linux Linux WC command with examples

Linux WC command with examples

by Arun Kumar
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:

  • First column: It shows the number of lines in the file.
  • Second column: This shows the number of words in the file.
  • Third column: This shows the number of characters present in the file.
  • Fourth column: It shows the name of the file you passed as an argument.

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

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

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

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

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

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

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

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

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

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

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

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.

You may also like

1 comment

sedlav March 25, 2021 - 10:03 AM

wc is not a Linux command else a GNU conmand part of the GNU Core Utils, please don’t confuse people.

Reply

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.