Displays the head of the file and the specified lines from the head.

NAME
     head -- display first lines of a file

SYNOPSIS
     head [-n count | -c bytes] [file ...].

github

  • The file in jupyter notebook format on github is here.

google colaboratory

  • To run it in google colaboratory here /head_nb.ipynb)

environment

The author’s OS is macOS, and the options are different from those of Linux and Unix commands.

If you want to run the command, you need to use the leading ! and %%bash in the first line.

!sw_vers
ProductName: Mac OS X
ProductVersion: 10.14.6
BuildVersion: 18G2022
!bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.

Example usage

The default setting with no options prints the first 10 lines.

%%bash
echo "Create text for testing"
echo -e "1 \n2 \n3 \n4 \n5 \n6 \n7 \n8 \n9 \n10 \n11 \n12" > temp
head temp
echo "Only the first 10 lines are displayed."
Create the text for testing
1
2
3
4
5
Six.
Seven.
8
9
10
Only the first 10 lines will be displayed.

Typical options

  • c : Display the specified number of bytes from the top
  • n : Display the specified number of lines from the beginning ⇒ Most frequently used option

n option

%%bash
echo -e "test \ntest \ntest \ntest \ntest \ntest \ntest \n" > test
echo "Display the first three lines"
head -n 3 test
Display the first three lines
test
test
test