• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to Count lines in a file in UNIX/Linux

by admin

Question: I have a file on my Linux system having a lot of lines. How do I count the total number of lines in the file?

Using “wc -l”

There are several ways to count lines in a file. But one of the easiest and widely used way is to use “wc -l”. The wc utility displays the number of lines, words, and bytes contained in each input file, or standard input (if no file is specified) to the standard output.

The syntax is:

# wc -l [filename]

So consider the file shown below:

$ cat file01.txt
this is a sample
file
with
some sample
data

1. The “wc -l” command when run on this file, outputs the line count along with the filename.

$ wc -l file01.txt
       5 file01.txt

2. To omit the filename from the result, use:

$ wc -l < file01.txt
       5

3. You can always provide the command output to the wc command using pipe. For example:

$ cat file01.txt | wc -l
       5

You can have any command here instead of cat. Output from any command can be piped to wc command to count the lines in the output.

Using awk

If you must want to use awk to find the line count, use the below awk command:

$ awk 'END{print NR}' [filename]

Example output:

$ awk 'END{print NR}' file01.txt
5

Using sed

Use the below sed command syntax to find line count using GNU sed:

$ sed -n '$=' [filename]

Example output:

$ sed -n '$=' file01.txt
5

Using grep

Our good old friend "grep" can also be used to count the number of lines in a file. These examples are just to let you know that there are multiple ways to count the lines without using "wc -l". But if asked I will always use "wc -l" instead of these options as it is way too easy to remember.

$ grep -c ".*" [filename]

Example Output:

$ grep -c ".*" file01.txt
5

With GNU grep, you can use the below grep syntax:

$ grep -Hc ".*" [filename]

Here is another version of grep command to find line count.

$ grep -c ^ file01.txt
5

Example Output:

$ grep -Hc ".*" file01.txt
file01.txt:5

Some more commands

Along with the above commands, its good to know some rarely used commands to find the line count in a file.

1. Use the nl command (line numbering filter) to get each line numbered. The syntax for the command is:

$ nl [filename]

Example output:

$ nl file01.txt
     1	this is a sample
     2	file
     3	with
     4	some sample
     5	data

Not so direct way to get line count. But you can use awk or sed to get the count from last line. For example:

$ nl file01.txt | tail -1 | awk '{print $1}'
5

2. You can also use vi and vim with the command ":set number" to set the number on each line as shown below. If the file is very big, you can use "Shift+G" to go to the last line and get the line count.

How to Count lines in a file in linux

3. Use the cat command with -n switch to get each line numbered. Again, here you can get the line count from the last line.

$ cat -n file01.txt
     1	this is a sample
     2	file
     3	with
     4	some sample
     5	data
$ cat -n file01.txt | tail -1 | awk '{print $1}'
5

4. You can also use perl one lines to find line count:

$ perl -lne 'END { print $. }' file01.txt
5

Filed Under: Linux

Some more articles you might also be interested in …

  1. How to use command redirection under Linux
  2. “mount.nfs: access denied by server while mounting” – how to resolve
  3. 10 Sed (Stream Editor) Command Examples
  4. How to Read Audit Log in Linux
  5. passwd: gkr-pam: couldn’t update the login keyring password: no old password was entered
  6. How to remove the multipath device after unmapping the storage LUN from server
  7. How to check failed or bad login attempts in Linux
  8. How To Configure Restricted Bash Shell in Linux
  9. How to configure the logging of failed login attempts for vsftpd
  10. How to work with multiple java versions under Linux

You May Also Like

Primary Sidebar

Recent Posts

  • grpck command – Remove corrupt or duplicate entries in the /etc/group and /etc/gshadow files.
  • xxd command – Expressed in hexadecimal form
  • sesearch: command not found
  • macof: command not found

© 2022 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright