As Standard Input, Standard Output, and Standard Error are data streams, we can redirect these streams.
We can redirect standard output by using > and >> symbols.
>will perform overwriting of existing data>>will perform appending to existing data
Example: To redirect the standard output of cat command from terminal to output.txt:
$ cat 1> output.txt
sample data
ctrl+d
Sample data won't be displayed to the terminal and will be written to output.txt.
Note: The redirection symbol > is always associated with 1 (Standard Output) by default. Hence, we are not required to specify 1 explicitly.
$ cat >> output.txt
sample data
ctrl+dInstead of overwriting, if we want to perform appending, we should use >>.
We can redirect error messages from the terminal to our own file by using > and >> symbols.
$ cal 34 w3892384208342 2>> error.txtNow the error message won't be displayed to the console and will be written to error.txt.
For error redirection, 2 is mandatory.
Error Redirection
We can implement error redirection by using > and >> symbols.
Standard Error Stream is associated with digit 2.
$ cal pqr347 2>> error.txt #===>Error Redirection$ cal abc721 >> error.txt #===>Output redirectionWe can redirect standard input from keyboard to our required file.
We can perform input redirection by using the < symbol.
akshay@myubuntu:~/Documents/Linux$ cat 0<demo.txt
add some content here
add omre extra cotent here
this is extra line added after permission updated
added last line by akshay $ cat 0< a.txt 1>>output.txt 2>>error.txtThe < symbol is always associated with 0 by default. Hence we can remove it:
$ cat < a.txt >>output.txt 2>>error.txtNote: To redirect both standard output and standard error to the same destination, we can use the following shortcut:
$ cat < a.txt &> output.txt&> means both standard output and standard error.
In Unix, everything is treated as a file, even our terminal.
We can find terminal-related files by using the tty command.
Terminal 2:
$ tty
/dev/pts/1Terminal 1:
$ ls -l 1> /dev/pts/1Terminal 1's long listing output will be displayed on Terminal 2.
-
In how many ways can a command get input?
- Answer: 2 ways
- From Standard Input
- From command line arguments
- Answer: 2 ways
-
Which of the following contains Data Streams?
- A. Standard Input
- B. Standard Output
- C. Standard Error
- D. Command Line arguments
- Answer: A, B, C
-
By default, Standard Input is connected to:
- A. Terminal
- B. Keyboard
- C. A File
- Answer: B
-
True or False: By default, Standard Output is connected to Terminal.
- Answer: True
-
True or False: By default, Standard Error is connected to Terminal.
- Answer: True
-
What number represents the Standard Input Stream?
- Answer: 0
-
What number represents the Standard Output Stream?
- Answer: 1
-
What number represents Standard Error?
- Answer: 2
-
How can we redirect Standard Output of the ls command to a file called output.txt?
- A ls 2> output.txt
- B ls 0< output.txt
- C ls 1< output.txt
- D ls 1> output.txt
- Answer: D
Note:
ls 2> output.txtredirects standard error from terminal to output.txtls 0< output.txtredirects standard input from keyboard to output.txtls 1< output.txtis meaningless
-
How can we redirect the Standard Output of the ls command to output.txt, while at the same time redirecting Standard Error to error.txt?
- Answer:
or
ls 1> output.txt 2> error.txt
ls > output.txt 2> error.txt
- Answer:
-
Explain the difference between <, >, >> in Redirection?
- Answer:
<symbol is meant for input redirection>symbol is meant for output redirection where the existing data will be overwritten>>symbol is meant for output redirection where the data will be appended instead of overwriting
- Answer: