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]
wc command can accept more than one file as input.
Below are some of the
OPTIONS that are mostly used with
||Prints the total number of lines|
||Prints the total number of words|
||Prints the total number of bytes|
||Prints the total number of characters|
wc command with no options prints a four-column output.
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.txt wc 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.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
View all Courses