<blockquote><p>In Linux, stream means data flow, which is a string of data read in a certain order, so the direction of the data flow is the reading order of the data flow. The process of the Linux system importing the output results after reading the data into other files is called redirected data flow. After inputting a command under Linux and running it, two results will be displayed on the screen: the result of successful operation is the standard output, and the result of the failed operation is the standard error output; if not processed, they will be displayed on the screen and redirected through the data flow. You can save it to other files. </p></blockquote>
<p><img src="https://img.php.cn/upload/article/202303/17/2023031709544952034.jpg" alt="What does stream mean in linux?" ></p>
<p>#The operating environment of this tutorial: linux7.3 system, Dell G3 computer. </p>
<p><strong>In Linux, stream means data flow. </strong></p>
<h2><strong>What does stream (data stream) mean?</strong></h2>
<p>stream (data stream) is a string of data read in a certain order, so the direction of the data flow It is the reading order of the data stream. </p>
<p>Data stream is originally a concept used in the field of communications and represents a sequence of digitally encoded signals used in transmission of information. This concept was first proposed by Henzinger in 1998 in Document 87. He defined a data stream as "a sequence of data that can only be read once in a predetermined order." </p>
<p>Linux system redirection data flow is to import the output results after reading the data into other files. </p>
<h2><strong>Linux data flow redirection</strong></h2>
<p>Data flow redirection is simply to locate the input data source or output data of a certain command to other files. Everything under Linux is a file. The files here can be device files, ordinary files, etc. </p>
<p><a id="1_stdin_stdout_stderr_5"></a><span style="font-size: 18px;"><strong>1. stdin stdout stderr</strong></span></p>
<blockquote><p><code>stdin</code>: standard input. Refers to some information provided for the command to run. <br><code>stdout</code>: Standard output. Refers to the correct information returned by command execution. <br><code>stderr</code>: Standard error output. Refers to the error message returned after the command execution fails. </p></blockquote>
<p>The relationship between these three and the command is usually as follows:</p>
<div class="mermaid sequence-diagram"><svg id="mermaid-svg-43Q0mtxvjga7WpCa" width="100%" xmlns="http://www.w3.org/2000/svg" height="158" style="max-width: 436.46875px;" viewbox="0 0 436.46875 158"><style>#mermaid-svg-43Q0mtxvjga7WpCa {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-43Q0mtxvjga7WpCa .error-icon{fill:#552222;}#mermaid-svg-43Q0mtxvjga7WpCa .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-43Q0mtxvjga7WpCa .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-43Q0mtxvjga7WpCa .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-43Q0mtxvjga7WpCa .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-43Q0mtxvjga7WpCa .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-43Q0mtxvjga7WpCa .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-43Q0mtxvjga7WpCa .marker{fill:#333333;stroke:#333333;}#mermaid-svg-43Q0mtxvjga7WpCa .marker.cross{stroke:#333333;}#mermaid-svg-43Q0mtxvjga7WpCa svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-43Q0mtxvjga7WpCa .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-43Q0mtxvjga7WpCa .cluster-label text{fill:#333;}#mermaid-svg-43Q0mtxvjga7WpCa .cluster-label span{color:#333;}#mermaid-svg-43Q0mtxvjga7WpCa .label text,#mermaid-svg-43Q0mtxvjga7WpCa span{fill:#333;color:#333;}#mermaid-svg-43Q0mtxvjga7WpCa .node rect,#mermaid-svg-43Q0mtxvjga7WpCa .node circle,#mermaid-svg-43Q0mtxvjga7WpCa .node ellipse,#mermaid-svg-43Q0mtxvjga7WpCa .node polygon,#mermaid-svg-43Q0mtxvjga7WpCa .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-43Q0mtxvjga7WpCa .node .label{text-align:center;}#mermaid-svg-43Q0mtxvjga7WpCa .node.clickable{cursor:pointer;}#mermaid-svg-43Q0mtxvjga7WpCa .arrowheadPath{fill:#333333;}#mermaid-svg-43Q0mtxvjga7WpCa .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-43Q0mtxvjga7WpCa .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-43Q0mtxvjga7WpCa .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-43Q0mtxvjga7WpCa .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-43Q0mtxvjga7WpCa .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-43Q0mtxvjga7WpCa .cluster text{fill:#333;}#mermaid-svg-43Q0mtxvjga7WpCa .cluster span{color:#333;}#mermaid-svg-43Q0mtxvjga7WpCa div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-43Q0mtxvjga7WpCa :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}</style>
<g><g class="output"><g class="clusters"></g><g class="edgePaths"><g class="edgePath LS-A LE-B" style="opacity: 1;" id="L-A-B"><path class="path" d="M54.40625,79L61.493489583333336,79C68.58072916666667,79,82.75520833333333,79,96.9296875,79C111.10416666666667,79,125.27864583333333,79,132.36588541666666,79L139.453125,79" marker-end="url(#arrowhead34)" style="fill:none"></path><defs><marker id="arrowhead34" viewbox="0 0 10 10" refx="9" refy="5" markerunits="strokeWidth" markerwidth="8" markerheight="6" orient="auto"><path d="M 0 0 L 10 5 L 0 10 z" class="arrowheadPath" style="stroke-width: 1; stroke-dasharray: 1, 0;"></path></marker></defs></g><g class="edgePath LS-B LE-C" style="opacity: 1;" id="L-B-C"><path class="path" d="M229.451171875,56L237.5322265625,51.833333333333336C245.61328125,47.666666666666664,261.775390625,39.333333333333336,277.8069661458333,35.166666666666664C293.8385416666667,31,309.7395833333333,31,317.6901041666667,31L325.640625,31" marker-end="url(#arrowhead35)" style="fill:none"></path><defs><marker id="arrowhead35" viewbox="0 0 10 10" refx="9" refy="5" markerunits="strokeWidth" markerwidth="8" markerheight="6" orient="auto"><path d="M 0 0 L 10 5 L 0 10 z" class="arrowheadPath" style="stroke-width: 1; stroke-dasharray: 1, 0;"></path></marker></defs></g><g class="edgePath LS-B LE-D" style="opacity: 1;" id="L-B-D"><path class="path" d="M229.451171875,102L237.5322265625,106.16666666666667C245.61328125,110.33333333333333,261.775390625,118.66666666666667,277.8069661458333,122.83333333333333C293.8385416666667,127,309.7395833333333,127,317.6901041666667,127L325.640625,127" marker-end="url(#arrowhead36)" style="fill:none"></path><defs><marker id="arrowhead36" viewbox="0 0 10 10" refx="9" refy="5" markerunits="strokeWidth" markerwidth="8" markerheight="6" orient="auto"><path d="M 0 0 L 10 5 L 0 10 z" class="arrowheadPath" style="stroke-width: 1; stroke-dasharray: 1, 0;"></path></marker></defs></g></g><g class="edgeLabels"><g class="edgeLabel" style="opacity: 1;" transform="translate(96.9296875,79)"><g transform="translate(-17.5234375,-13)" class="label"><rect rx="0" ry="0" width="35.046875" height="26"></rect><foreignobject width="35.046875" height="26"><div style="display: inline-block; white-space: nowrap;">
<span id="L-L-A-B" class="edgeLabel L-LS-A' L-LE-B">stdin</span>
</div></foreignobject></g></g><g class="edgeLabel" style="opacity: 1;" transform="translate(277.9375,31)"><g transform="translate(-21.453125,-13)" class="label"><rect rx="0" ry="0" width="42.90625" height="26"></rect><foreignobject width="42.90625" height="26"><div style="display: inline-block; white-space: nowrap;">
<span id="L-L-B-C" class="edgeLabel L-LS-B' L-LE-C">stderr</span>
</div></foreignobject></g></g><g class="edgeLabel" style="opacity: 1;" transform="translate(277.9375,127)"><g transform="translate(-22.703125,-13)" class="label"><rect rx="0" ry="0" width="45.40625" height="26"></rect><foreignobject width="45.40625" height="26"><div style="display: inline-block; white-space: nowrap;">
<span id="L-L-B-D" class="edgeLabel L-LS-B' L-LE-D">stdout</span>
</div></foreignobject></g></g></g><g class="nodes"><g class="node default" style="opacity: 1;" id="flowchart-A-18" transform="translate(31.203125,79)"><rect rx="5" ry="5" x="-23.203125" y="-23" width="46.40625" height="46" class="label-container"></rect><g class="label" transform="translate(0,0)"><g transform="translate(-13.203125,-13)"><foreignobject width="26.40625" height="26"><div style="display: inline-block; white-space: nowrap;">File</div></foreignobject></g></g></g><g class="node default" style="opacity: 1;" id="flowchart-B-19" transform="translate(184.84375,79)"><rect rx="5" ry="5" x="-45.390625" y="-23" width="90.78125" height="46" class="label-container"></rect><g class="label" transform="translate(0,0)"><g transform="translate(-35.390625,-13)"><foreignobject width="70.78125" height="26"><div style="display: inline-block; white-space: nowrap;">Command</div></foreignobject></g></g></g><g class="node default" style="opacity: 1;" id="flowchart-C-21" transform="translate(377.0546875,31)"><rect rx="5" ry="5" x="-51.4140625" y="-23" width="102.828125" height="46" class="label-container"></rect><g class="label" transform="translate(0,0)"><g transform="translate(-41.4140625,-13)"><foreignobject width="82.828125" height="26"><div style="display: inline-block; white-space: nowrap;">Screen/File</div></foreignobject></g></g></g><g class="node default" style="opacity: 1;" id="flowchart-D-23" transform="translate(377.0546875,127)"><rect rx="5" ry="5" x="-51.4140625" y="-23" width="102.828125" height="46" class="label-container"></rect><g class="label" transform="translate(0,0)"><g transform="translate(-41.4140625,-13)"><foreignobject width="82.828125" height="26"><div style="display: inline-block; white-space: nowrap;">Screen/File</div></foreignobject></g></g></g></g></g></g></svg></div>
<p>In the Linux environment, after we enter a command and run it, two results will be displayed on the screen : The result of a successful operation is the standard output, and the result of a failed operation is the standard error output. </p>
<ul style="list-style-type: disc;">
<li><p>Standard output (STDOUT): Standard output refers to the correct information returned by command execution. </p></li>
<li><p>Standard error output (STDERR): Standard error output refers to the error information returned by command execution. </p></li>
</ul>
<p>If STDOUT and STDERR are not processed, they will be displayed on the screen. They can be stored in other files through data flow redirection, and stored in files. The special characters are: </p>
<ul style="list-style-type: disc;">
<li><p>Standard input stin: code is 0, use <code><</code> or <code><<</code></p></li><li><p>Standard output stout: code is 1, use <code>></code> or <code>>></code></p></li>
<li><p> standard error output stderr: code is 2, use <code>2></code> or <code>2>></code></p></li>
</ul>
<p><a id="2__21"></a><span style="font-size: 18px;">## 2. Use of data stream redirection<strong></strong></span></p>
<ul><li>stdout<code>: standard output redirection. What is redirected here is the correct output. </code>
</li></ul>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">find / -name '.bashrc' > ~/out_right</pre><div class="contentsignin">Copy after login</div></div>This command directs the correct output to the file <p>~/out_right<code>. If </code>~/out_right<code> does not exist, it will be created automatically. Because the error message is not processed, the error output is output to the screen terminal by default. At this time, the terminal will only display the error output. </code></p>
<ul><li>stderr<code>: Standard error output redirection. What is redirected here is the error output. </code>
</li></ul>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">find / -name '.bashrc' 2> ~/out_error</pre><div class="contentsignin">Copy after login</div></div>This command directs the error output to the file <p>~/out_error<code>. If </code>~/out_error<code> does not exist, it will be created automatically. Because the correct information is not processed, the correct output is output to the screen terminal by default. At this time, the terminal will only display the correct output (if the specified file is not found, there will be no output on the screen). </code></p>
<ul><li>stdin<code>: Standard input is generally used to replace keyboard input. </code>
</li></ul>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">cat > catfile < ~/.bashrc</pre><div class="contentsignin">Copy after login</div></div></ul><p>cat<code>Without any parameters after the command, cat will read the data from the keyboard. Using standard input, direct the input source to the file </code>~/.bashrc<code>. Then locate the standard output to the </code>catfile<code> file via </code>><code>. </code>cat<code> is the command to view the file. Here, the file </code>~/.bashrc<code> is directly read and then output to </code>catfile<code>. </code><p><a id="3_2_47"></a>##3、<span style="font-size: 18px;"><strong></strong>>>,2>>,<<<code><strong></strong></code></span></p>##>><ul>(<li>ACCUMULATION<code>): Append standard output. </code>cmd > file1<strong>, the command </strong>cmd<code> will output </code>stdout<code> to </code>file1<code>, if </code>fiel1<code> previously existed, and If there is data, then </code>file1<code> will be cleared in advance and then written to </code>stdout<code>. When using the command </code>cmd >> file1<code>, the </code>file1<code> file will not be cleared in advance, but the input data will be added directly to the end of the file. </code><code></code>2>></li> (<li>cumulative<code>): Additional standard error output. Similar to </code>>><strong>. </strong><code></code><<</li> (<li> specifies the end keyword <code>): followed by the input character </code> ending with <strong>. The keyword must be on its own line, otherwise it will be invalid. </strong><code>78d6b78ee578a0115f23a6fbe6b71ed8</blockquote>There is more than one implementation method, let’s choose the better-understood writing method<p>find / -name .bashrc > list 2>&1</li>
</ul></span></p> . <p>2>&1<code> in this code can be understood as redirecting standard error output (code 2) to standard output (code 1). Then </code>> list<code>redirects standard output to the file </code>list<code>. Because standard error output is directed as standard output, all output ends up being directed to file </code>list<code>. </code><code>Related recommendations: "</code>Linux Video Tutorial</p>"<p></p></pre>
The above is the detailed content of What does stream mean in linux?. For more information, please follow other related articles on the PHP Chinese website!