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.
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 |
wc
commandThe wc
command with no options prints a four-column output.
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.
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.txtwc file.txt
In the code below, the input to wc
command is passed as standard input and hence, in the output the
echo $'hello world \nindiana jones' >> file.txtwc < 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.txtwc -w file.txt