TechBuzzPro

Tail Only Show Lines with Specific Text: A How-To

· 5 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To make tail display only lines containing specific text, pipe its output into grep: tail -f /path/to/logfile | grep "search text". This shows the last few lines of a file and then follows new lines as they are written, filtering so only matching lines appear. You can also use tail -n 100 file | grep "text" for a one-time view of the last 100 lines that match. Nothing needs to be installed on Linux or macOS; on Windows, use Windows Terminal or WSL, since the built-in PowerShell tail alias does not support the same flags.

What You'll Need

  • A terminal or command-line interface (Linux, macOS, WSL, or Windows Terminal with a Unix-like shell).
  • The tail and grep commands, which are standard on Linux and macOS.
  • The path to the file you want to monitor.
  • Basic familiarity with typing commands and pressing Enter.

Step-by-Step

  1. Open your terminal. On Linux, press Ctrl+Alt+T or find Terminal in your applications. On macOS, open Terminal from Applications > Utilities. On Windows, open Windows Terminal and start a WSL session if you need Unix tools. Menu labels can differ slightly by version and region.
  2. Navigate to the file's directory (optional). If you know the full path, you can skip this. Otherwise, use cd /path/to/directory to change into the folder containing your log file.
  3. Run the basic filtered tail command. Type tail -f filename.log | grep "your text" and press Enter. Replace filename.log with your file and your text with the exact string you want to match. The -f flag tells tail to keep the file open and print new lines as they are added.
  4. Watch the output. Only lines containing your search text will appear. New matching lines show up in real time. Press Ctrl+C to stop following and return to the prompt.
  5. For a one-time snapshot, drop -f. Use tail -n 50 filename.log | grep "your text" to show the last 50 lines of the file that contain the text, then exit. Adjust the number after -n as needed.
  6. Make matching case-insensitive if needed. Add -i to grep: tail -f filename.log | grep -i "error". This matches "Error", "ERROR", and "error".
  7. Show line numbers for context. Use grep -n to prefix each matching line with its line number in the original file: tail -f filename.log | grep -n "timeout". This helps you locate the line in an editor.
  8. Limit output to the first match. If you only need the first occurrence, add -m 1 to grep: tail -f filename.log | grep -m 1 "startup complete". The command exits after the first match.
  9. Combine with other filters. You can chain another grep to exclude noise: tail -f filename.log | grep "error" | grep -v "debug". The -v flag inverts the match, hiding lines that contain "debug".
  10. Save the filtered output to a file. Redirect with > or append with >>: tail -f filename.log | grep "warning" >> warnings.txt. The file grows as new matching lines arrive. Stop with Ctrl+C.

Common Mistakes

  • Forgetting quotes around the search text. If your text contains spaces, the shell splits it into separate arguments. Always wrap the pattern in single or double quotes.
  • Using tail -f on a file that is rotated. When a log rotates, tail may keep following the old file. Use tail -F (capital F) to follow the file by name and reopen it after rotation.
  • Expecting grep to match across lines. grep works line by line. A phrase split across two lines will not match.
  • Confusing tail -n with head -n. tail shows the end of the file; head shows the beginning. Use the right one for your goal.
  • Running the command without permission. If you get "Permission denied", you may need to read the file as a different user or adjust permissions. Do not use sudo casually on log files.

Tips

  • Use grep -E for extended regular expressions when you need patterns like error|fail.
  • Pipe through less for scrollable output: tail -n 200 file | grep "text" | less.
  • Add --color=auto to grep to highlight matches in supported terminals.
  • If you only care about new lines, start with tail -n 0 -f file | grep "text" to ignore existing content.
  • On Windows PowerShell, Get-Content -Wait -Tail 10 file | Select-String "text" achieves a similar effect, but the syntax differs from Unix tail.

Troubleshooting

If no output appears, first confirm the file is actually receiving new lines by running tail -f filename.log without grep. If lines appear there but not with grep, check for typos, case sensitivity, or hidden characters in your search string. Try grep -i to rule out case. If you see "tail: cannot open" errors, verify the path and permissions. If the command exits immediately, you may have used grep -m 1 or forgotten -f. If output stops after a log rotation, switch to tail -F. On systems where grep is not available, install it via your package manager or use awk '/text/' as an alternative. For very large files, grep may be slow; consider using rg (ripgrep) if installed, but stick to standard tools when sharing commands.

Frequently Asked Questions

Can I use tail to show only lines that do NOT contain specific text?

Yes. Pipe to grep -v "text" to invert the match and hide lines containing that text.

Does this work on Windows without WSL?

The built-in Windows tail alias in PowerShell is limited. For Unix-style behavior, use WSL or Git Bash, or use PowerShell's Get-Content -Wait -Tail with Select-String.

How do I search for multiple words or patterns?

Use grep -E "word1|word2" for either word, or run separate greps chained with pipes.

Why does my command show old lines as well as new ones?

tail -f prints the last 10 lines by default before following. Add -n 0 to start with no old lines.

Can I make the output timestamped?

Not with tail alone. Pipe through a tool like ts from moreutils, or use awk to prepend a timestamp, but that requires additional setup.

What if the file is being written by another program and I see no new lines?

Check that the program is actually flushing to disk. Some applications buffer output; you may need to wait or adjust the program's logging settings.