Mastering the Shell: History, Hacks & Hidden Patterns (Part 3)

Mastering the Shell: History, Hacks & Hidden Patterns (Part 3)

Ever feel like typing commands in the terminal is like déjà vu? Or maybe you’ve seen developers dig through logs or transform text files without even opening them? This post is your cheat sheet to do just that — efficiently, and like a boss.

Let’s dive into some of the most underrated but powerful features of the shell — history tricksregex patternssearch commands, and text replacements. These tools make you faster, smarter, and less error-prone. Trust me, once you start using them, there’s no going back.


🤔 Why Should You Care?

  • ⚡ Save time with shortcuts instead of retyping commands.
  • 🧠 Avoid mistakes by letting your terminal do the remembering.
  • 🔍 Search inside files and logs like you wrote them.
  • ✂ Modify configs or text data without opening a text editor.
  • 🚀 Boost your productivity with just a few commands.

⏪ Shell History & Shortcuts

Recall Previous Commands

Just hit the up arrow — seriously, it’s that easy.

🔍 Search History

You can filter your command history:

history | grep python

🧨 Bang Bang (and Other Bangs)

  • !! → Runs the last command again (called bang bang).
  • !prefix → Runs the last command starting with prefix.
  • !42 → Executes the 42nd command from your history list.

⌨ Create Aliases

Don’t type long commands again and again. Make them short:

alias update='sudo apt-get update'

Want to keep them forever?

  • Add them to ~/.bash_profile for yourself.
  • Or to /etc/profile for all users.

🔡 Regex — Patterns Over Plain Text

Regular Expressions (regex) aren’t a command. They’re a way to describe patterns — and they’re super handy when searching or filtering text.

🎯 Examples of Regex Patterns:

  • . → Any single character
    p.n matches penpinpan...
  • \. → Escape special characters
    test\.test matches the literal text test.test.
  • Anchors:
    ^start → Starts with "start"
    end$ → Ends with "end"
  • Character classes:
    [a-z] → Any lowercase letter
    [0-9] → Any number
  • Quantifiers:
  • ? → 0 or 1 times
  • * → 0 or more
  • + → 1 or more
  • {3} → Exactly 3 times
  • Alternation (OR):
    cat|dog matches either word.

🕵️ Search and Extract Data

🔎 grep — Find Things Fast

grep -i "error" /var/log/app.log
  • -i makes it case-insensitive.
  • Also supports full regex.

🪓 cut — Trim Text Easily

Extract specific fields or columns from a file:

cut -d',' -f2 filename.csv

This cuts out just the second column from a comma-separated file.

🧠 awk — The Power Tool (Coming Soon)

Named after Aho, Weinberger, and Kernighan — it’s a full programming language for structured text processing. We’ll dive into this in a future post.


✍ Modify & Replace Without Opening Files

🧽 sed — Stream Editor

Change content in-place, super fast.

Replace “apple” with “orange” in a file:

sed 's/apple/orange/g' fruits.txt
  • s → substitute
  • g → global (all matches on a line)

Other cool options:

  • d → delete lines
  • p → print
  • n → skip to next line

🔤 tr — Translate or Strip Characters

Uppercase everything:

tr 'a-z' 'A-Z' < input.txt > output.txt

Remove all digits:

tr -d '0-9' < file.txt

🧠 TL;DR

  • Use history!!, and !prefix to avoid typing the same things twice.
  • Create aliases to save effort and reduce errors.
  • Regex helps define search patterns — learn it once, use it forever.
  • grepcutsed, and tr are fast and reliable tools for finding or modifying text.
  • Shell scripting isn’t just power — it’s freedom.

Next Up: We’ll talk about awk, a surprisingly powerful tool that’s like Excel for your terminal. Stay tuned! 👀

Post a Comment

Previous Post Next Post