Bash - how to count lines in a document?

9 points
Created by:

To count lines, words or characters we can use wc command:

$ cat /etc/passwd | wc
$ wc /etc/passwd 


1. Single file:

$ wc test.txt
  5  6 16 test.txt


  • 5 - number of rows
  • 6 - number of words
  • 16 - number of characters

2. Multiple files:

$ wc foo.txt bar.txt
    40     149     947 foo.txt
  2294   16638   97724 bar.txt
  2334   16787   98671 total 

There are few useful modifications too:

  • wc -l path/to/file - prints only total number of lines (note that if the last line does not have \n, the line will be not counted) 
  • wc -c path/to/file - prints only total number of bytes
  • wc -m path/to/file - prints only total number of character 
  • wc -L path/to/file - prints only length of longest line 
  • wc -w path/to/file - prints only number of words 
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.


Bash - count lines in a document
Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂