Skip to main content

Command Palette

Search for a command to run...

Head in Linux OS to display initial data from the file to make understandability easy.

Published
3 min read
Head in Linux OS to display initial data from the file to make understandability easy.
M

Hi, How are you !! Hope you doing good....

I got introduced to Cloud initially. As I went ahead learning what is cloud and how it works, then got to know a field which is DevOps that makes Cloud model more effective.

So, as I started working & got good experience on AWS. I have been learning the DevOps tool and technologies on how to use it with the Cloud, which will give me good understanding on how Cloud and DevOps go hand in hand to deploy my applications.

Last Blog Review

In the last blog we understood, how word count command looks very simple to use. But it can help in hell lot of ways to make the life easy for analyzing any file/folder. It will assist in counting lines, words, characters, bytes and can be used in shell scripts to automate the task as well.

What is head in Linux and how do we use it ???

Head command is used to display top “X” number of lines from the file.

Syntax -

head [arg] [filname] ........

How to use head ??

  1. When we use head w/o any arguments, by default it will display top 10 lines from the file.

  2. Arguments -

A. -n = It’s used to tell head to display specific number of lines from the file from the beginning of the file.

B. -c = It’s used to tell head to display bytes from the file from the beginning of the file.

C. -q = It’s used when we want to display data from more than one file together

D. -v = If we want to display the filename as well in the output

Let’s understand it practically

Scenario -

As a system administrator managing a server running multiple applications, it's crucial to keep track of system resource usage, including CPU, memory, and disk. While tools like top and ps are typically used to monitor processes, the output can often be overwhelming when you're just looking for a quick snapshot. So, please provide the initial data generated from top command which is resource-hungry processes without having to sift through the entire list.

top -n 1 | head -n 5

Explanation:

  • top -n 1: This tells the top command to run only one iteration and then exit.

  • |: The pipe symbol is used to send the output of the top command into the head command.

  • head -n 10: This limits the output to the first 10 lines.

top - 15:51:01 up  7:18,  0 user,  load average: 8.92, 8.26, 7.97
Tasks:  15 total,   1 running,  14 sleeping,   0 stopped,   0 zombie
%Cpu(s): 22.1 us, 20.1 sy,  0.0 ni, 57.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st 
MiB Mem :  64295.2 total,  11750.8 free,  11474.4 used,  41989.1 buff/cache     
MiB Swap:      0.0 total,      0.0 free,      0.0 used.  52820.8 avail Mem

Advantage of using Head -

  1. Saves Time: You don't have to wait for the log file to load in its entirety or open it in a text editor—head provides an instant preview of the most relevant information.

  2. Fast Issue Identification : With head, you can immediately review the start of the log file to detect any issues, such as errors or warnings, without needing to sift through the entire content.

  3. Efficient Troubleshooting: By reviewing the initial lines of the log file, you can easily identify problems that occurred during key events like application startup, allowing you to focus your efforts on resolving the issue efficiently.

Conclusion -

So, here we understood how head command makes it simple to read large file by saving time, to detect issue quickly, and efficient troubleshooting. Along with different arguments which allows to display top lines, bytes. Hope it was great/

💡
That’s a wrap for today’s post! I hope this has given you some valuable insights. Be sure to explore more articles on our blog for further tips and advice. See you in the next post!

More from this blog

Mihir's Tech blog

69 posts