Standard Error
Part of the Fundamentals section of Coddy's Terminal journey — lesson 41 of 82.
The third and final data stream is standard error, abbreviated as stderr. This stream is specifically for error messages and warnings, keeping them separate from normal output.
When a command fails, the error message goes to standard error, not standard output:
ls nonexistent_folderThe error message you see comes through stderr. This separation matters because you can redirect errors independently from regular output.
To redirect standard error, use 2> instead of >:
ls nonexistent_folder 2> errors.txtThe 2 refers to stderr's file descriptor number (stdout is 1, stderr is 2). This command captures the error message in errors.txt while keeping your terminal clean.
You can redirect both streams to different files:
ls existing_folder nonexistent_folder > output.txt 2> errors.txtSuccessful output goes to output.txt, while any errors go to errors.txt. To send both streams to the same file, use 2>&1:
ls existing_folder nonexistent_folder > all_output.txt 2>&1This redirects stderr to wherever stdout is going, combining everything into one file.
Challenge
EasyUse the 2> operator to redirect an error message to a file. Run these two commands in order:
- Try to list a folder called
missing_folderusingls, and redirect the error message to a file callederrors.txt - Use
catto display the contents oferrors.txt
Hint: The format is
ls foldername 2> filename. The error message goes into the file instead of appearing on screen. Usecatto verify it was captured.
Cheat sheet
The standard error stream (stderr) is used for error messages and warnings, keeping them separate from normal output.
To redirect standard error, use 2>:
ls nonexistent_folder 2> errors.txtThe 2 refers to stderr's file descriptor number (stdout is 1, stderr is 2).
You can redirect both stdout and stderr to different files:
ls existing_folder nonexistent_folder > output.txt 2> errors.txtTo send both streams to the same file, use 2>&1:
ls existing_folder nonexistent_folder > all_output.txt 2>&1Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Directories
Create A DirectoryCopy A DirectoryMove And Rename A DirectoryDelete A DirectoryRecap - Directory Operations7File Content
Head And TailWord CountSort CommandUnique CommandGrep BasicsGrep With FlagsRecap - Text Detective2Navigation
Print Working DirectoryList FilesChange DirectoryAbsolute vs Relative PathsHome And Root DirectoryRecap - Find Your Way8Redirection
Standard OutputOverwrite To A FileAppend To A FileStandard InputStandard ErrorRecap - Log Builder6Wildcards And Patterns
The Star WildcardThe Question Mark WildcardBracket WildcardsCombining WildcardsRecap - Selective Operations9Piping
What Is A PipeChaining Two CommandsChaining Multiple CommandsPipe With GrepRecap - Data Pipeline