Breaking News

How to use Redirection operator ( > ) in Linux Operating System

Redirection ( > ) is an Operator that you can use to determine where the results of a certain command should be sent. This helps and simplify the writing of commands in a best and simple way. To use this operator, just read the following steps examples and you will get a better understanding of it!
I'm using a Kali Linux but don't worry, any version apart from this Kali the command can work.

  • Open your Linux Machine
  • Now Open terminal (This will be used to write a command that will create a directory)
  • I want to use this operator in my Home Directory (root directory), so there's no need to navigate anywhere because by default I'm in my home directory.
  • When I use ls -l command in my home directory, I can see the following Information as you can see in the following image.

  • Now, I want to use Redirection operator ( > ) to send this results to the Desktop in a file called New_Result. This file will be created automatically when I send the results of ls -l command to the Desktop. Type the following command to complete that task.
  • ls -l > /root/Desktop/New_Result (ls command will show/list all files located in my directory, with -l switch/option will print those files in a long form, Redirection Operator (>) will send the results obtained from ls -l command to a file called New_Result on a Desktop)

  • Now, let us display/list all files located on Desktop by typing the following command.
  • ls Desktop (This command will display/show/list all files located on Desktop)

  • A file called New_Result is located on Desktop, now we need to open/read it to confirm if the results from ls -l command was sent on it. To do so, type the following command.
  • cat Desktop/New_Result (The cat command read a file called New_Result located on the Desktop)

  • As you can see, those information obtained from ls -l in my home directory now we have sent to the file called New_Result located on Desktop.
  • Note: If I'm using single Redirection Operator ( > ) as we used above, means I'm going to override an existing information on New_Result but if I will use double Redirection Operators (>>) means I'm going to append on existing information in the New_Result. Append means adding a something to the end of something (Add information to the end of Information). Now let us use double Redirection Operators (>>) to append other information to New_Result.
  • I want use cal command to display a Calendar, then this output (Calendar) will be appended to New_Result file. To do so, type the following command.
  • cal >> Desktop/New_Result (cal command used to display Calendar, and Redirection Operators >> used to Append Calendar result to New_Result)

  • Now, we expect the output from cal command has been appended to New_Result located on Desktop, to confirm we have to read it. To do so, type the following command.
  • cat Desktop/New_Result (The cat command read a file called New_Result located on the Desktop)

  • As we can see, we used Redirection Operator (>) to send the results of a command to a certain File. This operator also can be used with Piping operator ( | ) (Discussed on previous post).
  • We are done!!!
Please, comment and share this post with your friends
Thank you...!!!