head and tail commands go by their name.
head is used to display the first few lines of the file while tail is used to display the last few lines.
UNDERSTANDING head
head [OPTION]… [FILE]…
The arguments in the square brackets are optional. So, that means it is not necessary for you to provide options or file name.
- If in case you don’t provide the option, but do provide the file name, it prints the first ten lines to the standard output.
- If you do provide the option but no file name, you are redirected to the standard input and are asked to give input. But, in this case it does not matter what option you have provided because the moment you enter the input and press enter, your input is displayed.
- If you provide both the option and the filename, the number of lines provided in input are displayed from that file.
Let us understand head, using a file, practice1.
The content of the file practice1 are –
1. avantika
2.anamikc
3. avinash
4.abhijeet
5.aniket
6.akansh
7.abhilasha
8.animesh
9.arun
10.arpita
11. Arman
12.Ankit
13.Anita
head -2 practice1
1. avantika
2.anamikc
Understand – This command gives the first two lines.
head practice1
1. avantika
2.anamikc
3. avinash
4.abhijeet
5.aniket
6.akansh
7.abhilasha
8.animesh
9.arun
10.arpita
Understand – Since you have not provided any option, by default this command prints the first ten lines of the file.
head +4 practice1
There is no option like + in head. The output is like –
head: cannot open `+3′ for reading: No such file or directory
==> practice1 <==
and then it displays the first ten lines of the file.
You can even print the characters using the head command.
head -c23 practice1
This command prints the first 23 characters from the file practice1.
1. avantika
2.anamikc
head
this command simply takes input from you and prints it in then next line.
head
avantika
avantika
Note-: Providing an option is of no use, since no matter what, if you write simply head, after providing the input, the moment you press enter, in the next line it gets printed.
_______________________________________________________________________
head file1 file2
In this case, the first ten lines of both the files will get printed preceding the file name.
Expected Output –
==> file1 <==
content of file1(first ten lines)
==>file2 <==
content of file2(first ten lines)
_______________________________________________________________________
head -3 practice1 avantika
This command will print the first three lines of both the files.
==> practice1 <==
1. avantika
2.anamikc
3. avinash
==> avantika <==
we are trying to do something
let us hope that I am able to do it.
ERROR- If you want to find five lines of first file and seven lines of file2, this is not possible.
So, writing something like
head -3 practice1 -4 avantika will cause error.
UNDERSTANDING tail
tail [OPTION]… [FILE]…
This command is very similar to the head except for the fact that it prints the last few lines.
Now, let us understand it using some examples.
tail practice1
This command prints, by default, the last ten lines of the file.
4.abhijeet
5.aniket
6.akansh
7.abhilasha
8.animesh
9.arun
10.arpita
11. Arman
12.Ankit
13.Anita
_________________________________________________________________________
tail -4 practice1
This prints the last four lines of the file
10.arpita
11. Arman
12.Ankit
13.Anita
_______________________________________________________________________
tail +6 practice 1
This prints all the lines starting from the sixth line
6.akansh
7.abhilasha
8.animesh
9.arun
10.arpita
11. Arman
12.Ankit
13.Anita
________________________________________________________________
tail -c33 practice1
This prints the last 33 characters of the file practice1
pita
11. Arman
12.Ankit
13.Anita
_____________________________________________________________________
tail +c23 practice1
Unlike the other + option, this gives error.
tail: 3: invalid suffix character in obsolescent option
Happy Learning 🙂

Leave a comment