Linux / Unix: Display First Line of a File

How do I display first line of a text file called foo.txt on Linux or Unix-like operating systems? You display first lines of a file using head command.

Adblock detected 😱

My website is made possible by displaying online advertisements to my visitors. I get it! Ads are annoying but they help keep this website running. It is hard to keep the site running and producing new content when so many people block ads. Please consider donating money to the nixCraft via PayPal/Bitcoin, or become a supporter using Patreon.

Syntax

The syntax is:

head filename

OR

head -1 filename

Example: Displaying the first line

Open the Terminal application and type the following command:$ head -1 foo.txt The following example will show first 3 lines from /etc/passwd file:$ head -3 /etc/passwd Sample outputs:

A note about sed command

Posted by: Vivek Gite

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.

Your support makes a big difference:

I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft takes a lot of my time and hard work to produce. If everyone who reads nixCraft, who likes it, helps fund it, my future would be more secure. You can donate as little as $1 to support nixCraft: