{"id":51,"date":"2018-10-10T16:34:00","date_gmt":"2018-10-10T08:34:00","guid":{"rendered":"https:\/\/yeslq.com\/20181051.html"},"modified":"2018-11-11T16:11:07","modified_gmt":"2018-11-11T08:11:07","slug":"the-linux-command-line-i-o-redirection","status":"publish","type":"post","link":"https:\/\/yeslq.com\/?p=51","title":{"rendered":"The Linux Command Line&#8212;I\/O Redirection"},"content":{"rendered":"<h1 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\"><i style=\"font-size: x-small;\"><a href=\"http:\/\/linuxcommand.org\/tlcl.php\" style=\"color: #002740;\">The Linux Command Line&nbsp;<\/a><\/i><span style=\"font-size: xx-small;\">by William Shotts<\/span><\/h1>\n<div><span style=\"font-size: xx-small;\"><br \/><\/span><\/div>\n<div><span style=\"font-size: xx-small;\"><\/p>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">In this lesson, we will explore a powerful feature used by many command line programs called&nbsp;<i>input\/output redirection<\/i>. As we have seen, many commands such as&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">ls<\/tt>&nbsp;print their output on the display. This does not have to be the case, however. By using some special notations we can&nbsp;<i>redirect<\/i>&nbsp;the output of many commands to files, devices, and even to the input of other commands.<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\"><span style=\"font-size: small;\">Standard Output<\/span><\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Most command line programs that display their results do so by sending their results to a facility called&nbsp;<i>standard output<\/i>. By default, standard output directs its contents to the display. To redirect standard output to a file, the &#8220;&gt;&#8221; character is used like this:<\/div>\n<div style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">[me@linuxbox me]$<\/tt>&nbsp;<tt style=\"background: transparent; color: white; font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">ls &gt; file_list.txt<\/tt><\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">In this example, the&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">ls<\/tt>&nbsp;command is executed and the results are written in a file named file_list.txt. Since the output of&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">ls<\/tt>&nbsp;was redirected to the file, no results appear on the display.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Each time the command above is repeated, file_list.txt is overwritten from the beginning with the output of the command&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">ls<\/tt>. If you want the new results to be&nbsp;<i>appended<\/i>&nbsp;to the file instead, use &#8220;&gt;&gt;&#8221; like this:<\/div>\n<div style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">[me@linuxbox me]$<\/tt>&nbsp;<tt style=\"background: transparent; color: white; font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">ls &gt;&gt; file_list.txt<\/tt><\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">When the results are appended, the new results are added to the end of the file, thus making the file longer each time the command is repeated. If the file does not exist when you attempt to append the redirected output, the file will be created.<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\"><span style=\"font-size: small;\">Standard Input<\/span><\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Many commands can accept input from a facility called&nbsp;<i>standard input<\/i>. By default, standard input gets its contents from the keyboard, but like standard output, it can be redirected. To redirect standard input from a file instead of the keyboard, the &#8220;&lt;&#8221; character is used like this:<\/div>\n<div style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">[me@linuxbox me]$<\/tt>&nbsp;<tt style=\"background: transparent; color: white; font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">sort &lt; file_list.txt<\/tt><\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">In the example above, we used the&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/sort1.html\" style=\"color: #002740;\">sort<\/a><\/tt>&nbsp;command to process the contents of file_list.txt. The results are output on the display since the standard output was not redirected. We could redirect standard output to another file like this:<\/div>\n<div style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">[me@linuxbox me]$<\/tt>&nbsp;<tt style=\"background: transparent; color: white; font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">sort &lt; file_list.txt &gt; sorted_file_list.txt<\/tt><\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">As you can see, a command can have both its input and output redirected. Be aware that the order of the redirection does not matter. The only requirement is that the redirection operators (the &#8220;&lt;&#8221; and &#8220;&gt;&#8221;) must appear after the other options and arguments in the command.<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\"><span style=\"font-size: small;\">Pipelines<\/span><\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">The most useful and powerful thing you can do with I\/O redirection is to connect multiple commands together with what are called&nbsp;<i>pipelines<\/i>. With pipelines, the standard output of one command is fed into the standard input of another. Here is my absolute favorite:<\/div>\n<div style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">[me@linuxbox me]$<\/tt>&nbsp;<tt style=\"background: transparent; color: white; font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">ls -l | less<\/tt><\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">In this example, the output of the&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">ls<\/tt>&nbsp;command is fed into&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">less<\/tt>. By using this&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">\"| less\"<\/tt>&nbsp;trick, you can make any command have scrolling output. I use this technique all the time.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">By connecting commands together, you can acomplish amazing feats. Here are some examples you&#8217;ll want to try:<\/div>\n<table border=\"\" cellpadding=\"8\" style=\"background-color: white; color: black; font-family: verdana, arial, helvetica, sans-serif;\" summary=\"Examples of various commands used together with pipelines.\">\n<caption>Examples of commands used together with pipelines<\/caption>\n<tbody>\n<tr>\n<th><strong>Command<\/strong><\/th>\n<th><strong>What it does<\/strong><\/th>\n<\/tr>\n<tr>\n<td><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">ls -lt |&nbsp;<a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/head1.html\" style=\"color: #002740;\">head<\/a><\/tt><\/td>\n<td>Displays the 10 newest files in the current directory.<\/td>\n<\/tr>\n<tr>\n<td><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/du1.html\" style=\"color: #002740;\">du<\/a>&nbsp;| sort -nr<\/tt><\/td>\n<td>Displays a list of directories and how much space they consume, sorted from the largest to the smallest.<\/td>\n<\/tr>\n<tr>\n<td nowrap=\"\"><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/find1.html\" style=\"color: #002740;\">find<\/a>&nbsp;. -type f -print |&nbsp;<a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/wc1.html\" style=\"color: #002740;\">wc<\/a>&nbsp;-l<\/tt><\/td>\n<td>Displays the total number of files in the current working directory and all of its subdirectories.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\"><a href=\"https:\/\/draft.blogger.com\/null\" name=\"filters\"><span style=\"font-size: small;\">Filters<\/span><\/a><\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">One kind of program frequently used in pipelines is called&nbsp;<i>filters<\/i>. Filters take standard input and perform an operation upon it and send the results to standard output. In this way, they can be combined to process information in powerful ways. Here are some of the common programs that can act as filters:<\/div>\n<table border=\"\" cellpadding=\"8\" style=\"background-color: white; color: black; font-family: verdana, arial, helvetica, sans-serif;\" summary=\"Talbe of common filter commands.\">\n<caption>Common filter commands<\/caption>\n<tbody>\n<tr>\n<th><strong>Program<\/strong><\/th>\n<th><strong>What it does<\/strong><\/th>\n<\/tr>\n<tr>\n<td><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/sort1.html\" style=\"color: #002740;\">sort<\/a><\/tt><\/td>\n<td>Sorts standard input then outputs the sorted result on standard output.<\/td>\n<\/tr>\n<tr>\n<td><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/uniq1.html\" style=\"color: #002740;\">uniq<\/a><\/tt><\/td>\n<td>Given a sorted stream of data from standard input, it removes duplicate lines of data (i.e., it makes sure that every line is unique).<\/td>\n<\/tr>\n<tr>\n<td><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/grep1.html\" style=\"color: #002740;\">grep<\/a><\/tt><\/td>\n<td>Examines each line of data it receives from standard input and outputs every line that contains a specified pattern of characters.<\/td>\n<\/tr>\n<tr>\n<td><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/fmt1.html\" style=\"color: #002740;\">fmt<\/a><\/tt><\/td>\n<td>Reads text from standard input, then outputs formatted text on standard output.<\/td>\n<\/tr>\n<tr>\n<td><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/pr1.html\" style=\"color: #002740;\">pr<\/a><\/tt><\/td>\n<td>Takes text input from standard input and splits the data into pages with page breaks, headers and footers in preparation for printing.<\/td>\n<\/tr>\n<tr>\n<td><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/head1.html\" style=\"color: #002740;\">head<\/a><\/tt><\/td>\n<td>Outputs the first few lines of its input. Useful for getting the header of a file.<\/td>\n<\/tr>\n<tr>\n<td><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/tail1.html\" style=\"color: #002740;\">tail<\/a><\/tt><\/td>\n<td>Outputs the last few lines of its input. Useful for things like getting the most recent entries from a log file.<\/td>\n<\/tr>\n<tr>\n<td><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/tr1.html\" style=\"color: #002740;\">tr<\/a><\/tt><\/td>\n<td>Translates characters. Can be used to perform tasks such as upper\/lowercase conversions or changing line termination characters from one type to another (for example, converting DOS text files into Unix style text files).<\/td>\n<\/tr>\n<tr>\n<td><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/sed1.html\" style=\"color: #002740;\">sed<\/a><\/tt><\/td>\n<td>Stream editor. Can perform more sophisticated text translations than&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">tr<\/tt>.<\/td>\n<\/tr>\n<tr>\n<td><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/gawk1.html\" style=\"color: #002740;\">awk<\/a><\/tt><\/td>\n<td>An entire programming language designed for constructing filters. Extremely powerful.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><br style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\" \/><\/p>\n<div style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 1em;\">\n<h2 style=\"font-weight: normal;\"><span style=\"font-size: small;\">Performing tasks with pipelines<\/span><\/h2>\n<ol>\n<li><strong>Printing from the command line.<\/strong>&nbsp;Linux provides a program called&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/lpr1.html\" style=\"color: #002740;\">lpr<\/a><\/tt>&nbsp;that accepts standard input and sends it to the printer. It is often used with pipes and filters. Here are a couple of examples:\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\"><br \/>cat poorly_formatted_report.txt | fmt | pr | lpr<br \/><br \/>cat unsorted_list_with_dupes.txt | sort | uniq | pr | lpr<br \/><\/tt><br \/><\/pre>\n<p>In the first example, we use&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">cat<\/tt>&nbsp;to read the file and output it to standard output, which is piped into the standard input of&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">fmt. fmt<\/tt>&nbsp;formats the text into neat paragraphs and outputs it to standard output, which is piped into the standard input of&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">pr. pr<\/tt>&nbsp;splits the text neatly into pages and outputs it to standard output, which is piped into the standard input of&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">lpr. lpr<\/tt>&nbsp;takes its standard input and sends it to the printer.<br \/>The second example starts with an unsorted list of data with duplicate entries. First,&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">cat<\/tt>&nbsp;sends the list into&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">sort<\/tt>&nbsp;which sorts it and feeds it into&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">uniq<\/tt>which removes any duplicates. Next&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">pr<\/tt>&nbsp;and&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">lpr<\/tt>&nbsp;are used to paginate and print the list.<\/li>\n<li><strong>Viewing the contents of tar files<\/strong>&nbsp;Often you will see software distributed as a&nbsp;<i>gzipped tar file<\/i>. This is a traditional Unix style tape archive file (created with&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/tar1.html\" style=\"color: #002740;\">tar<\/a><\/tt>) that has been compressed with&nbsp;<tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\"><a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/gzip1.html\" style=\"color: #002740;\">gzip<\/a><\/tt>. You can recognize these files by their traditional file extensions, &#8220;.tar.gz&#8221; or &#8220;.tgz&#8221;. You can use the following command to view the directory of such a file on a Linux system:\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">tar tzvf name_of_file.tar.gz | less<br \/><\/tt><br \/><\/pre>\n<p><\/li>\n<\/ol>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; padding: 1em;\">\n<div style=\"text-align: right;\"><\/div>\n<\/div>\n<p><\/span><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Linux Command Line&nbsp;by William Shotts In this l &#8230; <a title=\"The Linux Command Line&#8212;I\/O Redirection\" class=\"read-more\" href=\"https:\/\/yeslq.com\/?p=51\" aria-label=\"\u9605\u8bfb The Linux Command Line&#8212;I\/O Redirection\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4,6],"tags":[],"class_list":["post-51","post","type-post","status-publish","format-standard","hentry","category-linux","category-shell","category-the-linux-command-line"],"_links":{"self":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/51","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=51"}],"version-history":[{"count":1,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/51\/revisions"}],"predecessor-version":[{"id":113,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/51\/revisions\/113"}],"wp:attachment":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=51"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=51"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}