I/O Redirection in Linux

Three types of I/O redirections in Linux

1. stdin         <
2. stdout       >
3. stderr       2>

Examples: 

1. # date > file1.txt
    Redirects the output of date command to the file file1.txt .

2. #cal >> file1.txt
    Redirects the output of cal command to file1.txt. Note that the out put of cal command will append to file    file1.txt.   > simply replaces the content where >> will append the content.

3. #tr [a-z] [A-Z] file1.txt  < file1.txt 
    Translates the all lowercase letter to uppercase letter in file1.txt. Here we are giving the file1.txt as input to the command using < .

4. $find / -name linux 2> out_error.txt
    Here a normal user is trying to find the file/folder with name "linux" under root file system ( / ).A normal user doesn't have permissions to every location under root file system ( / ). So the above command will give the output as well as some errors. 
    We can redirect the error messages to a file out_error.txt so that it can display only the found results.
We can use > out_results.txt to capture only the found resuts in out.txt file and leave the errors to display on stdout.

5. $find /-name linux > out_all.txt 2>&1
     The constructor "2>1" will redirect the stderr messages as stdout (but not file). The constructor "2>&1" also do the same but & indicates the output will be stored in a file. So both found results and error messages will store in the file out_all.txt

0 comments:

Post a Comment