Linux/Unix: Cat Command Display Line Numbers

How do I use the cat command to display the line numbers for a file called myapp.c under Linux or Unix like operating systems?

You can use the cat command concatenate files and show on the screen under Linux or Unix like operating systems.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements cat command on Linux or Unix
Est. reading time 2 mintues
The cat command also number all output lines starting with number one with the following syntax:

Syntax For Cat Command To Display Line Numbers

The syntax is as follows:
cat -n fileNameHere
cat --number foo.c

We can use the more command/less command as filter when text can not be fitted on the screen:
cat --number foo.c | more
cat --number foo.c | less

Displaying line number using cat command

The -b / --number-nonblank option number all nonempty output lines, starting with one and the syntax is:
cat -b fileNameHere
cat --number--nonblank filename

Removing empty output while showing line numbers

Finally, you suppress or remove repeated empty output lines with the -s / --squeeze-blank option:
cat -s -n fileNameHere
cat -s -n /etc/resolv.conf

Sample outputs:

     1	#
     2	# Mac OS X Notice
     3	#
     4	# This file is not used by the host name and address resolution
     5	# or the DNS query routing mechanisms used by most processes on
     6	# this Mac OS X system.
     7	#
     8	# This file is automatically generated.
     9	#
    10	search
    11	nameserver
    12	nameserver
    13	nameserver

OR (GNU only syntax)
cat --squeeze-blank -n filename

Say hello to nl command

Use the nl command number lines of files under Linux or Unix oses. The syntax is:

nl filename


Create a text file called hello.c as follows:

/* Purpose: Sample see program to print Hello world on stdout 
 * Author: nixCraft
 * Copyright: None / Copyleft
/* our main */
int main(void){
	printf("Hello world\n");
	return 0;

Use the cat or nl command to display line numbers:

cat -n hello.c
nl hello.c

Fig.01: Displaying line numbers for hello.c using the cat and nl command.

A note about sed

To just print 3rd line use the sed command:
sed -n 3p /etc/resolv.conf
To just print 3rd and 5th line use the sed command:
sed -n -e 3p -e 5p /etc/resolv.conf
To see specific range, say show lines between 3 to 5, run:
sed -n 3,5p /etc/resolv.conf

Summing up

You learned various commands that can display line numbers on Linux or Unix-like systems. See the following man page:
man cat
man nl
man sed

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 3 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
3 comments… add one
  • Manish Jun 27, 2014 @ 8:56

    Hi Thanks for sharing … Its of great help …
    And I m a great fan of your … because I found 90% of what i think on cyberciti.

    Just a little correction here in this post ..
    “cat –number–nonblank filename ”
    is giving error
    cat: unrecognized option ‘–number–nonblank’
    Try ‘cat –help’ for more information.

    Just make it
    “cat –number-nonblank filename”
    a single hyphen – after number

  • tahi Sep 2, 2014 @ 5:04

    Thank you!

    But we had a trouble. “cat” command does not stop display when the text is full screen.
    So, we have any other option to resolve this in-convenience.


  • ojas May 18, 2015 @ 10:09

    The standard output could instead be redirected using a pipe (represented by a vertical bar) to a filter (i.e., a program that transforms data in some meaningful way) for further processing. For example, if the file is too large for all of the text to fit on the monitor screen simultaneously, as is frequently the case, the text will scroll down the screen at high speed and be very difficult to read. This problem is easily solved by piping the output to the filter less, i.e.,

    cat file1 | less

    This allows the user to advance the contents of the file one screenful at a time by pressing the space bar and to move backwards by pressing the b key. The user can exit from less by pressing the q key.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum