{"id":11458,"date":"2019-03-14T08:08:01","date_gmt":"2019-03-14T08:08:01","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=11458"},"modified":"2019-03-14T08:08:01","modified_gmt":"2019-03-14T08:08:01","slug":"learn-the-basics-of-how-linux-i-o-input-output-redirection-works","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/14\/learn-the-basics-of-how-linux-i-o-input-output-redirection-works\/","title":{"rendered":"Learn The Basics of How Linux I\/O (Input\/Output) Redirection Works"},"content":{"rendered":"<p>One of the most important and\u00a0<a href=\"https:\/\/www.tecmint.com\/how-to-setup-and-configure-static-network-routing-in-rhel\/\" target=\"_blank\" rel=\"noopener\">interesting topics under Linux administration<\/a>\u00a0is I\/O redirection. This feature of the command line enables you to redirect the input and\/or output of commands from and\/or to files, or join multiple commands together using pipes to form what is known as a \u201c<strong>command pipeline<\/strong>\u201d.<\/p>\n<p>All the commands that we run fundamentally produce two kinds of output:<\/p>\n<ol>\n<li>the command result \u2013 data the program is designed to produce, and<\/li>\n<li>the program status and error messages that informs a user of the program execution details.<\/li>\n<\/ol>\n<p>In Linux and other Unix-like systems, there are three default files named below which are also identified by the shell using file descriptor numbers:<\/p>\n<ol>\n<li><strong>stdin or 0<\/strong>\u00a0\u2013 it\u2019s connected to the keyboard, most programs read input from this file.<\/li>\n<li><strong>stdout or 1<\/strong>\u00a0\u2013 it\u2019s attached to the screen, and all programs send their results to this file and<\/li>\n<li><strong>stderr or 2<\/strong>\u00a0\u2013 programs send status\/error messages to this file which is also attached to the screen.<\/li>\n<\/ol>\n<p>Therefore, I\/O redirection allows you to alter the input source of a command as well as where its output and error messages are sent to. And this is made possible by the\u00a0<code>\u201c&lt;\u201d<\/code>\u00a0and\u00a0<code>\u201c&gt;\u201d<\/code>\u00a0redirection operators.<\/p>\n<h3>How To Redirect Standard Output to File in Linux<\/h3>\n<p>You can redirect standard output as in the example below, here, we want to store the output of the\u00a0<a href=\"https:\/\/www.tecmint.com\/12-top-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">top command<\/a>\u00a0for later inspection:<\/p>\n<pre>$ top -bn 5 &gt;top.log\r\n<\/pre>\n<p>Where the flags:<\/p>\n<ol>\n<li><code>-b<\/code>\u00a0\u2013 enables\u00a0<strong>top<\/strong>\u00a0to run in batch mode, so that you can redirect its output to a file or another command.<\/li>\n<li><code>-n<\/code>\u00a0\u2013 specifies the number of iterations before the command terminates.<\/li>\n<\/ol>\n<p>You can view the contents of\u00a0<code>top.log<\/code>\u00a0file using\u00a0<a href=\"https:\/\/www.tecmint.com\/13-basic-cat-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">cat command<\/a>\u00a0as follows:<\/p>\n<pre>$ cat top.log\r\n<\/pre>\n<p>To append the output of a command, use the\u00a0<code>\u201c&gt;&gt;\u201d<\/code>\u00a0operator.<\/p>\n<p>For instance to append the output of\u00a0<a href=\"https:\/\/www.tecmint.com\/12-top-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">top command<\/a>\u00a0above in the\u00a0<strong>top.log<\/strong>\u00a0file especially within a script (or on the command line), enter the line below:<\/p>\n<pre>$ top -bn 5 &gt;&gt;top.log\r\n<\/pre>\n<p><strong>Note<\/strong>: Using the file descriptor number, the output redirect command above is the same as:<\/p>\n<pre>$ top -bn 5 1&gt;top.log\r\n<\/pre>\n<h3>How To Redirect Standard Error to File in Linux<\/h3>\n<p>To redirect standard error of a command, you need to explicitly specify the file descriptor number,\u00a0<code>2<\/code>\u00a0for the shell to understand what you are trying to do.<\/p>\n<p>For example the\u00a0<a href=\"https:\/\/www.tecmint.com\/tag\/linux-ls-command\/\" target=\"_blank\" rel=\"noopener\">ls command<\/a>\u00a0below will produce an error when executed by a normal system user without root privileges:<\/p>\n<pre>$ ls -l \/root\/\r\n<\/pre>\n<p>You can redirect the standard error to a file as below:<\/p>\n<pre>$ ls -l \/root\/ 2&gt;ls-error.log\r\n$ cat ls-error.log \r\n<\/pre>\n<div id=\"attachment_24175\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Redirect-Standard-Error-in-Linux.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24175\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Redirect-Standard-Error-in-Linux.png\" alt=\"Redirect Standard Error to File\" width=\"552\" height=\"173\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Redirect Standard Error to File<\/p>\n<\/div>\n<p>In order to append the standard error, use the command below:<\/p>\n<pre>$ ls -l \/root\/ 2&gt;&gt;ls-error.log\r\n<\/pre>\n<h3>How To Redirect Standard Output\/ Error To One File<\/h3>\n<p>It is also possible to capture all the output of a command (both standard output and standard error) into a single file. This can be done in two possible ways by specifying the file descriptor numbers:<\/p>\n<p><strong>1.<\/strong>\u00a0The first is a relatively old method which works as follows:<\/p>\n<pre>$ ls -l \/root\/ &gt;ls-error.log 2&gt;&amp;1\r\n<\/pre>\n<p>The command above means the shell will first send the output of the\u00a0<a href=\"https:\/\/www.tecmint.com\/15-basic-ls-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">ls command<\/a>\u00a0to the file\u00a0<strong>ls-error.log<\/strong>\u00a0(using\u00a0<code>&gt;ls-error.log<\/code>), and then writes all error messages to the file descriptor\u00a0<strong>2<\/strong>\u00a0(standard output) which has been redirected to the file\u00a0<strong>ls-error.log<\/strong>\u00a0(using\u00a0<code>2&gt;&amp;1<\/code>). Implying that standard error is also sent to the same file as standard output.<\/p>\n<p><strong>2.<\/strong>\u00a0The second and direct method is:<\/p>\n<pre>$ ls -l \/root\/ &amp;&gt;ls-error.log\r\n<\/pre>\n<p>You can as well append standard output and standard error to a single file like so:<\/p>\n<pre>$ ls -l \/root\/ &amp;&gt;&gt;ls-error.log\r\n<\/pre>\n<h3>How To Redirect Standard Input to File<\/h3>\n<p>Most if not all commands get their input from standard input, and by default standard input is attached to the keyboard.<\/p>\n<p>To redirect standard input from a file other than the keyboard, use the\u00a0<code>\u201c&lt;\u201d<\/code>\u00a0operator as below:<\/p>\n<pre>$ cat &lt;domains.list \r\n<\/pre>\n<div id=\"attachment_24176\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Redirect-Standard-Input-to-File.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24176\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Redirect-Standard-Input-to-File.png\" alt=\"Redirect Standard Input to File\" width=\"417\" height=\"116\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Redirect Standard Input to File<\/p>\n<\/div>\n<h3>How To Redirect Standard Input\/Output to File<\/h3>\n<p>You can perform standard input, standard output redirection at the same time using\u00a0<a href=\"https:\/\/www.tecmint.com\/sort-command-linux\/\" target=\"_blank\" rel=\"noopener\">sort command<\/a>\u00a0as below:<\/p>\n<pre>$ sort &lt;domains.list &gt;sort.output\r\n<\/pre>\n<h3>How to Use I\/O Redirection Using Pipes<\/h3>\n<p>To redirect the output of one command as input of another, you can use pipes, this is a powerful means of building useful command lines for complex operations.<\/p>\n<p>For example, the command below will\u00a0<a href=\"https:\/\/www.tecmint.com\/find-recent-modified-files-in-linux\/\" target=\"_blank\" rel=\"noopener\">list the top five recently modified files<\/a>.<\/p>\n<pre>$ ls -lt | head -n 5 \r\n<\/pre>\n<p>Here, the options:<\/p>\n<ol>\n<li><code>-l<\/code>\u00a0\u2013 enables long listing format<\/li>\n<li><code>-t<\/code>\u00a0\u2013\u00a0<a href=\"https:\/\/www.tecmint.com\/find-and-sort-files-modification-date-and-time-in-linux\/\" target=\"_blank\" rel=\"noopener\">sort by modification time with the newest files<\/a>\u00a0are shown first<\/li>\n<li><code>-n<\/code>\u00a0\u2013 specifies the number of header lines to show<\/li>\n<\/ol>\n<h3>Important Commands for Building Pipelines<\/h3>\n<p>Here, we will briefly review two important commands for building command pipelines and they are:<\/p>\n<p><strong>xargs<\/strong>\u00a0which is used to build and execute command lines from standard input. Below is an example of a pipeline which uses\u00a0<strong>xargs<\/strong>, this command is used to\u00a0<a href=\"https:\/\/www.tecmint.com\/copy-file-to-multiple-directories-in-linux\/\" target=\"_blank\" rel=\"noopener\">copy a file into multiple directories in Linux<\/a>:<\/p>\n<pre>$ echo \/home\/aaronkilik\/test\/ \/home\/aaronkilik\/tmp | xargs -n 1 cp -v \/home\/aaronkilik\/bin\/sys_info.sh\r\n<\/pre>\n<div id=\"attachment_24177\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Copy-Files-to-Multiple-Directories.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24177\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Copy-Files-to-Multiple-Directories.png\" alt=\"Copy Files to Multiple Directories\" width=\"736\" height=\"97\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Copy Files to Multiple Directories<\/p>\n<\/div>\n<p>And the options:<\/p>\n<ol>\n<li><code>-n 1<\/code>\u00a0\u2013 instructs xargs to use at most one argument per command line and send to the\u00a0<a href=\"https:\/\/www.tecmint.com\/progress-monitor-check-progress-of-linux-commands\/\" target=\"_blank\" rel=\"noopener\">cp command<\/a><\/li>\n<li><code>cp<\/code>\u00a0\u2013 copies the file<\/li>\n<li><code>-v<\/code>\u00a0\u2013\u00a0<a href=\"https:\/\/www.tecmint.com\/monitor-copy-backup-tar-progress-in-linux-using-pv-command\/\" target=\"_blank\" rel=\"noopener\">displays progress of copy command<\/a>.<\/li>\n<\/ol>\n<p>For more usage options and info, read through the<strong>\u00a0xargs<\/strong>\u00a0man page:<\/p>\n<pre>$ man xargs \r\n<\/pre>\n<p>A\u00a0<strong>tee<\/strong>\u00a0command reads from standard input and writes to standard output and files. We can demonstrate how\u00a0<strong>tee<\/strong>works as follows:<\/p>\n<pre>$ echo \"Testing how tee command works\" | tee file1 \r\n<\/pre>\n<div id=\"attachment_24178\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/tee-command-example.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24178\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/tee-command-example.png\" alt=\"tee Command Example\" width=\"715\" height=\"135\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">tee Command Example<\/p>\n<\/div>\n<p><a href=\"https:\/\/www.tecmint.com\/linux-file-operations-commands\/\" target=\"_blank\" rel=\"noopener\">File or text filters<\/a>\u00a0are commonly used with pipes for\u00a0<a href=\"https:\/\/www.tecmint.com\/linux-file-operations-commands\/\" target=\"_blank\" rel=\"noopener\">effective Linux file operations<\/a>, to process information in powerful ways such as restructuring output of commands (this can be vital for\u00a0<a href=\"https:\/\/www.tecmint.com\/linux-performance-monitoring-and-file-system-statistics-reports\/\" target=\"_blank\" rel=\"noopener\">generation of useful Linux reports<\/a>), modifying text in files plus several other\u00a0<a href=\"https:\/\/www.tecmint.com\/automating-linux-system-administration-tasks\/\" target=\"_blank\" rel=\"noopener\">Linux system administration tasks<\/a>.<\/p>\n<p>To learn more about Linux filters and pipes, read this article\u00a0<a href=\"https:\/\/www.tecmint.com\/find-top-ip-address-accessing-apache-web-server\/\" target=\"_blank\" rel=\"noopener\">Find Top 10 IP Addresses Accessing Apache Server<\/a>, shows a useful example of using filters and pipes.<\/p>\n<p>In this article, we explained the fundamentals of I\/O redirection in Linux. Remember to share your thoughts via the feedback section below.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/linux-io-input-output-redirection-operators\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the most important and\u00a0interesting topics under Linux administration\u00a0is I\/O redirection. This feature of the command line enables you to redirect the input and\/or output of commands from and\/or to files, or join multiple commands together using pipes to form what is known as a \u201ccommand pipeline\u201d. All the commands that we run fundamentally &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/14\/learn-the-basics-of-how-linux-i-o-input-output-redirection-works\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Learn The Basics of How Linux I\/O (Input\/Output) Redirection Works&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11458","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11458","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=11458"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11458\/revisions"}],"predecessor-version":[{"id":11459,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11458\/revisions\/11459"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=11458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=11458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=11458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}