• 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. pulseaudio: command not found
  2. e2image Command Examples in Linux
  3. How to Integrate CentOS/RHEL system into an AD Domain with LDAP/Kerberos/SSSD
  4. mate-search-tool Command Examples in Linux
  5. aptitude: command not found
  6. mkfs: command not found
  7. cuyo Command Examples in Linux
  8. Linux Boot Process
  9. How to use “btrfs device” comamnd to add/delete device to/from btrfs filesystem
  10. CentOS / RHEL 7 : How to enable or disable automatic updates (via packagekit)

You May Also Like

Primary Sidebar

Recent Posts

  • Chezmoi: A multi-machine dotfile manager, written in Go
  • cheat: Create and view interactive cheat sheets on the command-line
  • chars: Display names and codes for various ASCII and Unicode characters and code points
  • chafa: Image printing in the terminal

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright