You can use any existing file or create any new file to test the functions of ‘head’ and ‘tail’ commands. Here, two text files named products.txt and employee.txt are created to show the use of ‘head’ and ‘tail’ commands.
Run the following command to display the content of products.txt file.
Run the following command to display the content of employee.txt file.
Use of ‘head’ command:
By default, ‘head’ command reads 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 ‘-n’ option with ‘head’ command.
head command syntax:
head [option] [filename]…[filename]
Using option in ‘head’ command is optional. You can apply ‘head’ command for one or more files.
Example – 1: ‘head’ command without any option
products.txt file has 11 lines with heading. The following command will display the first 10 lines of products.txt file because no option is used with ‘head’ command.
Example – 2: ‘head’ command with -n option and positive value
‘-n’ option with 5 is used in the following ‘head’ command. The first five lines of products.txt file will be shown in the output.
Example – 3: ‘head’ command with -n option and negative value
You can use negative value with ‘-n’ option in ‘head’ command if you want to omit the some lines from the file. The following command will omit the last 7 lines from products.txt file.
Example – 4: ‘head’ command with multiple files
You can apply ‘head’ command for reading specific lines of multiple files. The following command will read first 2 lines of products.txt and employee.txt files.
Use of ‘tail’ command:
By default, ‘tail’ command reads 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 ‘-n’ option with ‘tail’ command.
tail command syntax:
tail [option] [filename]…[filename]
Like ‘head’ command ‘tail’ command is also applicable for multiple files and using option is optional for ‘tail’ command.
Example – 1: ‘tail’ command without any option
employee.txt file has only 6 lines which is less than 10. So, the following command will display the full content of employee.txt file.
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 ‘-n’ option with positive value. The following command will display the last 2 lines of employee.txt file.
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 ‘-n’ option with negative value in ‘tail’ command. The following command will display the content of employee.txt file by omitting 3 lines from the beginning.
Example – 4: ‘tail’ command with multiple files
The following command will display the last 3 lines of products.txt and employee.txt file.
Example – 5: Using ‘head’ and ‘tail’ commands together
If you want to read the content from the middle of any file then only ‘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 products.txt file. At first, ‘head’ command will retrieve first 6 lines by omitting the last 5 lines for negative value and ‘tail’ command will retrieve the last 5 line from the output of ‘head’ command.
I hope after practicing the above examples, anyone will be able to apply ‘head’ and ‘tail’ command properly.