5 Levels of Handling I/O Redirection in Linux
Redirecting the stdout, stderr and stdin like a guru
Yesterday, my colleague asked me a very interesting question about a Linux script she found on our server:
What does the following line of command mean?
date >> daily_log.txt 2>&1
I smiled and kindly told her the details as always. 🙂
This is about the redirection of standard output and standard error in Linux.
Basically, there are 3 types of standard I/O in Linux:
- Standard output (stdout)
- Standard error (stderr)
- Standard input (stdin)
This article will reveal the secrets of the above command and give you a comprehensive guide about the tips and tricks of the I/O redirection on Linux.
1. Redirecting Standard Output to a File
In some cases, it’s necessary to save the results of a command into a file for future usage instead of printing them on the standard output. To implement it, all we need is just the >
operator:
date > log.txt
The above command saves the result of the date
command into a file named log.txt
through the >
…