Unix / Linux: Show First 10 or 20 Lines Of a File

I have a directory with many files. I am trying to write a shell script that will only show first 10 or 20 lines of a file. How do I display first 10 or 20 lines of a file on Linux or Unix-like systems?

You can use any one of the following command on Unix or Linux to view first 10 lines of a file:

ADVERTISEMENTS

  1. head command
  2. [donotprint]
    Tutorial details
    Difficulty Easy (rss)
    Root privileges No
    Requirements None
    Time 1m
    [/donotprint]
  3. sed command
  4. awk command
  5. Perl/Python/Php/Ruby

head command example to print first 10/20 lines

Type the following head command to display first 10 lines of a file named “bar.txt”:

head -10 bar.txt

Type the following head command to display first 20 lines of a file named “bar.txt”:

head -20 bar.txt

How to view the beginning of text file with head command

Pass the -n option, as follows to control the Length of output:
head -n 5 /etc/passwd
You can view multiple files too:
head file1 file2 file3
head -2 /etc/passwd /etc/hosts

It is also possible to use the shell pipe i.e. use head command to filter the output of commands or files:
command1 | head
command1 | head -n 5
ls -l /etc/ | head -n 5

You can store output to the shell variable too:
file=$(ls -1 -t | head -1)
cp "$file" ~/network-share/backups/

sed command example to print first 10/20 lines

Type the following sed command to display first 10 lines of a file named “/etc/group”:

sed -n 1,10p /etc/group

Type the following sed command to display first 20 lines of a file named “/etc/group”:

sed -n 1,20p /etc/group

awk command example to print first 10/20 lines

Type the following awk command to display first 10 lines of a file named “/etc/passwd”:

awk 'FNR <= 10' /etc/passwd

Type the following awk command to display first 20 lines of a file named “/etc/passwd”:

awk 'FNR <= 20' /etc/passwd
Linux Show First 10 or 20 Lines Of a File

perl command example to view first 10/20 lines of a file

Type the following perl command to display first 10 lines of a file named “/etc/passwd”:

perl -ne'1..10 and print' /etc/passwd

Type the following perl command to display first 20 lines of a file named “/etc/passwd”:

perl -ne'1..20 and print' /etc/passwd

Sample outputs from sed, awk, head, and perl commands:

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

I strongly suggest that you read man pages – awk(1) for more information.

🐧 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 VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
1 comment… add one
  • Chandra Dec 4, 2015 @ 6:32

    Thank you just what i needed to read a few lines from my huge files! Very helpful website. Cheers

Leave a Reply

Your email address will not be published.

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