0
0
Linux CLIscripting~20 mins

stderr redirection (2>, 2>>) in Linux CLI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Stderr Redirection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this command?
Consider the command:
ls /nonexistent 2> error.log; cat error.log

What will be the output of cat error.log?
Linux CLI
ls /nonexistent 2> error.log; cat error.log
Als: cannot access '/nonexistent': No such file or directory
Bbash: ls: command not found
C
ls: cannot access '/nonexistent': No such file or directory
ls: cannot access '/nonexistent': No such file or directory
DNo output (empty file)
Attempts:
2 left
💡 Hint
The 2> operator redirects error messages to a file.
💻 Command Output
intermediate
2:00remaining
What happens when appending stderr with 2>>?
Given the commands:
echo 'First error' 1>&2 2> error.log
 echo 'Second error' 1>&2 2>> error.log
 cat error.log

What will be the output of cat error.log?
Linux CLI
echo 'First error' 1>&2 2> error.log
 echo 'Second error' 1>&2 2>> error.log
 cat error.log
ASecond error
BNo output
CFirst error
D
First error
Second error
Attempts:
2 left
💡 Hint
2> overwrites, 2>> appends to the file.
🔧 Debug
advanced
2:00remaining
Why does this command not redirect stderr as expected?
You run:
grep 'pattern' file.txt | wc -l 2> errors.log

But errors.log remains empty even if file.txt does not exist. Why?
Linux CLI
grep 'pattern' file.txt | wc -l 2> errors.log
ABecause wc -l overwrites errors.log
BBecause 2> redirects stderr of wc, not grep
CBecause file.txt exists and has no errors
DBecause only stdout of grep is piped, stderr is not redirected properly
Attempts:
2 left
💡 Hint
Think about which command's stderr is redirected.
📝 Syntax
advanced
2:00remaining
Which command correctly appends stderr to a file?
Choose the command that appends stderr output to errors.log without overwriting it.
Acommand 2>> errors.log
Bcommand 2> errors.log
Ccommand >> 2 errors.log
Dcommand 2> > errors.log
Attempts:
2 left
💡 Hint
Appending means adding to the end of the file.
🚀 Application
expert
3:00remaining
How to redirect both stdout and stderr to different files simultaneously?
You want to run mycommand and save its standard output to out.log and its error output to err.log. Which command does this correctly?
Amycommand > out.log 2>> err.log
Bmycommand 2> err.log > out.log
Cmycommand > out.log 2> err.log
Dmycommand 2>> err.log > out.log
Attempts:
2 left
💡 Hint
Order of redirections matters less here, but appending vs overwriting does.