Chapter 24: Bash View End (tail)

What does tail actually do? (super simple first)

tail = shows the last part of a file (or from pipe/input).

By default:

  • Shows the last 10 lines of a file
  • Very fast – doesn’t read the whole file, just jumps to the end

Think of it as the opposite of head (which shows the beginning).

Most common real uses:

  • See the latest logs without opening huge files
  • Watch a log file live as new lines are added (-f)
  • Check the last error in a long log
  • Get the last few lines of a report/script output

1. Basic usage (try these now!)

First, let’s create a test file with many lines:

Bash

Now basic commands:

Bash

2. The MOST used option → -f (follow / live tail)

This is why sysadmins love tail:

Bash
  • Shows last 10 lines immediately
  • Then waits and prints new lines in real-time as they are added
  • Like watching live CCTV of your log file
  • Press Ctrl+C to stop

Real example you see every day:

Bash

3. Very common options (you’ll use these 95% of time)

Option What it does Example command Real use case
-n N or -N Show last N lines tail -n 20 error.log Last 20 errors
-f Follow – keep showing new lines tail -f app.log Live monitoring
-q Quiet – no header when multiple files tail -q -n 5 *.log Clean multi-file output
-v Verbose – show filename header tail -v -n 3 *.txt When checking many files
+N Start from line N to end tail -n +50 bigfile.txt Skip first 49 lines
-c N Show last N characters/bytes tail -c 100 short.txt Last part of small file
–pid=PID Stop when process PID dies (with -f) tail -f –pid=$$ log.txt Smart scripts

4. Real-world examples you will use daily

Bash

5. Combining tail with other commands (power moves)

Bash

6. Quick cheat-sheet table

Goal Command example Notes
Last 10 lines (default) tail filename.log Everyday use
Last N lines tail -n 50 error.log or tail -50 error.log Most common
Live monitoring tail -f /var/log/nginx/access.log Watch live
Last N characters tail -c 200 short.txt Small files
Skip first lines (start from N) tail -n +100 bigfile.txt Skip header
Only last line tail -1 file.txt Quick last status
Multiple files quiet tail -q -n 5 *.log No headers
Stop follow when process dies tail -f –pid=$(pgrep myapp) app.log Smart scripts
Tail + grep live errors tail -f log.txt grep -i error

7. Pro tips from daily use

  • tail -f is your best friend when debugging servers/apps
  • Use tail -f | grep instead of opening huge logs in editor
  • tail -n +1 = show whole file (but useless – use cat)
  • For very huge files (GBs) – tail is instant (doesn’t read beginning)
  • In scripts: tail -n 1 to get exit code/message from last command
  • Press Ctrl+C to stop -f, or *Ctrl+* (SIGQUIT) if stuck

Now open your terminal and try these 3 right now:

Bash

Tell me what you see! Or ask:

  • “How to tail last 100 lines and stop after 30 seconds?”
  • “Best way to tail multiple services at once?”
  • “tail vs less – when to use which for logs?”

We’ll build exact commands together! 😄

You may also like...

Leave a Reply

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