9 wc Command Examples in Linux (Complete Guide)

Today in this guide you will learn how to use wc command in Linux.

wc stands for Word Count.

As you can understand from the name itself, wc command is used to count the available words in the contents of a file.

Not only this, wc has more features which we are going to discuss in this article.

Features of wc command:

With the help of wc you can collect the following information of a file:

  • Word Counts
  • Byte Counts
  • Character Counts
  • Newline Counts
  • Maximum Display Width

Now, first of all, let’s focus on some of the most important options that we can use with the wc.

OptionsExplanation
-c, --bytesprint the byte counts
-m, --charsprint the character counts
-l, --linesprint the newline counts
-L, --max-line-lengthprint the maximum display width
-w, --wordsprint the word counts
--helpDisplay this help and exit
--versionoutput version information and exit

Syntax:

You must follow the syntax given below to use the wc command.

wc [OPTION]... [FILE]...
wc [OPTION]... --files0-from=F

1. How to use the wc Command?

Here I have a file named months.txt which contains some content. You can display the contents of this file using cat command.

With the help of this, I will explain this concept to you.

~$ cat months.txt 
### Months of the Year ###

January
February
March
April
May
June
July
August
September
October
November
December

By default the wc command prints the following information without any option.

  • Word Counts
  • Byte Counts
  • Character Counts
  • Line Counts

Example:

~$ wc months.txt 
 15  18 115 months.txt
Linux wc Command

If you don’t want to print the filename type the following command.

~$ wc < months.txt 
 14  18 114

Now lets Print Line, Word, and Byte counts present in multiple files. Here is an example:

~$ wc months.txt week.txt 
 14  18 114 months.txt
  7   7  58 week.txt
 21  25 172 total

2. Print the Word Counts

To print the count of the Words present in a file, Pass the -w option to wc.

~$ wc -w months.txt 
18 months.txt
Word Counts

You can also use the long option --words.

~$ wc --words months.txt

3. Print the Byte Counts

To print the count of the Bytes present in a file, Pass the -c option to wc command.

~$ wc -c months.txt 
115 months.txt

Note: You can also say that the size of this file is 115 bytes.

Byte Counts

You can also use the long option --bytes.

~$ wc --bytes months.txt

4. Print the Character Counts

To print the count of the Characters present in a file, Pass the -m option to wc.

~$ wc -m months.txt 
115 months.txt
Character Counts

You can also use the long option --chars.

~$ wc --chars months.txt

Note: As you can see byte and character counts are same because 1 byte = 1 character

5. Print the Newline Counts

To print the count of the New Lines present in a file, Pass the -l option to wc Command.

~$ wc -l months.txt 
14 months.txt
Newline Counts

You can also use the long option --lines.

~$ wc --lines months.txt

6. Print the Maximum Display Width

You must be wondering what Maximum Display Width is. It means to print the longest line present in a file.

Note: wc command treats each letter, character, space as a count.

To do so you can use the -L argument with wc command.

~$ wc -L months.txt
26 months.txt
Maximum Display Width
Longest line of the File

You can also use the long option --max-line-length.

~$ wc --max-line-length months.txt

7. Usage of Wildcard with the help of wc

You can use Wildcards with the help of wc command. Let's take some examples:

Ex # 1: Following command will print the count of words present in all those files whose extension is *.txt.

~$ wc *.txt

Ex # 2: Following command will print the count of lines present in all those files that start with "te"

~$ wc te*

8. Combine wc Command with other Commands

You can combine the wc command with other commands with the help of Pipe (|).

Let's take some examples:

Ex # 1: Combine wc with ls command.

Task #1 Count the number of words and lines in the output of ls -l command.

~$ ls -l | wc -wl
     17     146

Ex # 2: Combine wc with egrep command.

Task #1 Count how many files are in the current directory.

~$ ls -l | egrep '^-' | wc -l

Task #2 Count how many directories are in the current directory.

~$ ls -l | egrep '^d' | wc -l

9. Usage of Redirection operators with the help of wc Command

You can use Redirection operators like >, >> with wc.

Let's take some examples:

Ex #1: Following command will redirect the output of wc command to a file named test.txt.

~$ wc months.txt > test.txt

Ex #2: Following command will append the output of wc to the existing file, named test.txt.

~$ wc months.txt >> test.txt

Infographic

Linux wc Command

You can visit at following websites to get more information on wc command.

Conclusion

I hope you have learned something from this article.

I have tried my best to include all the features of wc command in this guide.

Now I’d like to hear your thoughts.

Was this guide useful to you?

Or maybe you have some queries.

Have I not included any command in this guide?

Leave a comment below.

If you like our content, please consider buying us a coffee.

Buy Me A Coffee

We are thankful for your never ending support.

Leave a Comment