Disclosure: This article may contain affiliate links. When you purchase, we may earn a commission.

10 Examples of head and tail command in Linux

Hello guys, if you are wondering how to use head and tail command in Linux then you have come to the right place. Both head and command are great tool to view content from files in Linux. As the name suggest, "head" is used to view the top portion of the file and "tail" is used to see the file content from bottom. For example when you say "head data.json" then it will display first 10 lines of the file and when you say "tail data.json" then it will display last 10 lines of the file. As a Java developer, I find myself using tail more often than head while checking logs. As you can use tail to see the last few lines of the log file and you can also use tail -f application.log to see the logs rolling in real time. I often use this command to track request and see what application is doing at a particular time. 


In the past, I have shared examples of essential commands like the grep command, find command, curl, netstat, nslookup, lsof and more and In this article, You will be learning how to use the head and tail commands in Linux. What is the head command? think of it as something at the top or above. Even as the name implies. same thing as the tail too, something below.

In Linux, when you use a head command, basically you are calling some set of data or information that is at the top. you would often work with a text file and manipulate it. You could specify the numbers of the line you want to see in the text file. let's say for instance we have a text file with a list of information inside. 

If I call the head command on the text it will give me the top ten, and the reason is that I did not specify a particular number of lists it should bring. so the default number is 10. I hope this is clear, So we can now move to the different ways or examples of using the head.


How to use head and tail command in Linux

Here are example of how to use head and tail command in Linux. I have tried to cover as many as possible head and tail options so that you can effectively use this command for viewing files in Linux. 

1. To Bring out the top information

$ head sample.txt

So, in the text file "sample.txt" it is going to bring out the first 10 items. Here is an example where you can see the first 10 lines of a json file which contains more than 2833 lines

10 Examples of head and tail command in Linux


2. To Bring out the list with the file name

$ head -v sample.txt

This command written above is somewhat similar to the first one. The only difference is that we the option -v is used with it. Using this command you would still have the top 10 but the name of the file would be seen above first, then the top 10 information


3. To print out a specified number of lines of information

$ head -n 6 sample.txt

This simply means that there will be a list of the first six pieces of information in the sample.txt file. The difference between this and the first command provided is that the particular command brings the first 6 while the first one brings the first 10. you could specify different types of numbers based on what you need.


4. To print a specific number of bytes

$ head -c 3 sample.txt

This is similar to the previous command before this. the difference is in the option. this time around it is an option -c that enables us to do that.


5. To output multiple files

We have been working with a demo file before now named sample.txt. Let us say for instance another file has been loaded namely "sample2.txt". So we want to output these 2 files, see the command below:

$ head -n 5 sample.txt  sample2.txt

The command will result in displaying two different files. you can always output as many files as you want, you only need to separate them by spaces. Don't forget this command will give us the first 5 lines of each file respectively.

Now at this point. The tail Command shall be explained. the head command was explained initially. Tail is the opposite of head if you call the tail command on a text file, this will bring out the last 10 lines. let's see


6. To display lines of information using tail

$ tail sample.txt

this brings  the last 10 lines, remember the default is 10


7. To bring out the specified number of lines

$ tail -n 5 sample.txt

The command above would bring out the last 5 lines



8. To output multiple files.

Given that we have two files already, let's take for instance demo1 and demo2 respectively. If you want to output both files, then you would do the following

$ tail demo1.txt  demo2.txt

In this case, it is going to output the last 10 lines of both files one after the other. you could specify more if that is what you need.



9. To print a file with its name.

$ tail -v sample.txt

This command will result in having the last 10 lines with the name of that file



10. Using the tail and head together.

There could be instances you may want to use both the head and tail together. let's say you need the first 5 information and the last 5 information. here is the command for it

$ head -n 5 sample.txt | tail -n 5


That is all for this article Here you learned about the head command and the things you could do with it and also the tail command respectively. then lastly the usage of both commands at the same time. Note that the head and tail command basically has to do with manipulating text files. It is paramount that you already have those text files you want to manipulate loaded on your system.

Other Linux Articles and Resources you may like
  • 10 Linux command line courses for Beginners (courses)
  • 5 Example of kill commands in Unix and Linux (example)
  • 10 examples of Networking commands in Unix (nslookup)
  • How to set up cron jobs in Linux (Crontab example)
  • VI Editor examples and tips for beginners (vi examples)
  • 10 examples of lsof command in Linux? (examples)
  • How does the nslookup command work in UNIX? (answer)
  • How to use the netstat command to find which process is listening on a port? (example)
  • 7 Best Linux Courses for DevOps Engineers (Linux courses)
  • Linux find + du + grep example (example)
  • 10 Examples of curl command in Linux (cURL)
  • 10 Examples of chmod command in Linux (chmod)

Thanks for reading this article so far. If you like this article and my explanation of sending an email from a Linux machine then please share it with your friends and colleagues.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.