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, andgrepβ 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]
π Default Behavior:
Shows the first 10 lines of a file.
π§ͺ Examples:
head /etc/passwd
Outputs the first 10 lines of the
/etc/passwdfile.
head -n 5 myfile.txt
Shows first 5 lines.
-nlets you define how many lines.
head -c 20 myfile.txt
Prints the first 20 characters, not lines! βοΈ
πΉ tail β The Underdog with Superpowers π
π Syntax:
tail [options] [file_name]
π Default Behavior:
Shows the last 10 lines of a file.
π§ͺ Examples:
tail /var/log/syslog
See the last 10 lines of system logs β often where the juicy errors live π.
tail -n 3 notes.txt
Just give me the last 3 lines, thanks.
tail -f /var/log/apache2/access.log
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]
π 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
Show lines with the word root in
/etc/passwd.
grep -i hello greetings.txt
Case-insensitive search for hello (will match Hello, HELLO, etc.)
grep -v admin users.txt
Show lines that donβt contain the word admin.
-v= invert match.
ps aux | grep apache
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
Ignore case (so
Error,error, andERRORare all matched).
grep -v pattern file.txt
Invert match β show lines that do not contain the pattern.
grep -n pattern file.txt
Show matched lines with line numbers.
grep -r pattern /path/to/dir
Recursively search through all files and folders under the path β powerful for project-wide scans! ππ
grep -c pattern file.txt
Count the number of matches (number of lines that contain the pattern).
grep -A 2 -B 2 "error" logfile.txt
Show 2 lines after (-A) and 2 lines before (-B) each match β useful for contextual debugging.
grep -E 'foo|bar' file.txt
Extended regex β match either foo or bar (acts like
egrep).
π― Combine Powers β π₯· Ninja Moves
head -n 15 data.txt | grep "error"
From the first 15 lines, only show those with error β quick scan!
tail -f /var/log/syslog | grep "disk"
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
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
You'll see new logs appear as theyβre written β ideal for monitoring services.
tail -f app.log | grep "ERROR"
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)