How to use WC command to print character, byte, newline and word count in Linux

WC is a Linux command to print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified. If there is no input file parameter or it is “-” and then it reads the input from the standard input device.

Below is the syntax,

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

All the parameters are optional.

Here,

Option basically controls what you want the command to do, shall it print byte, the character on newline count.

-c:- print the byte counts
-m:- print the character counts
-l:- print the newline counts
-L:- print the length of the longest line
-W:- print the word counts
–help:- Print help
–version:- Display version information

File is the input file name. You can pass multiple file names separated by space as input to wc command but when you do not specify any file name, the command expects you to enter in the standard input device i.e terminal.

How to use wc command

wc command simply reads the input file or user-entered input text, count number of newlines, characters, and bytes and print on the terminal.

Let’s consider below sample file.

$ cat wc_demo.txt
this is line 1
this is line 2
this is line 3
this is line 4

Enter the wc command with a file name(wc_demo.txt) as the input parameter as shown below.

$ wc wc_demo.txt
 4 16 60 wc_demo.txt

You can see 4 columns output.

  • first column shows the number of newlines – 4
  • second column shows the number of words present in each file – 16
  • Third columns show the number of characters – 60
  • fourth column shows the name of the file – wc_demo.txt

You can also pass multiple files as input separated by space. When multiple files are there, it shows additional output (total) showing the total of all files as shown below.

$ wc wc_demo_1.txt wc_demo_2.txt
  4  16  60 wc_demo_1.txt
  3  12  45 wc_demo_2.txt
  7  28 105 total

You can also input data from the keyboard.

Enter wc on shell prompt and click enter. Type any text separated with space and lines. Click enter and then press CTRL+D to end the input.

$ wc
this is what entered from input device
      1       7      39

It also supports shell special characters(metacharacters) like redirection and pipe. You can pass the output of any command to wc to get the required words and lines count.

Here ls command lists the content of the directory and the same is passed to wc using a pipe.

$ ls -l | wc
 26 227 1684

The below example shows how the file is passed (wc_demo.txt) as input using < redirection.

$ wc < wc_demo.txt
 1  4 15

The below example shows how the file is passed (wc_demo.txt) as input using < redirection and the output of the command is redirected to the output file using > redirection.

$ wc < wc_demo.txt > output
$ cat output
 1  4 15

WC Command Examples

Below are the examples showing the use of the WC command.

1. Print the byte counts

Use – c option to print byte count.

$ wc -c wc_demo.txt
20 wc_demo.txt

2. Print the character count

Use – m option to print byte count. Output of -c and -m will be the same unless your file contains multi-byte characters e.g Chines, Japanese, etc

$ wc -m wc_demo.txt
20 wc_demo.txt

3. Print the newline count

-l option prints the newline count(not the number of lines). One point to remember here, wc works on “/n” lines character. It counts the newline not the number of lines. If there is no newline character, then the count will be one less.

Check this thread on StackOverflow.

$ wc -l wc_demo.txt
1 wc_demo.txt

4. Print length of the longest line

-L print the length of the longest line. Remember the difference between capital case L and small case l.

wc -L wc_demo.txt
19 wc_demo.txt

5. Print the word count

-w option prints the word count.

$ wc -w wc_demo.txt
4 wc_demo.txt

Below are generic parameters supported by all the commands

6. –help option displays help and exit

$ wc – help

7. –version option output version information and exit

$ wc – version

Summary

Now you know how to print newline, word, and byte counts in Linux/Unix OS. You can always refer manual by running the man -wc command on the terminal.

I hope you found this article useful. Please share and subscribe.

Share This: