appzasGamesToolsDevLearnCheat sheetsQuick calcsConvertersReferenceNetworkTimeCalculatorsCompareLLM prices

Pipes, redirection and searching text

Lesson 4 of 510 minBeginner

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 file

A 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 recursively

Counting 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 column

A 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 | head

Note> 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.

Hintwc counts lines with the -l option.
Hintgrep has an option for case-insensitive search.
3. Which operator appends output to a file without erasing it?
4. What does a pipe (|) do?

Key terms