0
0
Linux CLIscripting~15 mins

stderr redirection (2>, 2>>) in Linux CLI - Mini Project: Build & Apply

Choose your learning style9 modes available
Redirecting Error Messages in Linux Command Line
📖 Scenario: You are working on a Linux system and want to manage error messages from commands. Sometimes commands show errors on the screen, but you want to save these errors to a file for later review.
🎯 Goal: Learn how to redirect error messages (stderr) from commands into files using 2> and 2>> operators.
📋 What You'll Learn
Create a command that produces an error message
Redirect the error message to a file using 2>
Use a variable to hold the error file name
Append error messages to the file using 2>>
Display the contents of the error file
💡 Why This Matters
🌍 Real World
System administrators and developers often need to capture error messages from commands to troubleshoot problems later without cluttering the screen.
💼 Career
Knowing how to redirect error messages is essential for writing scripts and managing Linux systems efficiently.
Progress0 / 4 steps
1
Create a command that produces an error message
Write a command that tries to list a non-existent directory called missing_dir to produce an error message.
Linux CLI
Need a hint?

Use the ls command with a folder name that does not exist.

2
Redirect the error message to a file using 2>
Create a variable called error_file and set it to errors.txt. Then run the command ls missing_dir and redirect its error output to the file stored in error_file using 2>.
Linux CLI
Need a hint?

Use error_file=errors.txt to set the variable. Use 2>$error_file to redirect stderr.

3
Append error messages to the file using 2>>
Run the command cat missing_file.txt and append its error output to the file stored in error_file using 2>>.
Linux CLI
Need a hint?

Use 2>>$error_file to append stderr to the file.

4
Display the contents of the error file
Use the cat command to display the contents of the file stored in error_file.
Linux CLI
Need a hint?

Use cat $error_file to show the saved error messages.