Input Output redirection in linux

While learning all the concepts of Linux we come across one of the most important concept of Linux O.S. Let’s explore this concept in detail.Redirection is the way of directing those commands which reads input to where commands sends output, trying to achieve something different. For this, we will have to use some controls to redirect this commands to change their destination.

If by default we have an output on our screen named ‘stdout’, we use ‘>’ symbol or ‘ >>’ symbol for redirection. When we run any command, sometimes it displays various outputs that its a bigger task to find out proper output, and most important is to tell whether the output appears is successful or error? At such times, its technically impossible to contact to the developer and know his intention of giving output. So,whatever the commands/programs they create and whatever output he displays along with they sends a exit code which tells us about the proper output. If by default you have an input on the screen named ‘stdin’, we use ‘<‘ symbol for redirection.

There are two types of codes which denotes whether the output is correct or not:- *if number ‘0’ comes as a output, then the output is successful. **if number ‘apart from 0’ comes as a output, then the output is failure/error.

Let’s check whether following outputs are right or not.

$ date                                                                                                                                Thu Apr 02 13:40:20 EST 2020    //output//

#exit code is stored in shell ‘?’ command, we have ‘echo $?’ command for this-

$ echo  $?                                                              0              //output; shows correct//

$ mnb
bash: mnb: command not found…    //output//

$ echo  $?
134    //output; shows error//

#Sometimes, we don’t want to print the output on the screen; here we use ‘> xyz.txt’, command for redirecting the output. ‘>’ symbol is used to send the output to another location ‘xyz.txt’

$ date                                                          Thu Apr 02 14:09:20 EST 2020    //output//                       $ date  >  f1.txt                                               $  cat  f1.txt                                                       Thu Apr 02 14:09:20 EST 2020    //output//

Shell is that smart enough to know whether the output is successful or not. It consists of 2 pipes- 1. One pipe is for the outputs for command who gives successful output. If it is correct shell internally sends the output through this pipe. 2. Second pipe is for the outputs for command who gives failure/error data. When the output comes, shell sends the output through this pipe saying it is error.

#While using ‘>’ symbol, by default it has pipe 1 with it, denoted by ‘1>’.

$ date  >  f1.txt
$ date  1>  f1.txt   //output is been sent to the located file//

$ rty  1>  f1.txt
bash: rty: command not found…    //output//

#here,the output didn’t went on his location because it shows error which needs to be in pipe 2; thus, it is showing the output on screen itself. In this case we need to give ‘2>’ symbol.

$ rty  2>  f1.txt  //no output appears here//

#If you have to append or add some data in the existing file, we use ‘>>’ symbol in both the pipes.

$ rty  2>>  f1.txt 
$  cat  f1.txt
bash: rty: command not found…    //output//

$ date  2>  f1.txt
Thu Apr 02 14:14:21 EST 2020    //output//
$ date 1>  f1.txt
$  cat  f1.txt
Thu Apr 02 14:14:21 EST 2020    //output//

# cat command shows us the output located in the file in which it is stored.We also have one more symbol ‘&>’, for redirection,

$ date  &>  f1.txt
$  cat  f1.txt
Thu Apr 02 14:19:34 EST 2020    //output//
$ rty  &>  f1.txt
$  cat  f1.txt
bash: rty: command not found…    //output//



Categories: Linux

Tags: ,

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: