Quick file viewing utilities: show the end of a file Which command displays the last 10 lines of a file by default?

Difficulty: Easy

Correct Answer: tail

Explanation:


Introduction / Context:
Log inspection and troubleshooting often require viewing the most recent lines of a growing file. Unix provides a simple utility that, by default, shows the last 10 lines and can follow a file as it grows in real time, making it indispensable for monitoring logs and long-running processes.



Given Data / Assumptions:

  • We want to see the last few lines of a text file.
  • Default behavior is acceptable (10 lines).
  • Standard Unix command set is available.


Concept / Approach:

The tail command prints the end of files. With no options, it outputs the last 10 lines. Adding -n changes the number of lines, and -f follows the file, printing appended data in real time, which is ideal for tailing logs during service restarts or deployments.



Step-by-Step Solution:

Run tail /var/log/some.log to see the last 10 lines.Use tail -n 50 file to see the last 50 lines.Use tail -f file to follow new lines as they are written.Combine with grep: tail -f file | grep pattern for live filtering.


Verification / Alternative check:

Compare with head (first lines) to understand the difference. cat shows the whole file, which is inefficient for very large logs when only the end is needed.



Why Other Options Are Wrong:

  • grep: searches text; it does not specifically show the last lines.
  • head: shows the first lines, not the last.
  • cat: dumps entire file to standard output.
  • None of the above: incorrect because tail fits perfectly.


Common Pitfalls:

Forgetting -n when a different count is required, using tail -f without stopping it in scripts, and tailing the wrong rotated log file. Use tail -F to handle log rotations gracefully.


Final Answer:

tail

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion