WC Command

Linux wc (Word Count) Command

WC Command


The "wc" command or "word count" as it is commonly known is used for the counting of words, lines and bytes of file(s). Below are some useful examples of the "wc" command in action.



General Syntax of wc command



wc [OPTION]... [FILE]...

wc - Parameters


The following table contains various parameters that may be passed to the wc command:


-c, --bytes Print the byte counts
-m, --chars Print the character counts
-l, --lines Print the newline counts
-L, --max-line-length Print the length of the longest line
-w, --words Print the word counts
--help Display this help and exit
--version Output version information and exit

wc - No Parameters passed


In this example we will use the "wc" command with no additional parameters. We will also use a test file to help demonstrate the wc command. The contents of the test file can be seen from the "cat" command below:



john@ubuntu01-pc:~/word_count_examples$ cat test.txt
Arch Linux
Bohdi Linux
CentOS Linux
Deepin Linux
Elementary OS
Fedora Linux
Gentoo
Kali
Lubuntu Linux
Mageia

wc test.txt



john@ubuntu01-pc:~/word_count_examples$ wc test.txt 
 10  17 109 test.txt

From the above output we can see that there are 10 lines, 17 words and a total of 109 characters.


Display Line Count


wc -l test.txt



john@ubuntu01-pc:~/word_count_examples$ wc -l test.txt 
10 test.txt

From the above we can see that only information relating to the number of lines has been returned. In this example, we have 10 lines.


Display Byte Count


wc -c test.txt



john@ubuntu01-pc:~/word_count_examples$ wc -c test.txt 
109 test.txt

From the above output we can see that the file test.txt is 109 bytes in size.


Display Character Count


wc -m test.txt



john@ubuntu01-pc:~/word_count_examples$ wc -m test.txt 
109 test.txt

From the above output we can see that there are 109 characters within the file test.txt.


Display the length of the longest line within a file


wc -L test.txt



john@ubuntu01-pc:~/word_count_examples$ wc -L test.txt 
13 test.txt

From the above output we can see that the length of the longest line within out test file is 13 characters long. (Elementary OS = 13 characters long)


Displaying the number of words in a file - word count


wc -w test.txt



john@ubuntu01-pc:~/word_count_examples$ wc -w test.txt 
17 test.txt

From the above output we can see that there are a total of 17 words found within our test file.


Display number of files in a directory


Another useful way to use the wc command is to combine the output from the "ls" command and pipe this into word count. This will then give us a rough file count.

ls | wc -l



john@ubuntu01-pc:~/word_count_examples$ ls
test2.txt  test3.txt  test4.txt  test.txt
john@ubuntu01-pc:~/word_count_examples$ ls | wc -l
4

From the above we can see that we have a total of 4 files in the current directory. Note files that begin with a dot "." are not counted. To count files that begin with a dot ".", the "-a" option would have to be used in conjunction with the "ls" command.