How to Count number of lines in a file in Linux ?

When you are just interested into number of lines in a file to make some decision based on it, in Linux using command you can know the same very easily. For an example, we have used simple C program with addition as a text file.

Using “wc” command

You can use “wc” command with “-l” option passed, so that it can print only new line counts on terminal as,

$ wc -l helloworld.c 
8 helloworld.c

Using “cat” command

You can also use the “cat” command to print the line numbers of the file, and at the end you will see the total number of lines.

$ cat -n helloworld.c 
     1	#include <stdio.h>
     2	
     3	int main(int argc, char **argv) {
     4		int a = 5, b = 7, sum = 0;
     5		sum = a + b;
     6		printf("Total is : %d", sum);
     7		return 0;
     8	}

Leave a Comment