Separating Errors, and Discarding Output

Why standard error is separate. If errors went to standard output, then grep pattern file > results.txt would put error messages into your results file. Keeping them apart means the results file holds results and the errors still reach you on screen.

command 2> errors.txt captures only the errors.

command > out.txt 2> err.txt splits them into two files.

command > everything.txt 2>&1 merges error into output, which is what you want when you are saving a full transcript.

The bin that discards. /dev/null is a special file that throws away everything written to it. command 2> /dev/null runs the command and silences its errors, which is genuinely useful when a command reports things you do not care about, such as permission warnings while searching a large tree.

✦ Redirection is how a script leaves a record

The reason this matters beyond convenience is that a scheduled script has no screen to print to. Everything it learns has to be written somewhere or it is lost. Appending to a log file with a double arrow is how a script tells you what it did while you were not watching.

SANKOFA SANDBOX
learner:~$

Try it → Run `cat nosuchfile` and see the error on screen. Now run `cat nosuchfile 2> /dev/null` and see nothing. Then run `ls > listing.txt` and `cat listing.txt` to see output captured instead of displayed.

◈ Graded Task

Build a small report. Create report.txt in your home directory that starts with the line Workspace report, then has the output of listing your home directory appended after it, then has the number of ERROR lines from logs/app.log appended at the end. The file must end up with all three parts, in that order.

Checked against the filesystem you leave behind, not the commands you type, so any route that gets the job done counts. 4 visible and 3 hidden checks.

SANKOFA SANDBOX
learner:~$

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Practice Test →
← Back