Bird
Raised Fist0
Linux CLIscripting~10 mins

stderr redirection (2>, 2>>) in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - stderr redirection (2>, 2>>)
Run command
↓
Command outputs
↓
stdout (normal output)
↓
stderr (error output)
↓
Redirect stderr?
Yes↓
Overwrite or Append file
↓
End
When a command runs, it sends normal output to stdout and errors to stderr. Using 2> or 2>> redirects stderr to a file, overwriting or appending.
Execution Sample
Linux CLI
ls /notexist 2> error.txt
ls /notexist 2>> error.txt
Run a command that fails, redirect error output to a file, first overwriting then appending.
Execution Table
StepCommandstderr OutputRedirection OperatorFile Content After Step
1ls /notexist 2> error.txtls: cannot access '/notexist': No such file or directory 2>ls: cannot access '/notexist': No such file or directory
2ls /notexist 2>> error.txtls: cannot access '/notexist': No such file or directory 2>>ls: cannot access '/notexist': No such file or directory ls: cannot access '/notexist': No such file or directory
💡 After step 2, stderr is appended to error.txt, so file has two error messages.
Variable Tracker
VariableStartAfter Step 1After Step 2
error.txt content(empty)ls: cannot access '/notexist': No such file or directory ls: cannot access '/notexist': No such file or directory ls: cannot access '/notexist': No such file or directory
Key Moments - 2 Insights
Why does the file content change differently between 2> and 2>>?
2> overwrites the file with new stderr output (see step 1 in execution_table), while 2>> appends new stderr output to the existing file content (see step 2).
Does 2> redirect normal output (stdout)?
No, 2> only redirects stderr (error messages). Normal output goes to stdout unless redirected separately.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the content of error.txt after step 1?
AEmpty file
BTwo error messages
COne error message
DNormal output of ls command
💡 Hint
Check the 'File Content After Step' column for step 1 in execution_table.
At which step does the file error.txt get appended instead of overwritten?
AStep 1
BStep 2
CBoth steps
DNeither step
💡 Hint
Look at the 'Redirection Operator' column in execution_table to see which uses 2>>.
If we used > instead of 2> in step 1, what would happen?
Astdout would be redirected to error.txt
BBoth stdout and stderr would be redirected
Cstderr would be redirected to error.txt
DNo output would be redirected
💡 Hint
Remember 2> redirects stderr, > redirects stdout by default.
Concept Snapshot
stderr redirection:
- 2> filename : redirect stderr to filename (overwrite)
- 2>> filename : redirect stderr to filename (append)
- stdout is not affected
- Use to capture error messages separately
- Useful for debugging commands
Full Transcript
When you run a command in Linux, it sends normal output to stdout and error messages to stderr. Using 2> followed by a filename redirects the error messages to that file, replacing any existing content. Using 2>> appends error messages to the file instead of replacing it. Normal output is not redirected by these operators. This helps separate errors from normal output and is useful for troubleshooting.

Practice

(1/5)
1. What does the command ls nonexistingfile 2> error.log do in Linux?
easy
A. Sends the error message to the file error.log, replacing its content.
B. Appends the error message to the file error.log.
C. Sends the normal output to error.log.
D. Displays the error message on the screen only.

Solution

  1. Step 1: Understand 2> redirection

    The operator 2> redirects standard error (stderr) to a file, replacing its content.
  2. Step 2: Analyze the command behavior

    The command tries to list a non-existing file, so it produces an error message sent to error.log, overwriting previous content.
  3. Final Answer:

    Sends the error message to the file error.log, replacing its content. -> Option A
  4. Quick Check:

    2> replaces error file content [OK]
Hint: 2> overwrites error file, 2>> appends error [OK]
Common Mistakes:
  • Confusing 2> with 2>>
  • Thinking 2> sends normal output
  • Assuming error shows on screen
2. Which of the following is the correct syntax to append error messages to a file named errors.txt?
easy
A. command 2>> errors.txt
B. command 2> errors.txt
C. command >2 errors.txt
D. command >>2 errors.txt

Solution

  1. Step 1: Identify append operator for stderr

    The operator 2>> appends standard error to a file without overwriting it.
  2. Step 2: Match syntax with options

    command 2>> errors.txt uses 2>> correctly to append errors to errors.txt.
  3. Final Answer:

    command 2>> errors.txt -> Option A
  4. Quick Check:

    Append errors with 2>> [OK]
Hint: Use 2>> to add errors, not overwrite [OK]
Common Mistakes:
  • Using 2> instead of 2>> to append
  • Swapping > and 2 in syntax
  • Using >>2 which is invalid
3. What will be the content of log.txt after running this command twice?
grep 'pattern' file.txt 2>> log.txt
Assuming file.txt does not exist.
medium
A. log.txt will be empty because errors go to screen.
B. log.txt will contain only one error message, overwritten by second run.
C. log.txt will contain two copies of the error message appended.
D. log.txt will contain normal grep output twice.

Solution

  1. Step 1: Understand 2>> appends stderr

    The operator 2>> appends error messages to the file without deleting previous content.
  2. Step 2: Analyze repeated command effect

    Since file.txt is missing, grep outputs an error each time, appending twice to log.txt.
  3. Final Answer:

    log.txt will contain two copies of the error message appended. -> Option C
  4. Quick Check:

    2>> appends errors, so two runs add two messages [OK]
Hint: 2>> adds errors, so repeated runs append messages [OK]
Common Mistakes:
  • Thinking 2>> overwrites file
  • Assuming errors print on screen
  • Confusing normal output with error output
4. You run cat missingfile 2> errors.log but errors.log remains empty. What is the likely mistake?
medium
A. You redirected stdout instead of stderr.
B. The command ran successfully with no errors.
C. You used 2>> instead of 2>.
D. The file missingfile actually exists.

Solution

  1. Step 1: Check if error occurs

    If missingfile exists, cat produces no error, so nothing is sent to errors.log.
  2. Step 2: Understand empty error file

    Since no error happened, the error file remains empty despite redirection.
  3. Final Answer:

    The command ran successfully with no errors. -> Option B
  4. Quick Check:

    No error means empty error file [OK]
Hint: Empty error file means no error occurred [OK]
Common Mistakes:
  • Assuming 2>>> is valid syntax
  • Confusing stdout and stderr redirection
  • Ignoring that file might exist
5. You want to run a script that logs all error messages to error.log without losing previous errors, but normal output should show on screen. Which command correctly does this?
hard
A. ./script.sh >> error.log 2>> error.log
B. ./script.sh 2> error.log
C. ./script.sh > error.log 2>&1
D. ./script.sh 2>> error.log

Solution

  1. Step 1: Identify error logging without overwrite

    To keep previous errors, use 2>> to append stderr to error.log.
  2. Step 2: Ensure normal output shows on screen

    Redirecting only stderr leaves stdout on screen, so ./script.sh 2>> error.log fits requirements.
  3. Final Answer:

    ./script.sh 2>> error.log -> Option D
  4. Quick Check:

    Append errors with 2>>; stdout stays on screen [OK]
Hint: Use 2>> for errors, no stdout redirection to keep screen output [OK]
Common Mistakes:
  • Using 2> which overwrites error log
  • Redirecting stdout to file losing screen output
  • Combining stdout and stderr incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard