Search

Learn Linux -9 Output Redirection



Aim: To introduce the reader to command redirection.


We have learnt quite a few commands by now, now lets have look at how the commands can be combined together or how can we use or store the output of a command.


Output Redirection:


Symbol used : " >" or ">>"


Syntax: "command" ">" "filename"
"command" ">>" "filename"


The ">" redirects the output of the command on its left to any file that is on its right. Please remember that ">" overwrites any existing file with the same filename or creates a new file if the file does not exist.


The ">>" redirects the output of the command on its left to the file on its right. But the difference in working from ">" is that, if the file exits, the data will be appended at the end and not over written.


For eg:

user@desktop:~/folder1$ cat file1 > file_redirect

user@desktop:~/folder1$




The command on the left of ">" is "cat". Which we know would output the contents of "file1". If we wanted to save this output to another file named "file_redirect" all we will have to do is put a ">" after "file1" and then give the name of the new file "file_redirect". If the file with the name "file_redirect" does not exist, it will be created and if the file does exist it will be over written.


For Eg:

user@desktop:~/folder1$ cat file2 >> file_redirect
user@desktop:~/folder1$


The above command would append the contents of the "file2" to "file_redirect". If the file did not exist before, it would create the file.




Input Redirection:

Symbol Used : "<"

Syntax: "command" "<" "file"

Just as we sent the output of one command to some file, we can send the input to a command from a file instead of from the terminal


To understand the working o the input redirection, lets look at a command "tr", which stands for translate.

tr:
The command is meant for translating all the occurrences of one string to other in the input


syntax: tr "string1" "string2"


string1: What characters have to transformed
string2: What should the characters should be transformed to.


Unlike "cat" and other commands we can not pass a filename as an input, but have to pass the input from the keyboard. The example below should make it clearer.




user@desktop:~/folder1$ tr cd CD

cd
CD
df
Df
user@desktop:~/folder1$


In the above example we have passed the the characters "c" and "d" to be transformed to upper case characters "C" and "D". Note that the transformation happens on each character respectively and not on a string as whole.
After you enter the command, the prompt will disappear and the cursor will be blinking waiting for you to input a string.
In the example as soon as we input "cd", it was transformed to "CD". Then we entered "df" which was transformed to "Df". Thus transforming each of the characters separately.


But what if we want to transform all the small case "a" to upper case "A" in a file. Typing the whole file on the terminal is surely not an optimal way of doing it.


In such cases input redirection can be used as shown below.


Foreg:

user@desktop:~/folder1$ tr a A < file1
creAting the first file in vi editor
VI is A widely used editor.
There Are other editors thAt cAn also be used.
user@desktop:~/folder1$



From the ouput it is evident that each occurrence of the small case "a" has been transformed to upper case "A".


Try it out:
Can "tr" be used to delete characters from an input file instead of transforming it.
Read the man page and find out what option to use for it.





Pipe:


Symbol : "|"



Syntax: "command1" | "command2"




Pipe i.e. the symbol " | " is used when we want use the output of one command as the input to another command. (The symbol is generally on the key above the enter key i.e shift+ "\").


Previously we saw how we can use input redirection to send a whole file as input to the "tr" command. We can also use "|" to send input to the "tr" as shown below.

For eg:


user@desktop:~/folder1$ cat file1 | tr a A

creAting the first file in vi editor

VI is A widely used editor.
There Are other editors thAt cAn also be used.
user@desktop:~/folder1$



What we did in the above example was send the output of the command "cat" as input to the command "tr". Which in turn transformed all the "a" to "A" and printed the output on the screen. There is not limit on how many pipes we can use together, i.e. if we want we can put one more "|" after the "tr" command and send the output as an input to some other command.


"|" is used extensively in scripting and is a very useful tool.



Try it out:

Can you write a command (i.e, using multiple commands) that will use all the redirection symbols we have learnt together.




Follow by Email