In Linux, all the primary operations are performed from the terminal, and so is the file management. Files can be created, edited, and deleted through the terminal. The number of lines in a file can be counted to analyze the size (vertically) of the file. This post will offer various command line utilities to count lines in a Linux file. The content discussed in the post is provided below:
- How to Count Lines in a Linux File?
- Method 1: Using wc Command
- Method 2: Using grep Command
- Method 3: Using sed Command
- Method 4: Using awk Command
- Method 5: Using nl Command
- Method 6: Using perl Command
- Additional Tip: How to Count the Number of Lines in Real-Time?
How to Count Lines in a Linux File?
While working with files in Linux, we may encounter the need to manage the file length by counting the number of lines. There are several methods to count lines in Linux files; let’s discuss each method.
For example, the following file will be used in the upcoming methods.
Method 1: Using wc Command
The “wc” (words count) command is the most popular way to count the number of lines in a document. This command counts the number of characters, words, and lines, words in a document.
Example 1: Count the Lines in a Single File
To count the total lines in a Linux file “testfile.txt” with the help of the “wc” command, the below command is used:
$ wc -l testfile.txt
The output shows 6 lines, including the blank line in “testfile.txt”.
Example 2: Count Lines in Multiple Files
Similarly, the wc command counts the lines for several files simultaneously. For instance, to find total lines in three files, i.e., “testfile.txt”, “testfile1.txt”, and “testfile2.txt”.
$ wc -l testfile.txt testfile1.txt testfile2.txt
The output represents the total lines (first column), and respective file name (column 2). Moreover, it also calculates the sum of the lines in three files.
Example 3: Count the Number of Lines, Characters, and Words
Using the wc command, you can count the characters, lines, and words in “testfile.txt”, use the following command:
$ wc -lwm testfile.txt
The output shows 6 lines, 9 words, and 58 characters in “testfile.txt”.
Method 2: Using grep Command
The grep command is widely used to search different patterns, words, and lines. The grep command counts all lines, lines with a specific word/pattern, and lines without a specific word or pattern.
Example 1: Count Lines in a File
To count the lines in “testfile.txt”, with the “grep” command, this command is used:
$ grep -c ".*" testfile.txt
Similarly, the below grep command can also be used to count lines in a Linux file:
$ grep -c ^ testfile.txt
Six (6) lines are in testfile.txt.
Example 2: Count Lines That Contain Specific Word
If you want to count lines having a specific pattern/word (text) in them, execute this command:
$ grep -w "text" -c testfile.txt
Two lines have a “text” word.
Example 3: Count Lines That Do Not Have Specific Word(s)
We can count the number of lines in which a specific word is not present. To find the lines which do not have a “text” word/pattern in them, run this command:
$ grep -w "text" -c -v testfile.txt
The output shows four (4) lines that do not have the “text” word.
Method 3: Using sed Command
The “sed”command allows the users to count the lines in a Linux file. For instance, to count the lines in “testfile.txt”, utilize the below command:
$ sed -n '$=' testfile.txt
The output shows the line count is 6.
Method 4: Using awk Command
The awk helps users to manipulate the file data and find the details. For counting the lines in “testfile.txt” with awk, execute this command:
$ awk 'END { print NR }' testfile.txt
The output displays the line count “6”.
Method 5: Using nl Command
The nl (number of lines) command reads the file parameters and displays details on the standard output. To count the “testfile.txt” lines with nl, use the below command:
Note: The nl command counts the non-empty lines only.
$ nl testfile.txt | tail -1 | awk '{print $1}'
It shows five (5) lines are in the testfile.txt, which excludes the empty lines.
Method 6: Using perl Command
The perl command is commonly used for text processing, like getting the desired information from a specific file. The perl command allows us to count the lines in Linux as done for “testfile.txt”:
$ perl -lne 'END { print $. }' testfile.txt
6 lines are present in the testfile.txt.
Additional Tip: How to Count the Number of Lines in Real-Time?
If you want to find the real-time file information after a specified delay, the watch command is used with wc. To count the lines in testfile.txt after a specific time (in this case, 2 seconds), the below command should be used:
$ watch wc -l testfile.txt
The output displays the time interval (2 seconds), file name, timestamp, the file lines are counted and the number of lines present in the file.
That’s the end of the post.
Conclusion
To count the lines in a Linux file, numerous commands are used, including wc (word count), grep, sed, awk, nl (number of lines), and perl. Among these, the “wc” utility is used the most as it offers the number of words and characters. This post has addressed various command line utilities used to count lines in a Linux file.