Blog

03/06/2019

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:

  1. head command
  2. [donotprint][/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

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.

Posted by: SXI ADMIN

The author is the creator of nixCraft 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.

14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....
12/08/2019

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...