{"id":6724,"date":"2017-05-08T16:56:10","date_gmt":"2017-05-08T08:56:10","guid":{"rendered":"http:\/\/rmohan.com\/?p=6724"},"modified":"2017-05-08T16:56:10","modified_gmt":"2017-05-08T08:56:10","slug":"rhel-7-rhcsa-notes-input-output-redirection","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=6724","title":{"rendered":"RHEL 7 \u2013 RHCSA Notes \u2013 input \/ output redirection"},"content":{"rendered":"<p>\nThree standard file descriptors :<\/p>\n<p>1. stdin 0 &#8211; Standard input to the program.<br \/>\n2. stdout 1 &#8211; Standard output from the program.<br \/>\n3. stderr 2 &#8211; Standard error output from the program.<br \/>\nPURPOSE\tCOMMAND<br \/>\nredirect std output to filename\t> filename or 1> filename<br \/>\nappend std out to filename\t>> filename<br \/>\nappend std out and std err to filename\t>> filename 2>&#038;1 or 1>> filename 2>&#038;1<br \/>\ntake input from filename\t< filename or 0 < filename\nredirect std error to filename\t2> filename<br \/>\nredirect std out and std error to filename\t1> filename 2>&#038;1 or > filename 2>&#038;1<br \/>\nSome examples of using I\/O redirection<\/p>\n<p># cat goodfile badfile 1> output 2> errors<br \/>\nThis command redirects the normal output (contents of goodfile) to the file output and sends any errors (about badfile not existing, for example) to the file errors.<\/p>\n<p># mail user_id < textfile 2> errors<br \/>\nThis command redirects the input for the mail command to come from file textfile and any errors are redirected to the file errors.<\/p>\n<p># find \/ -name xyz -print 1> abc 2>&#038;1<br \/>\nThis command redirects the normal output to the file abc. The construct \u201c2>&#038;1\u201d says \u201csend error output to the same place we directed normal output\u201d.<\/p>\n<p>Note that the order is important; command 2>&#038;1 1>file does not do the same as command 1>file 2>&#038;1. This is because the 2>&#038;1 construction means redirect standard error to the place where standard output currently goes. The construction command 2>&#038;1 1>file will first redirect standard error to where standard output goes (probably the terminal, which is where standard error goes by default anyway) then will redirect standard output to file. This is probably not what was intended.<br \/>\n# ( grep Bob filex > out ) 2> err<br \/>\n\u2013 any output of the grep command is sent to the file out and any errors are sent to the file err.<\/p>\n<p># find . -name xyz -print 2>\/dev\/null<br \/>\nThis runs the find command, but sends any error output (due to inaccessible directories, for example), to \/dev\/null. Use with care, unless error output really is of no interest.<\/p>\n","protected":false},"excerpt":{"rendered":"<p> Three standard file descriptors :<\/p>\n<p>1. stdin 0 &#8211; Standard input to the program. 2. stdout 1 &#8211; Standard output from the program. 3. stderr 2 &#8211; Standard error output from the program. PURPOSE COMMAND redirect std output to filename > filename or 1> filename append std out to filename >> filename append std [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6724"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6724"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6724\/revisions"}],"predecessor-version":[{"id":6725,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6724\/revisions\/6725"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}