grep, find, Pipes and Redirects
The Unix philosophy is small tools that each do one job, joined with pipes. With grep, sort, uniq and cut, you can answer questions about huge log files in one line - like which IP made the most requests or how many errors happened today.
grep: find lines that match
Example
$ grep "POST" logs/access.log
10.0.0.4 - [27/Nov/2026:09:02:01] "POST /orders HTTP/1.1" 201 230
10.0.0.2 - [27/Nov/2026:09:03:58] "POST /orders HTTP/1.1" 500 52
$ grep -c " 200 " logs/access.log
5
$ grep -i "kitchen" products.csv
1,Kettle,kitchen,1299
3,Toaster,kitchen,1599
4,Mixer,kitchen,2499-iignore capitals,-ccount,-nshow line numbers,-vlines that don't match,-rsearch a whole folder
Pipes: join commands with |
A pipe sends one command's output into the next command. Small tools joined together can answer real questions:
Example
$ cut -d' ' -f1 logs/access.log | sort | uniq -c | sort -rn
3 10.0.0.4
2 10.0.0.9
2 10.0.0.7
1 10.0.0.2
$ grep '" 5' logs/access.log
10.0.0.2 - [27/Nov/2026:09:03:58] "POST /orders HTTP/1.1" 500 52
$ tail -n +2 products.csv | cut -d, -f3 | sort | uniq -c
2 home
3 kitchenThe first line answers "which visitor made the most requests?". The second finds server errors (status 5xx). The third counts products in each category (tail -n +2 skips the header).
Redirects: save output to a file
Example
$ grep "GET" logs/access.log > gets.txt
$ wc -l gets.txt
6 gets.txt
$ echo "checked at 09:10" >> gets.txt
$ tail -n 1 gets.txt
checked at 09:10>writes to a file (replacing it),>>adds to the end2> errors.txtsaves error messages;&> all.txtsaves both
find: search for files
Example
$ mkdir -p shop/src shop/logs
$ touch shop/src/app.js shop/src/db.js shop/logs/today.log
$ find shop -name "*.js" | sort
shop/src/app.js
shop/src/db.js
$ find shop -type d | sort
shop
shop/logs
shop/srcKey points
- grep finds matching lines.
- | pipes output into the next command.
- > and >> save output to files.
📝 Test yourself
1. What does the | symbol do?
2. What's the difference between > and >>?
3. Fill in the grep option that ignores upper and lower case:
grep "kitchen" products.csv
