Linux: Display First Ten Lines (1-10) With Numbers Command

I‘m a new Linux user. I have various text files. I need to show first ten lines of each file. How can I display 1-10 line numbers in Linux or Unix-like system using shell prompt?

You need to use the combinations of the following Linux or Unix-like system commands:


  1. sed command
  2. cat command
  3. less command
  4. awk command
  5. mapfile command (Bash v4.x only)

To display 1 to 10 lines from /etc/passwd file and number all output lines, enter:

sed -n "1,10p" /etc/passwd | cat -n

Sample outputs:

Fig.01: Linux/Unix: Bash/KSH SHELL Print nth Line Of a Text File

In this example, print 50-200 lines from demo.txt and number all output lines using less command:

sed -n "50,200p" /etc/passwd | less -N

The following example use the head command to display first 10 lines of /etc/passwd file:

head +10 /etc/passwd | cat -n

Sample outputs:

     1	==> /etc//passwd <==
     2	root:x:0:0:root:/root:/bin/bash
     3	bin:x:1:1:bin:/bin:/sbin/nologin
     4	daemon:x:2:2:daemon:/sbin:/sbin/nologin
     5	adm:x:3:4:adm:/var/adm:/sbin/nologin
     6	lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     7	sync:x:5:0:sync:/sbin:/bin/sync
     8	shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     9	halt:x:7:0:halt:/sbin:/sbin/halt
    10	mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    11	uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

Display 10-20 lines using head and tail combo as follows:

head -20 /etc/group | tail -n 10 | cat -n

Sample outputs:

     1	nobody:*:-2:
     2	nogroup:*:-1:
     3	wheel:*:0:root
     4	daemon:*:1:root
     5	kmem:*:2:root
     6	sys:*:3:root
     7	tty:*:4:root
     8	operator:*:5:root
     9	mail:*:6:_teamsserver
    10	bin:*:7:

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.