Essential Command-Line Skills

💤0
Lv 10 XP
← 🧱 Foundations · Command Line Skills

Essential Command-Line Skills

Beginner ⭐ 50 XP ⏱ 20 min #cli#shell#productivity

The handful of commands and shortcuts that make you fast and dangerous in any terminal.

📖Theory

The command line feels intimidating until you realize most real work uses a tiny core of commands, glued together. Master these categories and you can operate any Unix-like system:

  • Look around: ls, pwd, cat, less, head, tail
  • Find things: find, grep, which
  • Move data: cp, mv, rm, mkdir
  • Inspect the system: ps, top, df, free
  • Combine commands: the pipe | and redirection >

The real superpower is composition: small tools that each do one thing, piped together to answer a question. cat access.log | grep 404 | wc -l counts your 404 errors — three simple tools forming an answer.

🌍Real-World Example

Say a service is misbehaving and you want to see what’s eating memory and find recent errors:

# Top 5 memory-hungry processes
ps aux --sort=-%mem | head -n 6

# How much disk is free?
df -h

# Find ERROR lines from today's app log and show the last 20
grep -i error /var/log/myapp.log | tail -n 20

# Count how many times each IP hit the server (classic one-liner)
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head

Notice the pattern: generate a stream of text, filter it, then summarize. That shape solves a huge fraction of day-to-day ops problems.

✍️Hands-On Exercise
  1. Use history | grep cd to find every cd command you’ve run this session.
  2. Create a file with three lines, then show only the middle line using a combination of head and tail.
  3. Use grep -rn "TODO" . to find every TODO comment in a project folder, with file names and line numbers.
  4. Pipe ls -l into wc -l to count how many items are in a directory.
🧾Cheat Sheet
TaskCommand
Search file contentsgrep -rni "pattern" .
Find files by namefind . -name "*.log"
First / last N lineshead -n 20 / tail -n 20
Follow a log livetail -f app.log
Count lineswc -l
Sort & dedupesort | uniq -c | sort -rn
Redirect output to filecmd > out.txt (overwrite) >> (append)
Pipe into next commandcmd1 | cmd2
Re-run last command!!
Run last command as rootsudo !!
Search historyCtrl + R
Cancel a running commandCtrl + C
💬Common Interview Questions
What does a pipe `|` do?

It connects the standard output of one command to the standard input of the next, letting you chain small tools into a processing pipeline without temporary files.

Difference between `>` and `>>`?

> redirects output to a file, overwriting it. >> appends to the file, preserving existing contents.

How do you watch a log file update in real time?

tail -f /path/to/file — it prints new lines as they’re written. Add -n 100 to also show the last 100 existing lines.

🚑Troubleshooting Scenarios

A command “hangs” with no output. It may be waiting on standard input. Press Ctrl+C to cancel. Some tools (like cat with no arguments) read from the keyboard until you press Ctrl+D.

grep returns nothing but you expect matches. Check case sensitivity (grep -i), and remember grep treats the pattern as a regex — special characters like . or * may need escaping or -F for a fixed string.

“command not found”. The program isn’t installed or isn’t on your PATH. Check with which <cmd> and install it via your package manager.

📚Official Documentation

📝 My notes on this topic

Auto-saves as you type