When working with large text files in Linux, it’s often useful to view only the first few lines to get a quick overview of the contents. Fortunately, Linux provides a simple command line tool called head that allows you to display the first few lines of a file. In this article, we’ll explore how to use the head command to find the first five lines of a file in Linux. We’ll start by introducing the head command and its options, then provide step-by-step instructions for using it to view the first five lines of a file. We’ll also cover some practical examples and tips for customizing the output to suit your needs.
let us check below command and try to understand it
1 |
head -n <span class="hljs-tag"><<span class="hljs-name">number</span> <span class="hljs-attr">of</span> <span class="hljs-attr">lines</span>></span> <span class="hljs-tag"><<span class="hljs-name">filename</span>></span> |
The head command is to display the first few lines of a file in Linux is: The -n option is used to specify the number of lines you want to view from the beginning of the file, and <filename>is the name of the file you want to view. For example, to view the first five lines of a file named text.txt (write any content in this file what you want to view), we would use the command:
filename is the name of file which lines you want to read.
Example:
1 |
head -5 test.txt |
This would display the first five lines of the text.txt file in the terminal. You can adjust the number of lines by changing the value after the -n option. If you don’t specify a value for -n, the default behavior is to display the first 10 lines of the file.
Let check out the result as shown below:
hello world this is first line.
hello world this is second line.
hello world this is third line.
hello world this is fourth line.
hello world this is fifth line.
In conclusion, the head command in Linux is a simple and powerful tool for viewing the first few lines of a file. With just a few keystrokes, you can get a quick overview of the contents of a large text file, without having to open it in a text editor. By using the options provided by the head command, you can customize the output to suit your needs, such as displaying a specific number of lines, or including file names in the output. With the knowledge and skills gained from this article, you’ll be well-equipped to use the head command to efficiently view and analyze the contents of text files in your Linux environment.
Please do not forget to see this tutorial CONVERT TEXT INTO THE UPPERCASE IN LINUX USING SHELL PROGRAM