How to Use the head and tail Commands for Text Processing on Linux
There are many Linux commands and tools used to process text files. But there are times when you do not want to read the whole content of a file, but rather a specific part of it. Do you know that you can use the head and tail commands on Linux to output the beginnings and ends of a file respectively?
Read on to discover how you can use these two commands to effectively process and manipulate text on Linux.

What Is the head Command?
The head command is used to print out the initial parts of a file. It reads the files from the beginning. If you have a file that has over a thousand lines, it would be very cumbersome to open it and read. You can easily just print out a few lines from the top using the head command.
The head Command Syntax
The basic syntax of the head command is:
There are many options available to use alongside the head command, some of which would be discussed later. Toget command-line helpregarding the head command, check its manual page by running:

How to Use the head Command
For this example, create a file:numbers.txt. In the file, list the numbers one to 20 in words. it’s possible to choose to use any file of your choice, but make sure that it has at least 11 lines.
Using the Default head Command
By default, the head command prints out the first 10 lines of text in a file. Try it out with the numbers.txt file by executing this command in your terminal:
It will print out the first 10 lines of the file:

If the file has less than 10 lines, the head command will print all the lines present.
Print the First N Number of Lines
you’re able to use the head command to print a specific number of lines instead of the default 10. To print the first three lines of the numbers.txt file, execute this command:
The result:

Exclude the Last N Lines Using the head Command
Just like you can print out the first lines of a text, you can also decide to exclude the last N lines when printing. You can do this by using a negative number for the N parameter.
To exclude the last 15 lines of the numbers.txt file, run:

Print the First N Characters of a File
The head command also has the option to print the first number of characters or bytes in a file. You can do this by using the-coption. To print out the first 10 characters, use:
What Is the tail Command?
As the name implies, the tail command prints the last lines of a file. It reads the files from the end and outputs the ending lines.
The tail Command Syntax
The basic syntax of the tail command is:
For more information on the tail command, check its manual page by issuing:
How to Use the tail Command
We shall be using the already created numbers.txt file for the following examples.
Using the Default tail Command
The tail command outputs the last 10 lines of a file when used without any special options. For example:
The output will display the last 10 lines as mentioned:
Print the Last N Number of Lines
In a situation where you do not want to print the last 10 lines, but a specific number, you can use the-noption to achieve that. To print the last four lines of the numbers.txt file, execute this command:
Print Lines After a Specific Line
If you want to start from a particular line N, you can use the-noption alongside a positive number to achieve that. To print the output of the numbers.txt file from the 17th line, execute this command:
Print the Last N Characters of a File
Just like the head command, you can print the last few characters in the file by using the-coption. To print the last 10 characters of the numbers.txt file, execute this command:
Using the head and tail Commands With Multiple Files
you may also use head and tail to print more than one file at a time. This is the syntax for using the commands with multiple file inputs:
Using the head and tail Commands Together
you’re able to even use head and tail in the same command using the pipe symbol. The pipe symbolredirects the output of one commandas an input to another.
For example, to get the sixth, seventh, and eighth lines, you can execute this command:
In the aforementioned one-liner, the head command displayed the first eight lines of the file, then the output was passed on to the tail command, which printed the last three lines of the redirected output.
head and tail: Helpful Text Manipulation Commands
Linux offers lots of commands to help you effectively manipulate and process text files, and the head and tail commands are just two of many. The most common text manipulation commands include grep, uniq, sort, sed, awk, etc. Each of them fulfills a different purpose with its unique feature set.
Apart from head and tail, there are tons of other text and file manipulation commands available for Linux users.
With a command-focused OS like Linux, you don’t need a graphical interface to manage your files. The terminal has got everything you need.
Obsidian finally feels complete.
Windows is great, but adding this makes it unstoppable.
These are the best free movies I found on Tubi, but there are heaps more for you to search through.
When your rival has to bail out your assistant.
If an AI can roast you, it can also prep you for emergencies.