What is the wc command in Linux?

The wc command in Linux and Unix-like operating systems counts the number of lines, words, characters, and bytes in a file or from the standard input.

A line is a sequence of characters separated by the newline character.

A word is a sequence of characters delimited by white space characters.

Syntax

wc [OPTIONS] [FILES or standard input]

The wc command can accept more than one file as input.

Below are some of the OPTIONS that are mostly used with wc.

Option Description
-l Prints the total number of lines
-w Prints the total number of words
-c Prints the total number of bytes
-m Prints the total number of characters

Output of the wc command

The wc command with no options prints a four-column output.

  1. The first column indicates the number of lines.
  2. The second column indicates the number of words.
  3. The third column indicates the number of bytes.
  4. The fourth column indicates the name of the file passed or is blank when standard input is used.

Example

wc file.txt

The output looks as follows:

      82     129    1961 file.txt

The output above indicates that the file contains 82 lines, 129 words, and 1961 bytes, and the last column indicates the file name.

Code

In the example below, some random text is written to file.txt using the echo command. Next, the wc command is used to get the line, word and byte count of the file.

echo $'hello world \nindiana jones' >> file.txt
wc file.txt

In the code below, the input to wc command is passed as standard input and hence, in the output the file namei.e., the fourth column isn’t displayed.

echo $'hello world \nindiana jones' >> file.txt
wc < file.txt

In the code below, we use wc command with the -w option. The output indicates the number of words in the file.

echo $'hello world \nindiana jones' >> file.txt
wc -w file.txt

Free Resources