Linux standard input output error
Every command or in-turn respective process initialized with some kind of streams.
- Standard input stream (abbreviated stdin) which is use to get the input for the associated program or process.
- Standard output (abbreviated stdout) where process or program writes the output.
- Standard error (abbreviated stderr) used to store or display error preferably on the screen.
In Linux terminology each above streams are indicated by the digits or symbol as below:
While using the above symbol you must use either “<” and “>” depending on the condition.
How to redirect stdin
Redirecting standard input stdin in Linux is pretty simple you need to use “<” indicator along with symbol mentioned above which is 0.
Example for redirect stdin:
Cat command displays contents of the file on the screen. Now as an example we are using redirect method which inputs the data streams for cat command. As a result it displays the contents of hello.txt on the screen.
[root@rhel test]# cat 0< hello.txt Welcome to UxTechno!!!
In the above example we have used the file hello.txt as a standard input stdin for the cat command or program.
How to redirect stdout and stderr to file
Redirect stdout to file
First of all we will learn how to redirect stdout to file. In order to redirect standard output to a file you need to use symbol “>” followed by the name of the file.
command > output.log
Here we are using “>” symbol as a result all the output of the command gets redirected to output.log file.
[root@rhel ~]# df -h > /tmp/output.log [root@rhel ~]# cat /tmp/output.log Filesystem Size Used Avail Use% Mounted on devtmpfs 484M 0 484M 0% /dev tmpfs 497M 0 497M 0% /dev/shm tmpfs 497M 57M 441M 12% /run tmpfs 497M 0 497M 0% /sys/fs/cgroup /dev/xvda1 8.0G 1.1G 7.0G 14% / tmpfs 100M 0 100M 0% /run/user/1000 [root@rhel ~]#
Redirect stderr to file
In some of the cases you also wanted to be redirect standard error (stderr) to file instead of displaying them on to the screen. In order to redirect standard error stderr to file you need to use “2>” symbol followed by the name of the file.
command 2> error.log
[root@rhel ~]# df -t 2> /tmp/error.log [root@rhel ~]# cat /tmp/error.log df: option requires an argument -- 't' Try 'df --help' for more information. [root@rhel ~]#
In the above example we are purposefully using wrong argument “-t” along with the command “df” because we want to generate error message. As a result error gets generated then redirected to file followed by “2>” symbol.
How to redirect standard error stderr and standard out stdout in one go
Most of the time we want stderr as well as stdout to be redirected to one single file. Hence we must use “2>&1” symbol which will redirect stderr to stdout. And then we can use “>” symbol to direct it to one single file. Yet other easy method would be use symbol “&>”.
command > file.log 2>&1
command &> file.log
standard output example
[root@rhel ~]# ls . > /tmp/stdout.log [root@rhel ~]# cat /tmp/stdout.log log.txt [root@rhel ~]#
standard error example
Now we are generating standard error stderr by listing file “.uxtecho” which does exists.
[root@rhel ~]# ls .uxtecho 2> /tmp/stderr.log [root@rhel ~]# cat /tmp/stderr.log ls: cannot access .uxtecho: No such file or directory [root@rhel ~]#
Combine redirection of both stderr and stdout to a single file as below:
[root@rhel ~]# ls . .uxtecho &> /tmp/stdall.log [root@rhel ~]# cat /tmp/stdall.log ls: cannot access .uxtecho: No such file or directory .: log.txt [root@rhel ~]#
Maybe you can also use below method which does almost same work.
[root@rhel ~]# ls . .uxtecho > /tmp/stdall2.log 2>&1 [root@rhel ~]# cat /tmp/stdall2.log ls: cannot access .uxtecho: No such file or directory .: log.txt [root@rhel ~]#
Probably you may like to Learn more about How to convert xls file to csv file in Linux and vice versa.