Linux Commands Quiz
grep, awk, sed, find, xargs — the shell's power tools with concrete use cases.
- grep
- Filter lines from text input that contain a certain pattern
- grep = "global regular expression print". Classic: `grep "ERROR" app.log`. Key flags: -r (recursive), -i (case-insensitive), -v (invert), -n (line numbers).
- sed
- Stream editing — find and replace text in a pipeline
- `sed = stream editor`. Classic: `sed "s/old/new/g" file.txt`. `-i` edits in place. For multi-line transformations often combined with awk.
- awk
- Column-based processing of text data (CSV, logs) — has its own mini programming language
- `awk "{print $2}"` prints the second column. Classic: `ps aux | awk "$3>50.0"` (processes using more than 50% CPU). Own language with variables, loops, conditions.
- Command → use case
- Zuordnen
- `uniq` requires that lines are already sorted. `sort -u` combines both. `cut` is simpler than awk when you only need to extract columns.
- grep recursive
- Lückentext
- `-r` (or `-R`) = recursive. `.` = current directory. Tip: `grep -rn` shows line numbers. Even faster: `rg` (ripgrep) — 10x+ faster than grep.
- find by extension
- Lückentext
- `find . -name "*.log"` — don't forget the quotes, otherwise the shell will expand the glob itself. Case-insensitive: `-iname`. By size: `-size +100M`.
- chmod 755
- Owner: rwx, Group: r-x, Others: r-x
- 4=read, 2=write, 1=execute. 7=4+2+1 (rwx), 5=4+1 (r-x). 755 is standard for executable scripts. 644 for normal files. 600 for sensitive ones (SSH keys).
- kill vs kill -9
- `kill` sends SIGTERM (process can clean up), `kill -9` sends SIGKILL (immediate, unblockable termination)
- SIGKILL (9) cannot be caught by the process. First choice is always SIGTERM (15) — gives e.g. DB connections time to close. -9 only as last resort.
- ps aux vs top
- `ps aux` gives a snapshot (good for pipes), `top` updates live and is interactive
- ps is great for scripts (`ps aux | grep node`). top shows live (on macOS prefer `top -o cpu`). Modern alternative: `htop` — colors, mouse, tree view.
- Network tool → function
- Zuordnen
- rsync is much more efficient than scp on large directories — transfers only differences. netcat ("Swiss Army knife of TCP/IP") for port scans, banner grabs, reverse shells.
- tar -xzvf
- Extract, gzip, verbose, file (filename follows)
- x = extract, z = gzip filter, v = verbose, f = file. To create: `tar -czvf out.tar.gz folder/`. List without extracting: `tar -tzvf`.
- Pipe |
- stdout of cmd1 becomes stdin of cmd2
- Pipes chain tools into pipelines: `cat log | grep ERROR | wc -l` counts ERROR lines. The Unix mantra: "do one thing well, compose with pipes".
- stdin / stdout / stderr
- stdin=0, stdout=1, stderr=2
- Hence: `cmd 2>/dev/null` swallows stderr, `cmd > out.log 2>&1` writes both to the same file. Bash shortcut: `&>` for stdout+stderr.
- Read a pipeline
- , ,
- Classic "top-N most frequent values" pipeline. uniq needs sorted input — otherwise it only counts directly consecutive repetitions.
- > vs >>
- `>` overwrites the target file, `>>` appends
- Big safety note: `>` is destructive — overwrites without asking. `>>` for logs. Tip: `set -o noclobber` protects against accidental overwrites.
- xargs
- It reads lines from stdin and turns them into arguments for another command
- `find . -name "*.tmp" | xargs rm` deletes all found files. Safer: `find ... -print0 | xargs -0 rm` (for filenames with spaces). Parallel: `xargs -P 8`.
- df -h
- Disk space per mountpoint (human-readable)
- `df` = disk free. `-h` = human-readable (GB instead of blocks). Related command: `du -sh *` (size per directory in the current path).
- Signal → meaning
- Zuordnen
- SIGHUP was originally sent when the phone connection was hung up. Today: nginx, sshd interpret it as "reread config".
- System logs
- /var/log/ (e.g. syslog, auth.log, messages)
- On systemd systems: `journalctl -u nginx` for service logs (instead of files). Kubernetes pods: `kubectl logs`. Docker: `docker logs <container>`.
- && vs ||
- `cmd1 && cmd2` runs cmd2 only if cmd1 exits 0. `||` is the opposite: only on failure.
- `make && ./run` — test, then run. `command || echo "failed"` — fallback. In pipelines: `set -e` aborts the script on the first failure.
- Tilde ~
- The home directory of the current user (value of $HOME)
- `~` = $HOME. `~user` = home dir of the given user. Watch out: inside quotes it is NOT expanded: `cd "~/bin"` fails, `cd ~/bin` works.