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.

· 2 min read · beginner

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:

bash
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

bash
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 remove

Searching inside files

bash
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 matches

Sorting, counting, de-duplicating

The classic "top N" pipeline:

bash
# most frequent IPs in a log
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head

Read 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

bash
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 100

Doing something to each result with xargs

bash
find . -name '*.jpg' | xargs -I{} cp {} /backup/     # copy each match
grep -rl "deprecated" . | xargs sed -i 's/old/new/g' # edit every matching file

A few that earn their keep

bash
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 history

Takeaways

  • Pipes compose small tools into big results — build them one stage at a time.
  • find locates files; grep searches contents; sort | uniq -c | sort -rn is the universal "top N."
  • awk/cut slice columns; xargs applies a command to each result.
  • A handful of history tricks (!!, Ctrl-R) save real time every day.

Related