Mikey’s Linux Field NotesAll topics

GUIDE 02 / EVERYDAY COMMANDS

Text & pipelines

Filter logs, count matches, extract fields, and direct output where you need it.

4 quick lessons · Examples to adapt to your server
In this guide
Separate output from errorsSearch literal text and patternsExtract fields with awkSave a copy while showing output

Commands use class paths and example filenames. Replace placeholders and inspect the target before making changes.

01

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.

COMMANDS & EXAMPLES
find /etc -name "*.conf" 2>/dev/null | wc -l
find /etc -name "*.conf" 2>&1 | wc -l
Field note

The 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.

02

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.

COMMANDS & EXAMPLES
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/config
Field note

grep -c counts matching lines. To count every occurrence, including several on one line, use grep -oF "Invalid user" FILE | wc -l.

03

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.

COMMANDS & EXAMPLES
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/passwd
Field note

The final example numbers whitespace-separated fields on the first line. Add -F: to number the colon-separated fields of /etc/passwd.

04

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.

COMMANDS & EXAMPLES
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 -nr
Field note

Use 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.