GUIDE 02 / EVERYDAY COMMANDS
Text & pipelines
Filter logs, count matches, extract fields, and direct output where you need it.
In this guide
Commands use class paths and example filenames. Replace placeholders and inspect the target before making changes.
Separate output from errors
Standard input is descriptor 0, standard output is 1, and standard error is 2. A pipe sends standard output to the next command. /dev/null discards data written to it.
find /etc -name "*.conf" 2>/dev/null | wc -l
find /etc -name "*.conf" 2>&1 | wc -lThe first command counts matching paths while hiding errors. The second counts both path lines and error lines. Without redirection, errors still appear on the terminal while wc counts only standard output.
Search literal text and patterns
grep -F treats a search as literal text. grep -E enables extended regular expressions such as alternatives with |. Use grep -E in new commands instead of the older egrep name.
sudo grep -Fc "Invalid user" /var/log/secure
sudo grep -F "Invalid user" /var/log/secure | head -n 10 | nl -ba
grep -E "^(SELINUX|SELINUXTYPE)=" /etc/selinux/configgrep -c counts matching lines. To count every occurrence, including several on one line, use grep -oF "Invalid user" FILE | wc -l.
Extract fields with awk
awk splits records into fields. $1 is the first field, $0 is the whole record, and NF is the number of fields. /etc/passwd uses colons rather than whitespace.
awk -F: '{print $1, $6}' /etc/passwd
awk -F: '$6 == "/var/adm" {n++} END {print n+0}' /etc/passwd
awk '{for (i=1; i<=NF; i++) print i, $i; exit}' /etc/passwdThe final example numbers whitespace-separated fields on the first line. Add -F: to number the colon-separated fields of /etc/passwd.
Save a copy while showing output
tee sends its input to the terminal and a file. -a appends instead of replacing a file. sort followed by uniq -c counts adjacent duplicate lines.
date | tee run-date.txt
printf "%s\n" "Another run" | tee -a run-date.txt
cut -d: -f1 /etc/passwd | sort
printf "%s\n" alice bob alice | sort | uniq -c | sort -nrUse command substitution to capture a command result: echo "$(date)". For file comparison, sha256sum FILE reports a digest; matching digests provide a strong integrity check. MD5 is unsuitable for security-sensitive verification.