How To Count Lines In Linux?

Linux is a file-based operating system that contains a lot of files. Linux generally prefers simple text files for configuration and different purposes for its simplicity and ease to read and write. But how can I count lines in Linux?

What Is Line In Linux?

A file consists of some data which can be a configuration, text, binary, XML, etc. But what makes the line in Linux. First of all binary files do not contains lines. Generally, human-readable file types like text, XML, configuration, JSON contain lines with the end of line markers. The end of line marker is “\n” in Linux operating systems. Even the end of the line is not displayed explicitly they exist and simply sets the current line end and jumps to the next line.

Count Lines with wc Command

The official tool to count lines in Linux operating system is the wc command. The wc command name comes from the “word count”. The wc command prints the line count of the specified file with the -l option.

wc -l file.txt

The output is like below where the 22 is the line count.

22 random.c

We can also use the wc command without any option where 3 numbers will be dispplayed. The first number is the count of lines and others are word count and character count.

wc myfile.txt

The output is like below where the 22 is the line count, 37 is the word count and 308 is the character count.

22  37 308 myfile.txt

Alternatively we can specify the full or absolute path for the line count.

wc -l /home/ismail/myfile.txt

Count Lines with nl Command

The nl command is another command which can be used to count lines of a file. But the nl command works a bit differently where only lines that contain non-space characters. Simply empty lines are not counted.

nl random.c
Count Lines with nl Command

Count Lines with grep Command

If you want to count lines those ends with the point the grep command can be used for this. But keep in mind that every point is counted as a line or sentence.

grep -c "*." myfile.txt

Count Lines with awk Command

The awk command is a very useful command which provides a lot of functions even a scripting language to work with files. As a talented command, it can be used to count lines like below.

awk 'END{print NR}' myfile.txt
  • END is the end of file specifier.
  • print NR prints the line number of the current line which is the last line.
READ  How To Delete Line In Vi/Vim?

Count Lines with sed Command

The sed command is the alternative command to the awk which provides the very same features. The sed command can be used to print the count of the lines in a file.

sed -n '$=' myfile.txt

Count Lines with cat Command

The cat is popular command used to print specified file into the standard output. Simply cat prints the contents into the terminal. The -n option enables the line numbers and displays the line numbers for every line. By printing the file line numbers can be printed to the end of file.

cat -n random.c
Count Lines with cat Command

Leave a Comment