Share your love
8 Ways to Count Lines in a File in Linux Command Line
In this article, you will learn 8 Ways to Count Lines in a File in the Linux Command Line. Sometimes you may want to determine the size of a file and how many lines it has. It may be difficult to find out this, but there are several Linux Commands that you can use to get the number of lines in a file Linux. You can follow this guide to get 8 Linux commands that can be very helpful in this situation.
8 Ways to Count Lines in a File in Linux Command Line with Examples
To complete this guide, you must have access to your Linux server as a root or non-root user with sudo privileges. You can visit the Orcacore website and check the initial server setup guides.
Now follow the steps below to get familiar with these amazing Linux Commands.
Number 1 – WC Linux Command
The first command that you can use to count the number of lines, words, and characters in a file is the wc command. For example, we have a text file named test.txt that has some lines and characters. To get the number of lines, words, and characters in it, we can run the command below:
wc test.txt
The results will look like this:
Output
4 31 171 test.txt
The first number is referred to as the number of lines which is 4 in this example. 31 is the word count. 171 is for the characters, and test.txt is the file you have appended to the command.
As you can see, with this amazing command you can easily get a number of lines, words, and characters in a file.
Also, you can use specific options with the wc command to get your desired count numbers.
To get the number of lines only, you can use the command below:
wc -l < test.txt
Output
4
If you want to count the words, you can use the command below:
wc -w < test.txt
Output
31
For the characters, you can use the following command:
wc -c < test.txt
Output
171
Number 2 – AWK Linux Command Utility
The awk command is mostly used for pattern scanning and processing. You can use this utility to manage the text in different ways. For example, you can count the lines in a file by using the following command:
awk 'END{print NR}' file-name
To get the number of lines in our test file, it is like this:
awk 'END{print NR}' test.txt
Output
4
Number 3 – Perl Linux Command Utility
At this point, you can use the Perl scripting language with the -lne option to print the number of file lines.
For example, we use the following syntax Perl command to count the lines of our test file:
perl -lne 'END { print $. }' test.txt
Output
4
Number 4 – Grep Linux command
As you must know, you can use the grep command to filter a pattern of strings and output them. For example, you can use the -c option in the grep command to count the line number:
grep -c ^ test.txt
Output
4
Also, you can use the following syntax to count every line:
grep -c ".*" test.txt
Number 5 – Sed Linux Command
At this point, you can use the sed command to search, find, replace, insert, and delete texts in Linux. To count the line number you can use the -n option in the following syntax:
sed -n '$=' test.txt
Output
4
Number 6 – Get the Number of Lines with NL Linux Command
Another command that you can use to get the number of lines is the NL command. It assigns numbers to each of the given file’s lines before returning the output. For example, we want to get the number of the lines of the test file:
nl test.txt
The results will be as follows:
Output
1 Counting file lines.
2 The wc, awk, sed, grep, perl, nl, pr, and cat commands.
3 The process is easy with these amazing commands.
4 Linux is so fun and easy to use!
Number 7 – File’s Line Count with PR Linux Command
The PR command is like the NL command. It divides the given file into numbered columns with header, date, and page number. You can use the -n option in the command to get the results:
pr -n test.txt
Output
2023-10-02 06:45 test.txt Page 1
1 Counting file lines.
2 The wc, awk, sed, grep, perl, nl, pr, and cat commands.
3 The process is easy with these amazing commands.
4 Linux is so fun and easy to use!
Number 8 – Count Lines in a File with Cat Linux Command
The cat command is the most useful Linux command. You can use this command to print the number of lines in a file by using the -n option. For example:
cat -n test.txt
Output
1 Counting file lines.
2 The wc, awk, sed, grep, perl, nl, pr, and cat commands.
3 The process is easy with these amazing commands.
4 Linux is so fun and easy to use!
Also, you can use it with another command. For example, to get only the number of lines, you can use it with the awk command:
cat -n test.txt | tail -1 | awk '{print $1}'
Output
4
That’s it, you are done.
How do I count the number of words, lines, and characters in a file in Linux?
To get a full report of a file that how many lines, words, and characters it has, you can use the wc command. As you saw in the guide, it will print all of this information in the Linux command line.
Conclusion
At this point, you have learned to use 8 different Linux commands including wc, awk, Perl, grep, sed, nl, pr, and cat commands to count the number of lines in a Linux file from the command line interface. Hope you enjoy using it.
You may be interested in these articles:
Upgrade PHP Version on AlmaLinux 8 and Rocky Linux 8