Bash Head and Tail Command Tutorial

Bash Head and Tail Command Tutorial
Many types of commands are available in bash to show the content a file. Most commonly used commands are ‘cat’, ‘more’, ‘less’, ‘head’ and ‘tail’ commands. To read the entire file, ‘cat’, ‘more’ and ‘less’ commands are used. But when the specific part of the file is required to read then ‘head’ and ‘tail’ commands are used to do that task. ‘head’ command is used to read the file from the beginning and ‘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. 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.

$ cat products.txt

Run the following command to display the content of employee.txt file.

$ cat  employee.txt

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.

$ head products.txt

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.

$ head -n 5 products.txt

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.

$ head -n -7 products.txt

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.

$ head -n 2 products.txt employee.txt

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.

$ tail employee.txt

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.

$ tail -n 2 employee.txt

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.

$ tail -n -3 employee.txt

Example – 4: ‘tail’ command with multiple files

The following command will display the last 3 lines of products.txt and employee.txt file.

$ tail -n 3 products.txt employee.txt

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.

$ head -n -5 products.txt | tail -n 5

I hope after practicing the above examples, anyone will be able to apply ‘head’ and ‘tail’ command properly.

Related Posts
Leave a Reply

Your email address will not be published.Required fields are marked *