head and tail command in Linux with examples

Hello everyone In this Article we are going to learn how to view the content of a files in Linux using head & tail command in a very simple and easy step-by-step approach

head and tail command in Linux with examples

head and tail command in Linux with examples

The head and tail commands are also used to display the content of the file

The head/tail commands only show 10 lines from top and bottom by default. This helps us for more readability of reading the file content.

Before Jumping on the command we should know the syntax of the head & tail command

  • head [Option]… [File]…
  • tail [Option]… [File]…

1) How to View 10 Lines from the top of a file

The head command will display 10 lines from the top.

$ head /etc/passwd 
Screenshot 1 35

2) How to View 10 Lines from the bottom of a file

The tail command will display 10 lines from the bottom.

$ tail /etc/passwd
Screenshot 3 34

3) How to View the Content Of a File in Number Format

To see the output in number format then we have to manipulate the head/tail command and we can get the output in with the line numbers

$ head /etc/passwd | cat -n
Screenshot 4 32
$ tail /etc/passwd | cat -n
Screenshot 5 32

4) How to View the Content of a File other than default value

To view the first 5 lines from the top or the last 5 lines from the bottom then we can use the ( -n ) option in the head/tail command.

$  head -n5 /etc/passwd
Screenshot 8 24
$ tail -n5 /etc/passwd 
Screenshot 9 26

5) How to view the Lines from the file in customized number format

To view the Lines in the customized format then we have to manipulate the head/tail command. Here we are viewing the Files from line number 21-25

$ head -n 25 /etc/passwd | cat -n | tail -n 5
Screenshot 10 24

6) How to check version Of the head/tail command 

To check what is the version of the head/tail command then we can execute the below command

$ head --version
Screenshot 11 25
$ tail --version
Screenshot 12 21

7) How to View the Most useful Options in the head/tail command 

To know the most useful options with the head/tail command then we can execute the below command.

$ head --help
Screenshot 13 19
$ tail --help
Screenshot 14 12

Conclusion

If you want to learn Linux in the most simple language then please click on this link.

If you want to see the man page of head command online then click on this link

If you want to see the man page of tail command online then click on this link

This is the complete knowledge of how we can view the content of a file using head/tail command in the Linux kernel. if you have any doubts please feel free to comment below. Please don’t forget to join our email subscription to get the latest updates on the DevOps Articles

Leave a Comment