Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!

Author Topic: What is Linux standard input output error and how to redirect stdout and stderr  (Read 2083 times)

0 Members and 1 Guest are viewing this topic.

akhilt

  • Guest
What is Linux standard input output error and how to redirect stdout and stderr to file in Linux

This article will quickly guide you about Linux standard input output error. Also, we will be learning how we can redirect stdout and stderr to file in Linux.

Linux standard input output error

Every command or in-turn respective process initialized with some kind of streams.
1. Standard input stream (abbreviated stdin) which is use to get the input for the associated program or process.
2. Standard output (abbreviated stdout) where process or program writes the output.
3. 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:

Data Stream                Symbol
standard input                  0
standard output                1
standard error                   2

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.

Code: [Select]
[root@rhel test]# cat 0< hello.txt
Welcome to Admin-ahead!!!
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.

Syntax:
Code: [Select]
command > output.log
Here we are using “>” symbol as a result all the output of the command gets redirected to output.log file.

Example:
[root@server ~]# df -h > /tmp/output.log

[root@server ~]# 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@server ~]#

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.

Syntax:
Code: [Select]
command 2> error.log
Example:
Code: [Select]
[root@server ~]# df -t 2> /tmp/error.log

[root@server ~]# cat /tmp/error.log
df: option requires an argument -- 't'
Try 'df --help' for more information.
[root@server ~]#

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 “&>”.

syntax:
Code: [Select]
command  > file.log 2>&1or
Code: [Select]
command &> file.log
standard output example
Code: [Select]
[root@server ~]# ls . > /tmp/stdout.log
[root@server ~]# cat /tmp/stdout.log
log.txt
[root@server ~]#

standard error example
Now we are generating standard error stderr by listing file “.admin-ahead” which does exists.
Code: [Select]
[root@server ~]# ls .admin-ahead 2> /tmp/stderr.log
[root@server ~]# cat /tmp/stderr.log
ls: cannot access .admin-ahead: No such file or directory
[root@server ~]#

Combine redirection of both stderr and stdout to a single file as below:

Code: [Select]
[root@server ~]# ls . .admin-ahead &> /tmp/stdall.log
[root@server ~]# cat /tmp/stdall.log
ls: cannot access .admin-ahead: No such file or directory
.:
log.txt
[root@server ~]#

Maybe you can also use below method which does almost same work.

Code: [Select]
[root@server ~]# ls . .admin-ahead > /tmp/stdall2.log 2>&1
[root@server ~]# cat /tmp/stdall2.log
ls: cannot access .admin-ahead: No such file or directory
.:
log.txt
[root@server ~]#

Hoping the information will come handy.! ;)