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:


    Tutorial details
    DifficultyEasy (rss)
    Root privilegesNo
  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

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:

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig 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 jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

2 comments… add one
  • aref ghobadi Aug 16, 2015 @ 17:42

    tail command display last ten lines

  • emoye10 Jan 6, 2016 @ 14:23

    it did not for me

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.