11. tail command - muneeb-mbytes/linux_course GitHub Wiki

Tail command:

"tail" command is the complementary of head command. The tail command, as the name implies, print the last 'n' number of data of the given input. By default it prints the last 10 lines of the specified files.

Block_diagram_tail (1)

      Fig-1: These are the variations in "tail" command

Cheat Sheet:

Sr.no cd commands with different variations description
1 tail file_name Prints last n lines of a file (default 10 lines).
2 tail -n Prints last n lines of a file.
3 tail -c -n Prints only n bytes from last, of a file.
4 tail -c +n Prints content after removing n bytes from the top, of a file.
5 tail -q Prints the data without the precedes of file_name.
6 tail file1 file2 Prints the data with the precedes of file_names.
7 tail -v Prints the data with including the file_name.
8 tail --version Gives the version of tail running on system.
9 tail --help Gives all possible variations available in tail.
10 Pipe in tail Application using tail.

Variations of Tail Command:

tail file_name

This command is used to print the last n lines that is by default 10 lines.

In the below Fig-2 you can see that initially all the contents are displayed and while after using the command it is going to print only the last 10 lines so that it started from Telangana and ended at Himachal Pradesh.

tail

      Fig-2: Usage of tail file_name command.

The below GIF shows the whole process from sourcing scripting file till the output.

tail_gif

      GIF-1: giphy for tail file_name command.

GitHub Link: https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_BJT/tail_commands/tail.csh

tail -n file_name

This command prints the last 'n' lines instead of last 10 lines. 'n' is mandatory to be specified in command otherwise it displays an error.

In the below Fig-3 you can see that initially all the contents are displayed and after using the command tail -3 it is going to print only the last 3 lines so that it started from Punjab and ended at Himachal Pradesh.

tail_num

      Fig-3: Usage of tail -n file_name command.

The below GIF shows the whole process from sourcing scripting file till the output.

tail_num_gif

      GIF-2: giphy for tail -n file_name command.

GitHub Link: https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_BJT/tail_commands/tail_num.csh

tail -c -n file_name

This command prints the last 'n' bytes from the file specified. Any newline encounter counts as a single character, so if tail prints out a newline, it will count it as a byte. In this option it is mandatory to write -c followed by positive or negative num depends upon the requirement. By +n, it displays all the data after skipping num bytes from starting of the specified file and by -n, it displays the last num bytes from the file specified.

In the below Fig-4 you can see that initially all the contents are displayed and after using the command and as we used tail -c -20 it is gonna count all the last 20 bytes of the file and print only them.

tail_bytes_neg (1)

      Fig-4: Usage of tail -c -n file_name command.

The below GIF shows the whole process from sourcing scripting file till the output.

tail_bytes_neg_gif

      GIF-3: giphy for tail -c -n file_name command.

GitHub Link: https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_BJT/tail_commands/tail_bytes.csh

tail -c +n file_name

This command prints the last 'n' bytes from the file specified. Any newline encounter counts as a single character, so if tail prints out a newline, it will count it as a byte. In this option it is mandatory to write -c followed by positive or negative num depends upon the requirement. By +n, it displays all the data after skipping num bytes from starting of the specified file and by -n, it displays the last num bytes from the file specified.

In the below Fig-5 you can see that initially all the contents are displayed and after using the command and as we used tail -c +20 it is gonna count all the first 20 bytes of the file and starts printing after the count.

pos_bytes

      Fig-5: Usage of tail -c +n file_name command.

The below GIF shows the whole process from sourcing scripting file till the output.

tail_bytes_pos_gif

      GIF-4: giphy for tail -c +n file_name command.

GitHub Link: https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_BJT/tail_commands/tail_bytes_positive.csh

tail -q file_name1 file_name2 (no precedes)

This command is used if more than 1 file is given. Because of this command, data from each file is not precedes by its file name.

In the below Fig-6 you can see that by using tail -q command it does not print file names but it displays as together two different file contents. File.txt consists of state names and file1.txt consists its's capitals. It continues to print the file2 after the contents of file1 (by default last 10).

tail_quite_no_preced (1)

      Fig-6: Usage of tail -q file_name1 file_name2 command.

The below GIF shows the whole process from sourcing scripting file till the output.

tail_quite_gif

      GIF-5: giphy for tail -q file_name1 file_name2 command.

gitHub Link: https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_BJT/tail_commands/tail_no_preceding.csh

tail file_name1 file_name2 (precedes)

This command is used if more than 1 file is given. Because of this command, data from each file will precedes by its file name.

In the below Fig-7 you can see that by using this command it does print the file names as precedes and displays two different file contents. It first prints the file name1 and all the contents and prints file name2 and all the contents in it (by default last 10).

tail_quite_preced_modified (1)

      Fig-7: Usage of tail file_name1 file_name2 command.

The below GIF shows the whole process from sourcing scripting file till the output.

tail_preceding_gif

      GIF-6: giphy for tail file_name1 file_name2 command.

GitHub Link: https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_BJT/tail_commands/tail_preceding.csh

tail -v file_name

This command is used only if 1 file is given. By using this option, data from the specified file is always preceded by its file name.

In the below Fig-8 you can see that by using this command you can first display the corresponding file name and the contents in it (by default last 10).

verbose (1)

      Fig-8: Usage of tail -v file_name command.

The below GIF shows the whole process from sourcing scripting file till the output.

tail_verbose_gif

      GIF-7: giphy for tail -v file_name command.

GitHub Link: https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_BJT/tail_commands/tail_verbose.csh

tail --version

This command is used to display the version of tail which is currently running on your system.

In the below Fig-9 you can see that by using this command we can see the tail version running on my system.

version (1)

      Fig-9: Usage of tail --version command.

tail --help

This command is used to display all possible variations of tail command.

In the below Fig-10 you can see that by using this command we can see all possible variations of tail command.

tail_help

      Fig-10: Usage of tail --help command.

Pipe in tail

This command is application based usage of tail command.

In the below fig-11 you can see that first head -7 command is used so that it takes first seven lines from the file and piped to tail -3 i.e, the first seven lines are given as input to tail command and tail -3 command takes last three lines from those seven lines and is routed(saved in) to list.txt. Now if you give cat list.txt it will have only last 3 lines.

hi

      Fig-11: Application usage using Head and pipe concept

The below GIF shows the whole process from sourcing scripting file till the output.

tail_application_gif

      GIF-10: giphy for tail application.

GitHub Link: https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_BJT/tail_commands/tail_application.csh

Errors

Please check the syntax before pressing enter button. If the syntax is wrong it will show error as below.

tail_error

      Fig-12: Syntax error of tail command.

Solution

Please look out for spellings and spaces between the words.

The below GIF shows the whole process from sourcing scripting file till the output.

tail_error_gif

      GIF-11: giphy for solution for syntax error of tail command.

To learn history command : https://github.com/muneeb-mbytes/linux_course/wiki/12.-history-command