Linux tools compose like building blocks: one emits text and another receives and processes it. Understanding standard input (stdin), standard output (stdout), and standard error (stderr) turns isolated commands into workflows.
Standard streams
In a terminal, stdin commonly comes from the keyboard, stdout displays normal results, and stderr displays errors. Their common file descriptor numbers are 0, 1, and 2. Pipes and redirection change where these streams come from or go.
stdin · 0data a command reads
stdout · 1normal result output
stderr · 2separate error output
Connect commands with |
ls /etc | less sends a listing into a pager; ps aux | grep nginx filters a process list. Pipes can chain, as in grep "ERROR" app.log | sort | uniq. When grep can read the file directly, an extra cat app.log | grep is usually unnecessary.
> replaces, >> appends
ls /etc > files.txt writes stdout to a file and replaces existing content. echo "another line" >> notes.txt preserves and appends. Combine both ideas with grep "ERROR" app.log | sort > errors.txt.
< input and 2> error redirection
sort < names.txt makes the shell provide the file as stdin. find / -name "config.txt" 2> errors.txt redirects errors only. 2> /dev/null discards them, but hiding every error is a poor troubleshooting habit.
Reading direction: trace data left to right; | hands it to the next command, while > or >> sends final stdout to a file.
Trace Data Through a Command
KNOWLEDGE QUIZ
KNOWLEDGE QUIZ
Build a Log Processing Flow
Write one command for each data-flow task, distinguishing replace, append, and stderr.
- Filter ERROR lines from app.log, sort them, and replace errors.txt.
- Append the current date to audit.log.
- Provide names.txt as sort's stdin and show results in the terminal.
- Search the whole filesystem for config.txt, display normal results, and save errors to denied.txt.