Read the first line of a file from terminal (on Ubuntu and Mac)

This post introduces how to read the first line of a file from terminal. It works on both Linux (Ubuntu) and Mac OS.

For getting top or bottom 10 files under a director from terminal (on Ubuntu and Mac), check here.

Note: you do not need to install anything, it is built-in on your Ubuntu/Mac OS.

Step 1: open a terminal 

Step 2: cd to the directory where your file (e.g., .txt file) is located 

Step 3: issue the following command

$ head -n 1 example.txt  
# this will read and print the first line of the file in the terminal

$ head -n 2 example.txt 
# this will read and print the first two lines of the file ... you got the idea...

***********************************************

Analogously, see the following command for displaying the last line of a file from terminal:

$ tail -n 1 example.txt # this will read and print the last line of the file in the terminal 

$ tail -n 2 example.txt # this will read and print the last two lines of the file ... you got the idea...

Leave a Reply

Your email address will not be published. Required fields are marked *