Enhancing Skills

tail: Display the last few lines of a file

Command: tail

Used to display the last few lines of a file. By default, it shows the last 10 lines.


Sample Command and Output:

$ tail file1.txt
This is the last line of file1.txt.
This is the second to last line.
This is the third to last line.
This is the fourth to last line.
This is the fifth to last line.
This is the sixth to last line.
This is the seventh to last line.
This is the eighth to last line.
This is the ninth to last line.
This is the tenth to last line.

Description:

  • tail file1.txt: Displays the last 10 lines of file1.txt. This is useful for viewing the end of a file, such as logs.

Sample Command and Output:

$ tail -n 5 file1.txt
This is the last line of file1.txt.
This is the second to last line.
This is the third to last line.
This is the fourth to last line.
This is the fifth to last line.

Description:

  • tail -n 5 file1.txt: Displays the last 5 lines of file1.txt. The -n option allows specifying the number of lines to display.

Sample Command and Output:

$ tail -c 20 file1.txt
second to last line.
This is the fifth

Description:

  • tail -c 20 file1.txt: Displays the last 20 bytes of file1.txt. The -c option allows specifying the number of bytes to display instead of lines.

Sample Command and Output:

$ tail -f /var/log/syslog
[Syslog entries are displayed in real-time as they are added]

Description:

  • tail -f /var/log/syslog: Continuously monitors and displays new lines added to /var/log/syslog. The -f option is useful for watching log files in real-time.

Additional Arguments:

  • -q: Suppresses the output of file headers when multiple files are listed.
  • -v: Always outputs the file headers, even when only one file is being displayed.

Sample Command and Output:

$ tail -q file1.txt file2.txt
==> file1.txt <==
This is the last line of file1.txt.
...

==> file2.txt <==
This is the last line of file2.txt.
...

Description:

  • tail -q file1.txt file2.txt: Displays the last 10 lines of both file1.txt and file2.txt without showing file headers for each file. The -q option suppresses file names in the output.


Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.