<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Redirection Archives - Linux Windows and android Tutorials</title>
	<atom:link href="https://www.osradar.com/tag/redirection/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.osradar.com</link>
	<description>tutorials and news and Seurity</description>
	<lastBuildDate>Thu, 21 Nov 2019 10:42:45 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.8.12</generator>
	<item>
		<title>Input Output Redirection in Linux/Unix with Examples</title>
		<link>https://www.osradar.com/input-output-redirection-in-linux-unix-with-examples/</link>
					<comments>https://www.osradar.com/input-output-redirection-in-linux-unix-with-examples/#respond</comments>
		
		<dc:creator><![CDATA[sabi]]></dc:creator>
		<pubDate>Wed, 02 Oct 2019 12:02:58 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Unix]]></category>
		<category><![CDATA[Input Output redirection]]></category>
		<category><![CDATA[Redirection]]></category>
		<category><![CDATA[unix]]></category>
		<guid isPermaLink="false">https://www.osradar.com/?p=14122</guid>

					<description><![CDATA[<p>Introduction to Redirection Redirection is the process of changing the input or output of commands given for execution in Linux/Unix. By redirection, you are going to change the execution of commands as standard input/output. The basic workflow of Linux is that it takes input and after processing show the output. As we all know the [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.osradar.com/input-output-redirection-in-linux-unix-with-examples/">Input Output Redirection in Linux/Unix with Examples</a> appeared first on <a rel="nofollow" href="https://www.osradar.com">Linux  Windows and android  Tutorials</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h4><strong>Introduction to Redirection</strong></h4>



<p>Redirection is the process of changing the input or output of commands given for execution in Linux/Unix. By redirection, you are going to change the execution of commands as standard input/output.</p>



<p>The basic workflow of Linux is that it takes input and after processing show the output. As we all know the standard input device (stdin) is keyboard and the standard output device (stdout)  is screen. Usually Linux operates on this basic system of input/output. With redirection, we can change the standard input and output.</p>



<p>So, in this tutorial, we will learn. </p>



<ul><li><strong>Output Redirection</strong></li><li><strong>Input Redirection</strong></li><li><strong>File Descriptors (FD)</strong></li><li><strong>Error Redirection</strong></li><li><strong>Why error Redirection?</strong></li><li><strong>Examples</strong></li></ul>



<h3><strong>Output Redirection</strong></h3>



<p>Output redirection is represented by a sign <strong>&#8220;&gt;&#8221;</strong> The <strong>&#8220;&gt;&#8221; </strong>symbol is used for output (STDOUT).</p>



<p>First, we will see with examples that how can we take output in Linux rather than having it on our screen we will see it in the Terminal.</p>



<p><strong>Example:</strong></p>



<p class="has-background has-very-light-gray-background-color">ls-al &gt; output</p>



<p>Here&#8217;s the output of the given command ls-al  is redirected to the file &#8220;output&#8221; rather than displaying on the screen.</p>



<div class="wp-block-image"><figure class="aligncenter is-resized"><img loading="lazy" src="//1723336065.rsc.cdn77.org/wp-content/uploads/2019/10/vmware_xQ1C6wybBV.png" alt="" class="wp-image-14127" width="642" height="482" srcset="https://www.osradar.com/wp-content/uploads/2019/10/vmware_xQ1C6wybBV.png 621w, https://www.osradar.com/wp-content/uploads/2019/10/vmware_xQ1C6wybBV-300x225.png 300w, https://www.osradar.com/wp-content/uploads/2019/10/vmware_xQ1C6wybBV-80x60.png 80w, https://www.osradar.com/wp-content/uploads/2019/10/vmware_xQ1C6wybBV-265x198.png 265w, https://www.osradar.com/wp-content/uploads/2019/10/vmware_xQ1C6wybBV-560x420.png 560w" sizes="(max-width: 642px) 100vw, 642px" /><figcaption>otuput redirection example</figcaption></figure></div>



<p><strong>Note</strong>: Double check the file name and make sure you are using correct file name while redirecting the output to the file. If you have already file with the same name and you again type the same file name all data on the file will be deleted and new output will be written to that file.</p>



<p>If you want to add some more information to previously output file, you can use the &#8220;&gt;&gt;&#8221; sign to add more output value to it.</p>



<div class="wp-block-image"><figure class="aligncenter is-resized"><img loading="lazy" src="//1723336065.rsc.cdn77.org/wp-content/uploads/2019/10/output.png" alt="" class="wp-image-14130" width="616" height="441" srcset="https://www.osradar.com/wp-content/uploads/2019/10/output.png 616w, https://www.osradar.com/wp-content/uploads/2019/10/output-300x215.png 300w, https://www.osradar.com/wp-content/uploads/2019/10/output-587x420.png 587w" sizes="(max-width: 616px) 100vw, 616px" /></figure></div>



<p>You can also redirect standard output to devices. For instance you want to add some audio outputs to the other device you can do this so. It means we can apply standard output to files as well as devices.</p>



<p class="has-background has-very-light-gray-background-color">$ cat song.mp3 &gt; /dev/audio</p>



<p>The cat command reads the file song.mp3 and sends the output to the device /dev/audio which will play the music of that file.</p>



<p><strong>Note:</strong> Make sure your audio systems are properly working and configuration of sound is working.</p>



<h3><strong>Input Redirection</strong></h3>



<p> Input redirection is represented by a sign <strong>&#8220;&lt;&#8220;</strong> .</p>



<p>The <strong>&#8220;&lt;&#8221; </strong>symbol is used for output (STDIN). </p>



<p> We will see with examples that how  we can use mail system to send mail as an input from our Terminal.</p>



<p>We can type the contents of the mail, we want to send to our Friends, Family or anyone. With the help of input redirection, we can add files to our mails by just typing a command in Terminal.</p>



<p class="has-background has-very-light-gray-background-color">Mail -s &#8220;Subject&#8221; to-address &lt; filename</p>



<p>This will read the file name, attach it to the mail recipient and finally send it to the recipient.</p>



<p>The above example described is sample, now we will move towards the advanced redirection techniques which will make use of advanced redirection techniques like File Descriptors (FD).</p>



<h3><strong>File Descriptors (FD)</strong></h3>



<h4><strong>What are File Descriptors?</strong></h4>



<p>In Linux/Unix everything is a file. This includes your regular files, folders, and even your devices are also files. These files are specified against specific numbers. These numbers are called File Descriptor (FD).</p>



<p>Your Screen is also assigned a file descriptor. When you execute a command or give instructions to show some results, the output is sent to the file descriptor of the screen which will show the results on your screen. Similarly, your printer also assigned a specific number which will be called when you require for printing some documents or anything else.</p>



<h3><strong>Error Redirection</strong></h3>



<p>In Linux/Unix, when we run a program or execute a command 3 files are open always.</p>



<ul><li>First, Standard Input</li><li>Standard Output</li><li>Standard Error</li></ul>



<p>These files are open whenever you run a program. As we discuss earlier that these files are associated with the specific descriptor number.</p>



<div class="wp-block-columns has-2-columns">
<div class="wp-block-column">
<p><strong>File</strong></p>



<p class="has-background has-pale-cyan-blue-background-color"><strong>Standard Input STDIN</strong></p>



<p class="has-background has-vivid-cyan-blue-background-color"><strong>Standard Output STDOUT</strong></p>



<p class="has-background has-luminous-vivid-orange-background-color"><strong>And Standard Error STDERR</strong></p>
</div>



<div class="wp-block-column">
<p><strong>File Descriptor</strong></p>



<p class="has-background has-pale-cyan-blue-background-color"><strong>0</strong></p>



<p class="has-background has-vivid-cyan-blue-background-color"><strong>1</strong></p>



<p class="has-background has-luminous-vivid-orange-background-color"><strong>2</strong></p>
</div>
</div>



<p>Normally the error stream is displayed on the screen but error redirection routs the errors to the files other than your screen.</p>



<h3><strong>Why Error Redirection Occurs?</strong></h3>



<p>Error-redirection is the most popular and common feature of Linux/Unix .</p>



<p>Frequent Unix users faces that many command return them a large amount of errors. Let&#8217;s see some examples.</p>



<ul><li>First, suppose you are looking for a file and you do not have specific permissions to read or write or even find that file, you will redirect to error. These errors show permission denied error and do not allow you to find a specific file.</li><li>Similarly, in executing shell scripts, you do not find the exact output you are looking for due to some errors.</li></ul>



<p>So, the only solution is to redirect the error to a specific file</p>



<p><strong>Example 1</strong></p>



<p class="has-background has-very-light-gray-background-color">$myfile 2&gt;errorfile</p>



<p>In above example, we are executing a program name &#8220;myfile&#8221;.</p>



<p>The file descriptor for error redirection is 2.  By using &#8220;2&gt;&#8221; output would not be cluttered with errors.</p>



<figure class="wp-block-image"><img loading="lazy" width="615" height="289" src="//1723336065.rsc.cdn77.org/wp-content/uploads/2019/10/error-redirection.png" alt="" class="wp-image-14131" srcset="https://www.osradar.com/wp-content/uploads/2019/10/error-redirection.png 615w, https://www.osradar.com/wp-content/uploads/2019/10/error-redirection-300x141.png 300w" sizes="(max-width: 615px) 100vw, 615px" /></figure>



<h3><strong>Summary</strong></h3>



<ul><li>So, Each file in Linux/Unix including any files, folders or devices is assigned with the descriptor numbers.</li><li>The keyboard is referred to as the basic and standard input device and the screen is standard output device in Linux/Unix.</li><li>The symbol <strong>&#8220;&gt;&#8221; </strong> shows the output redirection and the symbol <strong>&#8220;&lt;&#8220;</strong> shows the input redirection while the symbol <strong>&#8220;&gt;&gt;&#8221;</strong> appends the output to the existing file(only in case if you retype the filename that existed before)</li><li><strong>&#8220;&gt;&amp;&#8221; </strong>symbol redirects the output of one file to the other one.</li><li>Finally, you can redirect error redirection in accordance with the <g class="gr_ gr_10 gr-alert gr_gramm gr_hide gr_inline_cards gr_run_anim Style multiReplace replaceWithoutSep replaceWithoutSep" id="10" data-gr-id="10">corresponding  descriptor</g> number.</li></ul>



<p>You can also learn about the <a href="https://www.osradar.com/use-tar-command-to-archive-compress-files-folders-in-linux/" target="_blank" rel="noreferrer noopener" aria-label="Tar command (opens in a new tab)">Tar command</a> in our post.</p>



<p>Please share this post and join <a rel="noreferrer noopener" aria-label="our Telegram Channel (opens in a new tab)" href="https://t.me/osradar" target="_blank">our Telegram Channel</a>.</p>
<p>The post <a rel="nofollow" href="https://www.osradar.com/input-output-redirection-in-linux-unix-with-examples/">Input Output Redirection in Linux/Unix with Examples</a> appeared first on <a rel="nofollow" href="https://www.osradar.com">Linux  Windows and android  Tutorials</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.osradar.com/input-output-redirection-in-linux-unix-with-examples/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
