‘head‘ command is used to read the file from the beginning and the ‘tail‘ command is used to read the file from the ending. How you can use ‘head‘ and ‘tail‘ commands with different options to read the particular portion of a file is shown in this tutorial.
You can use any existing file or create any new file to test the functions of ‘head‘ and ‘tail‘ commands. Create two text files named products.txt and employee.txt with the following content to show the use of ‘head‘ and ‘tail‘ commands.
products.txt
01 HDD Samsung 1TB $70
02 Monitor DELL 15” $60
03 Mouse A4 N/A $05
04 Keyboard Atech Normal $10
05 Scanner HP N/A $50
06 Printer Samsung N/A $100
07 Adapter A4 N/A $10
08 Monitor Samsung 17” $80
employee.txt
S001 John Paul Sales Marketing Officer
S002 Wellium Bob Sales Sales Executive
E003 Jason HR Manager
E004 Jullie HR Assistant Manager
E005 Janifer HR Programmer
Use of Head Command
By default, the ‘head’ command reads the first 10 lines of the file. If you want to read more or less than 10 lines from the beginning of the file then you have to use the ‘-n’ option with the ‘head’ command.
Head Command Syntax
Using the option in the ‘head’ command is optional and the ‘head’ command can be applied for one or more files.
Head Command Options
The purposes of different `head` command options have explained below.
Option | Purpose |
---|---|
-n or –lines | It is used to print the first n number of lines. |
-c or –bytes | It is used to print the first n number of characters or bytes. |
-q or –quiet | It is used to print the content of one or more files without mentioning the filename in the output. |
-v or –verbose | It is used to print the content of one or more files by mentioning the filename in the output. |
Examples of Head Command
Different uses of the `head` command have shown in the following examples.
Example – 1: Head Command Without Any Option
The products.txt file has 9 lines with the heading. So, the following command will display all lines of the products.txt file because no option is used with the ‘head’ command.
Output:
The following output will appear after executing the above command.
Example – 2: Head Command with -n Option and Positive Value
The ‘-n’ option with 5 has been used in the following ‘head’ command to print the first five lines of the products.txt file in the output.
Output:
The following output will appear after executing the above command.
Example – 3: Head Command with -n Option and Negative Value
The ‘-n’ option with -7 has been used in the following ‘head’ command to print the content of the products.txt file after omitting the last 7 lines.
Output:
The following output will appear after executing the above command.
Example-4: Head Command with -c Option
The ‘-c’ option with 67 has been used in the following ‘head’ command to print the first 67 characters of the products.txt file in the output.
Output:
The following output will appear after executing the above command.
Example-5: Head Command with -q Option
The following `head` command with the –q option and two files will print the all content of both files without mentioning the file name.
Output:
The following output will appear after executing the above command.
Example-6: Head Command with -v Option
The following `head` command with the –v option and two files will print the all content of both files by mentioning the file names.
Output:
The following output will appear after executing the above command.
Example – 7: Head Command with -n Option and Multiple Files
The particular number of lines from the beginning of one or more files can be printed by using the -n option and number with the `head` command. The following command will print the first 2 lines of products.txt and employee.txt files.
Output:
The following output will appear after executing the above command.
Use of Tail Command
By default, the ‘tail’ command reads the last 10 lines of the file. If you want to read more or less than 10 lines from the ending of the file then you have to use the ‘-n’ option with the ‘tail‘ command.
Tail Command Syntax
Like ‘head’ command ‘tail’ command is also applicable for multiple files and using the option is optional for the ‘tail’ command.
Tail Command Options
The purposes of different `tail` command options have explained below.
Option | Purpose |
---|---|
-n or –lines | It is used to print the last n number of lines. |
-c or –bytes | It is used to print the last n number of characters or bytes. |
-q or –quiet | It works similar to the -q option of the `head` command. |
-v or –verbose | It works similar to the -v option of the `head` command. |
-f or –follow | It is used to monitor the log entries written by running programs. |
Example-1: Tail Command Without Any Option
The employee.txt file has only 6 lines which are less than 10. So, the following command will display the full content of the employee.txt file.
Output:
The following output will appear after executing the above command.
Example – 2: Tail Command with -n Option and Positive Value
When you want to read particular lines from the ending of the file then you have to use the ‘-n’ option with a positive value. The following command will display the last 2 lines of the employee.txt file.
Output:
The following output will appear after executing the above command.
Example – 3: Tail Command with -n and Negative Value
If you want to omit the specific lines from the beginning then you have to use the ‘-n’ option with a negative value in the ‘tail’ command. The following command will display the content of the employee.txt file by omitting 3 lines from the beginning.
Output:
The following output will appear after executing the above command.
Example – 4: Tail Command with -c Option
The ‘-c’ option with 65 has been used in the following ‘tail’ command to print the last 65 characters of the employee.txt file in the output.
Output:
The following output will appear after executing the above command.
Example – 5: Tail Command with -f Option
The ‘-f’ and ‘-n’ options with the path of history.log have been used in the following ‘tail’ command to print the 3 lines of the history.log file in the output.
Output:
The following output will appear after executing the above command.
Example – 6: Tail Command with -n Option and Multiple Files
The following command will display the last 3 lines of products.txt and employee.txt files.
Output:
The following output will appear after executing the above command.
Example – 5: Using Head and Tail Commands Together
If you want to read the content from the middle of any file then only the ‘head‘ or ‘tail‘ command can’t solve this problem. You have to use both ‘head‘ and ‘tail‘ commands together to solve this problem. The following command will read lines from 2 to 6 of the products.txt file. At first, the ‘head’ command will retrieve the first 6 lines by omitting the last 5 lines for the negative value and the ‘tail’ command will retrieve the last 5 lines from the output of the ‘head‘ command.
Output:
The following output will appear after executing the above command.
Conclusion
I hope, after practicing the above examples, the bash users will be able to apply ‘head‘ and ‘tail‘ commands properly.
To omit NUM lines from the beginning of a file when using the tail command, the correct way is to use tail -n +NUM file.txt as described by the manual page. Your article implies that this is done by using tail -n -NUM file.txt (a negative number).
This will not produce the desired behaviour, and the reason that it appears to work on the screenshot in the article is because it is actually just working as if the negative sign is not even there, i.e. as if you just specified NUM.
That means it is outputting the last 3 lines of the file, but since the file only has 6 lines that is the same as omitting the first 3 lines so this mistake is not immediately apparent unless you use a different number than 3, if you used `-n -2` for example it would only print the 2 last lines instead of the 4 last lines which would be expected if you are trying to omit the first 2 lines in a 6 line file. To correct it the only thing you need to do is change the usage of negative with tail to usage of +.
For the head command, using negative is correct. A negative number is only a mistake when used with the tail command, so references to using negative with head do not need to be changed.
— Lenny