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

The Geek Diary

CONCEPTS | BASICS | HOWTO

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • Linux Services
    • VCS
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
    • Data Guard
  • DevOps
    • Docker
    • Shell Scripting
  • Interview Questions
  • 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. Linux Interview Questions – Basic File and Directory Permissions
  2. How To Create a Local Yum Repository for MySQL Enterprise Packages
  3. CentOS / RHEL : iptables troubleshooting guide
  4. CentOS/RHEL: How to find the package with a missing file using YUM
  5. How to create partitions inside loopback device
  6. How to recover from deleted root entry in /etc/shadow and/or /etc/passwd files in CentOS / RHEL 6
  7. Understanding /proc/meminfo file (Analyzing Memory utilization in Linux)
  8. How to Increase the File Download Size Limit in Apache
  9. CentOS / RHEL : How to Set up SFTP to Chroot Jail only for Specific Group
  10. CentOS / RHEL 7 : Lock User Account After N Number of Incorrect Login Attempts

You May Also Like

Primary Sidebar

Recent Posts

  • How to Disable IPv6 on Ubuntu 18.04 Bionic Beaver Linux
  • How to Capture More Logs in /var/log/dmesg for CentOS/RHEL
  • Unable to Start RDMA Services on CentOS/RHEL 7
  • How to rename a KVM VM with virsh
  • Archives
  • Contact Us
  • Copyright

© 2021 · The Geek Diary