Languages
[Edit]
EN

Bash - how to count lines in a document?

9 points
Created by:
Marcin
2441

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

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

Examples: 

1. Single file:

$ wc test.txt
  5  6 16 test.txt

 Where:

  • 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 

Checkout latest findings & news:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Rockstar community members
Thank you for great work 👍