Pages

Monday, January 31, 2011

Redirection In Linux

Share it Please

Redirection in Linux

Whenever we run a program, we get some output. This output may be displayed on the screen or it can be sent to other location. The process of sending the output to some other location is called Redirection.

Redirection can be in two ways

Either we can send the data to other location
Or
Get the data from other location.

In this article , we will see how we can redirect the data

">“: This is the basic form of redirection which sends data to other location.

          vim sample1
          Enter "This Is Sample"
          Save
         
          Now we will see how to use the redirection ">"
          cat sample1 > sample2

        View the contents of the sample2.Now if the file is not available, it will create a file and write the contents.

Note: Another way of writing to a file is by using echo like echo -e "This is Sample" > sample2

       It also allows to add contents of multiple files like
          cat sample1 sample2 > sample3

"<" : This can be used as a input redirection.That is we can redirect input from some other place
          tr '[a-z]' '[A-Z]' < sample3 


">>“: This is the third type of redirection in which the contents are added to the end like
         cat sample1 >> sample3
          The contents of the sample1 are added to the end of the sample3.

"<<" : This is much similar to 'Ctrl + D', except this uses a string to perform the end of the file like
          cat << eod >> sim
          Now it gives us a editor to enter text, once you are finished we can type ‘eod’ to say the  end of file.

"|" (also called as pipe): Pipe is an another form of redirection in which the output of one command can be send as an input to other command like
           ls -lart | grep si
     This takes all the files and sends the list to grep to find files that are with "si"

tee : This allows to send the output of a command to a file as well as to the console like
          ls -alrt | tee samoo
          Now there will be a file called samoo with the directory listing as contents.

So Happy Coding ...

No comments :

Post a Comment