$
学ぶ Linux ハンドブック
ja
XP:0
クエスト 35Stream Master
+260 XP

Standard Error & /dev/null (2>, 2>&1)

このクエストの対話型ターミナルを起動する→インストール不要 • ブラウザ上で完全動作

概念と概要

In Linux, every process opens three standard I/O streams:
- File descriptor 0: stdin (Standard Input)
- File descriptor 1: stdout (Standard Output — where normal output goes)
- File descriptor 2: stderr (Standard Error — where error messages go)

When you run cmd > output.txt, only stdout is saved; error messages still appear on your screen. To redirect errors separately, specify file descriptor 2: cmd 2> errors.log. To silence noisy errors completely, redirect them to the Linux null device (black hole): cmd 2> /dev/null. To combine both stdout and stderr into one stream or file, use &> output.log or cmd > output.log 2>&1.

1. Discard permission warnings by searching root with stderr silenced: find / -name "*.conf" 2> /dev/null.
2. Capture an error by running cat nonexistent.txt 2> errors.log.
3. Combine both stdout and stderr into a single log: ls /etc/hosts nonexistent.txt &> all_output.log (or > all_output.log 2>&1).

コマンド構文リファレンス

Command / SyntaxDescription
find / -name "*.conf" 2> /dev/nullSilence stderr by redirecting to /dev/null
cat nonexistent.txt 2> errors.logRedirect standard error to errors.log
ls /etc/hosts nonexistent.txt &> all_output.logCombine stdout and stderr into all_output.log

検証目標

  • ○
    Run a command redirecting stderr to /dev/null (2> /dev/null)
  • ○
    Redirect error output to errors.log (2> errors.log)
  • ○
    Create all_output.log capturing both streams (&> or 2>&1)

クエスト完了への準備はできましたか? 35?