Linux/Unix: Cat Command Display Line Numbers

by on February 3, 2013 · 2 comments· LAST UPDATED April 27, 2013

in , ,

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
DifficultyEasy (rss)
Root privilegesNo
Estimated completion timeN/A
The cat command also number all output lines starting with number one with the following syntax:

cat -n fileNameHere


cat --number foo.c

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

cat -b fileNameHere


cat --number--nonblank filename

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


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:

l 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

Sample outputs:

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

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

Tweet itFacebook itG+ itDownload PDF versionFound an error/typo on this page?

{ 2 comments… read them below or add one }

1 Manish June 27, 2014 at 8:56 am

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


2 tahi September 2, 2014 at 5:04 am

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.



Leave a Comment

Tagged as: , , , , , , , , , ,

Previous Faq:

Next Faq: