Pipes, redirection and searching text
Chain small commands together and search through logs.
Linux commands are small and do one job. The power comes from combining them. Every command has three channels: stdin (input), stdout (normal output) and stderr (errors).
Pipes and redirection
ls -l | less # send the output of ls into less
command > out.txt # write output to a file (overwrites)
command >> out.txt # append to a file
command 2> errors.txt # redirect only errors
command < input.txt # read input from a fileA pipe (|) connects the output of the command on its left to the input of the command on its right.
grep: find lines that match
grep error app.log # lines containing "error"
grep -i error app.log # ignore upper and lower case
grep -n error app.log # show line numbers
grep -v debug app.log # lines that do NOT contain "debug"
grep -r "TODO" src/ # search a whole folder recursivelyCounting and sorting
wc -l app.log # number of lines
sort names.txt | uniq -c | sort -rn # count duplicates, most frequent first
cut -d" " -f1 access.log | sort | uniq -c # count by the first columnA real example
How many requests returned 404 in a web server log, and which addresses made the most?
grep " 404 " access.log | wc -l
grep " 404 " access.log | cut -d" " -f1 | sort | uniq -c | sort -rn | headNote> replaces the file without asking. Use >> when you want to add to it.
Test yourself
Answer all the questions, then check them. Finish with every answer right to mark the lesson as done.