grep
grep -i "pattern" file
Case-insensitive search.
grep -r "pattern" /path/to/dir
Recursive search through a directory.
grep -v "pattern" file
Invert match — show lines not matching the pattern.
grep -A 3 -B 2 "pattern" file
Show 2 lines before and 3 lines after matching lines.
grep -E "foo|bar" file
Use extended regex to match multiple patterns.
grep -n "pattern" file
Show line numbers where matches occur.
awk
awk '{print $1}' file
Print the first column.
awk -F ":" '{print $1, $3}' /etc/passwd
Use :
as a field separator, print fields 1 and 3.
awk '$3 > 100' file
Only show lines where the third column is greater than 100.
awk '{sum += $2} END {print sum}' file
Calculate the sum of the second column.
awk 'NR % 2 == 0' file
Print every second line (even lines).
Files
du -sh ./*
Show size of every item in the current directory.
find . -type f -mtime -3
Find files modified in the last 3 days.
find . -name "*.log" -exec rm {} \;
Find and delete all .log
files.
sort -k2 -n file
Sort file numerically by the second column.
sort file | uniq -c | sort -nr
Count and rank duplicate lines.
Tidbits
Replace last command
ncim hello.md
^ncim^nvim^
Remove text and square brackets from file names
rename 's/\ \[.*\]//' *