DEV Community

Cover image for πŸ“ Head, Tail, Grep β€” The Deadly Trio of Linux Text Processing πŸ”₯ (Bonus: sed sneak peek)
SAHIL
SAHIL

Posted on

πŸ“ Head, Tail, Grep β€” The Deadly Trio of Linux Text Processing πŸ”₯ (Bonus: sed sneak peek)

Welcome back to another command-line deep dive, fellow terminal warriors βš”οΈ! Today, we’re slicing and dicing files using three legendary Linux commands:

head, tail, and grep β€” your data ninjas for filtering, scanning, and previewing files with surgical precision. πŸ§ πŸ”

And hey β€” there's a sneaky bonus at the end πŸ‘€ (sed makes a guest appearance). Let’s dig in!


πŸ”Ή head – The Top Gun πŸ›«

πŸ‘‰ Syntax:

head [options] [file_name]
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Default Behavior:

Shows the first 10 lines of a file.

πŸ§ͺ Examples:

head /etc/passwd
Enter fullscreen mode Exit fullscreen mode

Outputs the first 10 lines of the /etc/passwd file.

head -n 5 myfile.txt
Enter fullscreen mode Exit fullscreen mode

Shows first 5 lines. -n lets you define how many lines.

head -c 20 myfile.txt
Enter fullscreen mode Exit fullscreen mode

Prints the first 20 characters, not lines! βœ‚οΈ


πŸ”Ή tail – The Underdog with Superpowers πŸ•

πŸ‘‰ Syntax:

tail [options] [file_name]
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Default Behavior:

Shows the last 10 lines of a file.

πŸ§ͺ Examples:

tail /var/log/syslog
Enter fullscreen mode Exit fullscreen mode

See the last 10 lines of system logs β€” often where the juicy errors live 😈.

tail -n 3 notes.txt
Enter fullscreen mode Exit fullscreen mode

Just give me the last 3 lines, thanks.

tail -f /var/log/apache2/access.log
Enter fullscreen mode Exit fullscreen mode

Follow log output in real-time β€” this is ✨πŸ”₯ GOLD πŸ”₯✨ when debugging live services. we will talk about this in the end.


πŸ”Ή grep – The Search King πŸ‘‘

πŸ‘‰ Syntax:

grep [options] pattern [file_name]
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ What it does:

Searches for patterns (strings/regex) in a file or input. It's your CLI Ctrl+F on steroids.


πŸ§ͺ Examples:

grep root /etc/passwd
Enter fullscreen mode Exit fullscreen mode

Show lines with the word root in /etc/passwd.

grep -i hello greetings.txt
Enter fullscreen mode Exit fullscreen mode

Case-insensitive search for hello (will match Hello, HELLO, etc.)

grep -v admin users.txt
Enter fullscreen mode Exit fullscreen mode

Show lines that don’t contain the word admin. -v = invert match.

ps aux | grep apache
Enter fullscreen mode Exit fullscreen mode

Find all processes with the name apache in them. Super useful when debugging services.



πŸ” More Grep Options – Master the Match πŸ’₯

Let’s go beyond the basics and unlock the real power of grep.

πŸ§ͺ Useful Options:

grep -i pattern file.txt
Enter fullscreen mode Exit fullscreen mode

Ignore case (so Error, error, and ERROR are all matched).

grep -v pattern file.txt
Enter fullscreen mode Exit fullscreen mode

Invert match β€” show lines that do not contain the pattern.

grep -n pattern file.txt
Enter fullscreen mode Exit fullscreen mode

Show matched lines with line numbers.

grep -r pattern /path/to/dir
Enter fullscreen mode Exit fullscreen mode

Recursively search through all files and folders under the path β€” powerful for project-wide scans! πŸ“πŸ“‚

grep -c pattern file.txt
Enter fullscreen mode Exit fullscreen mode

Count the number of matches (number of lines that contain the pattern).

grep -A 2 -B 2 "error" logfile.txt
Enter fullscreen mode Exit fullscreen mode

Show 2 lines after (-A) and 2 lines before (-B) each match β€” useful for contextual debugging.

grep -E 'foo|bar' file.txt
Enter fullscreen mode Exit fullscreen mode

Extended regex β€” match either foo or bar (acts like egrep).


🎯 Combine Powers – πŸ₯· Ninja Moves

head -n 15 data.txt | grep "error"
Enter fullscreen mode Exit fullscreen mode

From the first 15 lines, only show those with error β€” quick scan!

tail -f /var/log/syslog | grep "disk"
Enter fullscreen mode Exit fullscreen mode

Watch for disk-related logs in real time β€” this is DevOps magic right here πŸ§™β€β™‚οΈ.


πŸ”Έ Bonus: sed – The Silent Slicer 🍰

sed is like that cool silent ninja in the background β€” not flashy but lethal when used right.

πŸ§ͺ Very Basic Example (line slicing):

sed -n '5,10p' file.txt
Enter fullscreen mode Exit fullscreen mode

Print only lines 5 to 10 from the file. Think of this as a more surgical version of head | tail.

Want to delete lines? Replace text? sed's got you. But that's for another spicy article 🌢️.


βœ… TL;DR Cheatsheet

Command Action
head -n 5 First 5 lines
tail -f Follow file in real-time
grep pattern Search lines with pattern
grep -v pattern Show lines without pattern
sed -n 'x,yp' Show only lines from x to y

🐾 Deep Dive into tail -f

The tail -f command isn’t just for reading β€” it’s for live monitoring.

βœ… What it does:

  • Starts by printing the last 10 lines of a file.
  • Then, it keeps the file open and streams any new lines that are added.

Perfect for watching log files or real-time updates!

πŸ§ͺ Examples:

tail -f /var/log/syslog
Enter fullscreen mode Exit fullscreen mode

You'll see new logs appear as they’re written β€” ideal for monitoring services.

tail -f app.log | grep "ERROR"
Enter fullscreen mode Exit fullscreen mode

Only show real-time error logs. Great for live bug hunting πŸ”ŽπŸ’₯

🧠 Pro Tip:

Use Ctrl + C to stop a tail -f process running in the terminal.



πŸš€ Final Thoughts

These three tools are the holy trinity of text processing in the Linux world πŸ§™β€β™€οΈ. Once you’re comfy with head, tail, and grep, you’re already way ahead of the pack.

πŸ’‘ Pro Tip: Start combining these with awk, cut, and sed and you’ll become unstoppable in logs, data crunching, and automation.


πŸ”” Want a full post just on sed or awk next? Drop a comment and let me know πŸ‘‡

Thanks for reading! πŸ’»βš™οΈ


Written by Sahil Sharma – aspiring DevOps ninja πŸ₯· | Linux fanatic 🐧 | Learning in public πŸš€

Top comments (0)