This article will explain the standard error. If you want to check the standard output and find more information about input/output facilities (which would be good before starting this post) you can find out more information here.
The standard error detailed
It was fairly simple redirecting the standard output, right? We just used the dedicated operator – ‘>’ and we could redirect the output to the preselected file. When it comes to the standard error, we need to refer to its file descriptor. What is a file descriptor? It is a number that annotates the file stream. For example, we have standard input, output, and error file streams, and their descriptors are zero, one, and two, respectively. So when we want to redirect the standard error we will just add the appropriate file descriptor. In the example below, that descriptor is “2”.
ls -l /usr/error 2> ls-error-file.txt
Redirecting both standard output and standard error
You might also wonder is it possible to capture the whole output (including the standard error) of some command and store it in a file. Well, yes it is! We need to redirect both standard output and standard error at the same time, using another operator – “&”.
ls -l /usr &> ls-output.txt
Throwing the unwanted output away
Lets say we don’t want any output from a command, we just want to throw the output away. This could apply to error and status messages the most. We can simply do this by redirecting the output to a special file called “/dev/null”, this file accepts input and does nothing with it.
ls -l /usr/error 2> /dev/null
We will just recap what we learned in the screenshot below:
So, we ran a command that returns an error and we stored it (using the ‘2>’ operator) in our ls-error-file.txt (1). Then we just displayed the contents of that file inside the terminal (2). Afterward, we tried to redirect both the output and the error inside the ls-output.txt file (3). Of course, because the directory is nonexistent we only have the standard error to show (4). And lastly, we just threw away the command altogether (5).
Wrapping it up
This is our standard error command explained. Our last command in our I/O mini-series will be about standard input. Don’t miss it!
While you wait, you can read more Linux related posts here and other blog posts you can find here.
Pingback: Cat command and pipelines in Linux | The Dukh Chronicles
Comments are closed.