Essential Shell One-Liners
The pipes and filters that turn the terminal into a power tool — find, grep, sort, awk and xargs, composed into commands you'll reuse daily.
The Unix philosophy is small tools that do one thing, joined by pipes. Learn to compose them and the terminal becomes a power tool.
The pipe is the whole idea
| sends one command's output into the next command's input:
cat access.log | grep 404 | wc -l # how many 404s?Each stage transforms the stream. Chain enough and you can answer surprisingly complex questions in one line.
Finding files
find . -name '*.log' -type f # by name
find . -size +100M # larger than 100 MB
find . -mtime -1 # changed in the last day
find . -name '*.tmp' -delete # find and removeSearching inside files
grep -r "TODO" . # recursive
grep -rn "api_key" . # with line numbers (spot leaked secrets)
grep -i "error" app.log # case-insensitive
grep -c "404" access.log # count matchesSorting, counting, de-duplicating
The classic "top N" pipeline:
# most frequent IPs in a log
awk '{print $1}' access.log | sort | uniq -c | sort -rn | headRead it right to left in meaning: pull field 1, sort so duplicates group, uniq -c counts each, sort -rn orders by count descending, head keeps the top few.
Slicing columns with awk and cut
cut -d: -f1 /etc/passwd # usernames (colon-separated field 1)
awk -F, '{print $2}' data.csv # second CSV column
awk '$3 > 100' data.txt # rows where column 3 exceeds 100Doing something to each result with xargs
find . -name '*.jpg' | xargs -I{} cp {} /backup/ # copy each match
grep -rl "deprecated" . | xargs sed -i 's/old/new/g' # edit every matching fileA few that earn their keep
du -sh * | sort -rh | head # biggest things in this directory
ps aux --sort=-%mem | head # top memory hogs
!! # repeat the last command
sudo !! # ...as root
Ctrl-R # search your command historyTakeaways
- Pipes compose small tools into big results — build them one stage at a time.
findlocates files;grepsearches contents;sort | uniq -c | sort -rnis the universal "top N."awk/cutslice columns;xargsapplies a command to each result.- A handful of history tricks (
!!,Ctrl-R) save real time every day.