There might be a situation where it is necessary to maintain the records on the number of lines in a given file. To count the number of lines manually is time-consuming as the file may contain large and complex contents. The Bash shell has several commands that display the number of lines in the provided file. We will share all the commands that are executable in the Bash shell and will help you to accomplish the task of counting the number of lines from the files. Each of these commands returns a status based on how they are executed.
Example 1: Count the Lines in a File in Bash with the WC Command
The most effective and straightforward command to count the lines in a file is wc which stands for “word count”. Here, we provide the wc command in the terminal with the “-l” flag. The flag denotes the lines. The “wc -l” command is specified with the “Bash.sh” file for the number of lines to be counted from that file. When we execute this command, it generates the integer value of “13” along with the file name, “Bash.sh”. The output indicates the total lines of the “Bash.sh” file.
13 Bash.sh
The name of the file is also displayed with the number of counted lines. We can also prevent the file name from being passed as a parameter using the “<” symbol to divert the file’s content to the wc command. Here, we present the “wc -l” command with the “<” symbol and then set the “Bash.sh” file name. When the command is executed in the terminal, it just displays the number of lines that are present in the file.
13
We have another option and that is to apply the pipe (‘|’) symbol to input the method with the cat command to pass the contents of the file to the wc command. In the following, we execute the command where the cat keyword is used with the “Bash.sh” file. Then, we place the pipe “|” symbol before the “wc -l” command. The command returns only the number of lines located in the “Bash.sh” file.
13
Moreover, we can also count the number of lines from multiple files at the same time using the wc command by providing the space in between every file name. Here, we give a command where the wc uses the “-lines” flag with three files that have the same “Bash.sh” name. This command first displays the individual line counts for each file. Then, the total number of lines of all the files is shown at the end with the “total” keyword.
13 Bash.sh
13 Bash.sh
13 Bash.sh
39 total
Example 2: Count the Lines in a File in Bash with the NL Command
There is a technique to count the lines in any file that is rarely implemented. The command is denoted as “nl” which reads the numbered lines from the input and writes them to the standard output. In the following nl command, we set the “Data.txt” file name with the “nl” keyword which returns each line with the number and content of the file.
1 Sunday
2 Monday
3 Tuesday
4 Wednesday
5 Thursday
6 Friday
7 Saturday
The “-nl” command can be used with different options. Here, we first use and employ the nl command with the “Data.txt” file. After that, we use the pipeline “|” symbol and set the “tail” option on the right side of the pipeline symbol. The tail option is specified with the “-n3” value which counts the three lines from the last of the “Data.txt” file. The result of this command outputs the last lines with the input content in the following image:
5 Thursday
6 Friday
7 Saturday
Example 3: Count the Lines in a File in Bash with the Awk Command
Awk is a text-processing command-line tool that is immensely effective. It can be utilized for a variety of operations from which identifying the number of lines is the one. The “awk” command takes the “END {print NR}” parameter. The NR is the number of records that are printed by the END section to specify the lines in the file. The lines that are included in the Bash.txt file can be estimated using the awk command which is listed as follows:
28
Example 4: Count the Lines in a File in Bash with the Sed Command
Next, a method to get the number of total lines in the specified file is using the sed command. The sed is referred to as the stream editor for the Linux system. It mostly performs the search and replaces the operations on the data from the given files or streams. Here, we use the sed command with the “-n” option to count the overall number of lines from the files. The “$=” operator outputs the current line number. Then, we set the “NewFile.txt” file on which the sed command operation is performed.
14
Example 5: Count the Lines in a File in Bash with the Grep Command
The Grep filter reads a file for a specific character pattern and presents all lines that match that pattern. The Grep is an abbreviation for Global Regular Expression Print. Note the following command of Grep where we use the “-e” option to get the total number of lines. The “$” represents the end line of the regular expression and “c” denotes the count option. The grep command is performed over the “LinesFile.txt” file which is passed as an argument. When we run the specific grep file, it outputs the value of counting the lines from the file.
11
Previously, we use the “$” symbol for the end line of the regular expression. At the beginning line of the regular expression, the “^” symbol is deployed over the “$” symbol. The format of the grep command to get the count lines from the start of the regular expression with the output value is displayed in the following:
11
Example 6: Count the Lines in a File in Bash with the While Command
The while loop is another approach to counting the number of lines in the Bash shell. Here is the script of a while loop to get the lines of the file. We set the “count” object with the value of “0”. Then, we invoke the “while” loop which performs the read operation. The do is assigned with the “count=$count+1” condition which states that once every count from the file, its value is incremented by the value of “1”. The done command is used to set the “Bash3.sh” file name and display the number of total lines in the file.
linux@VirtualBox:~$ while read
> do
((count=$count+1))
done <Bash3.sh
echo $count
14
Conclusion
The guide is explained with all the commands which are used to obtain the number of lines from the file. These commands are helpful when working on a large scale where the developers measure the length of their files. Among all the commands for counting the file lines, the wc command is a very easy and mostly deployed method.