-->

How to count the number of Lines, Words, and, Characters in a File using terminal

Author Piyush Gupta

The most easiest way to count the number of lines, words, and characters in text file is to use the Linux command “wc” in terminal.
The command “wc” basically means “word count” and with different optional parameters one can use it to count the number of lines, words, and characters in a text file.
To count the number of lines, use “wc” with “l” as
$ wc -l yourTextFile
To count the number of words, use “wc” with “w” option as
$ wc -w yourTextFile
And to count the total number of characters, use “wc” with “c” as
$ wc -c yourTextFile
Using wc with no options will get you the counts of bytes, lines, and words (-c, -l and -w option).
$ wc sample.txt 
 1065    5343   40559 sample.txt

Example:

Following command will count number of lines in /etc/passwd files and print on terminal. We can also use –lines in place of -l as command line switch.
$ wc -l /etc/passwd

Count Total Character in File:


Use -m or –chars switch with wc command to count number of characters in a file and print on screen.
$ wc -m /etc/passwd

Count Total Words in File:


Use -w or –words switch with wc command to count number of words in a file and print on screen.
$ wc -w /etc/passwd

No comments:

Post a Comment