Calculating the number of lines in a file?

Solution 1:

Use the tool wc.

  • To count the number of lines: -l

    wc -l myfile.sh
    
  • To count the number of words: -w

    wc -w myfile.sh
    

See man wc for more options.

Solution 2:

As mentioned by souravc, you can use wc for this:

$ wc -w statusToFiles.sh 
10 statusToFiles.sh
$ wc -l statusToFiles.sh 
6 statusToFiles.sh

To only display the count itself, you can pipe that output to awk, like this:

$ wc -l statusToFiles.sh | awk '{ print $1 }'
6

...or as kos mentioned below:

$ < statusToFiles.sh wc -l
6