Separate the two output streams
Task
Prove to yourself, with commands whose result you can check, that stdout and stderr are separate channels, that redirection order decides what is captured, and that a pipe carries only one of them. Every one of these is a scenario the exam presents as a puzzle, and each becomes obvious once you have watched it happen.
Steps
- Run a command that writes to both channels at once:
ls /etc /nonexistent. Note that both appear on your terminal, mixed. - Send only stdout to a file:
ls /etc /nonexistent > out.txt. The error remains on screen. Inspectout.txt. - Send only stderr to a file:
ls /etc /nonexistent 2> err.txt. Now the listing is on screen and the error is not. - Capture both, correctly:
ls /etc /nonexistent > both.txt 2>&1. - Capture both, incorrectly:
ls /etc /nonexistent 2>&1 > wrong.txt. Compare the two files and explain the difference before reading on. - Demonstrate that a pipe carries stdout only:
ls /etc /nonexistent | wc -l-- the error still reaches your terminal and is not counted. - Create a file with content, then destroy it with
>:date > log.txt, thencat missing.txt > log.txt. Check the size oflog.txtafterwards.
Verify
cd ~/lab-redirect
grep -c . both.txt # contains the listing AND the error
grep -c . wrong.txt # contains the listing only
grep -ci 'no such file' both.txt # 1
grep -ci 'no such file' wrong.txt # 0
stat -c '%s' log.txt # 0 -- truncated before cat ever ran
both.txt must contain the error message and wrong.txt must not. If they are identical, the redirections ran in the same order and you have swapped something -- read the two command lines again, left to right.
Notes
Step 7 is the one worth internalising. The shell opens and truncates the target before the command starts, so a failing command still destroys the file. That is why > on a log you care about is a one-way operation, and why >> exists.