{"id":128,"date":"2025-10-26T12:00:00","date_gmt":"2025-10-26T12:00:00","guid":{"rendered":"https:\/\/rezachegini.com\/?p=128"},"modified":"2026-10-30T06:42:49","modified_gmt":"2026-10-30T06:42:49","slug":"linux-data-streams-and-redirection","status":"publish","type":"post","link":"https:\/\/rezachegini.com\/?p=128","title":{"rendered":"Linux Data Streams and Redirection"},"content":{"rendered":"<p class=\"wp-block-paragraph\">This post explains how input, output, and errors work in Linux, It also shows how to use redirection and pipes to connect commands and manage results in a simple way.<\/p><h3 class=\"wp-block-heading\">What You\u2019ll Learn<\/h3><ul class=\"wp-block-list\"><li>What are data streams (stdin, stdout, stderr)<\/li><li>How to redirect outputs and errors<\/li><li>How to use pipes to connect commands<\/li><li>Real examples from daily use<\/li><\/ul><h3 class=\"wp-block-heading\">1. What Are Streams<\/h3><p class=\"wp-block-paragraph\">In Linux, every program has three main data streams:<\/p><ol class=\"wp-block-list\"><li>stdin (0) \u2013 standard input, usually from keyboard<\/li><li>stdout (1) \u2013 standard output, normal messages<\/li><li>stderr (2) \u2013 standard error, error messages<\/li><\/ol><p class=\"wp-block-paragraph\">Think of them like water pipes. Data flows in and out through these pipes.<\/p><p class=\"wp-block-paragraph\">This helps us send data to files, separate errors, or connect commands together.<\/p><h3 class=\"wp-block-heading\">2. Redirecting Output<\/h3><h3 class=\"wp-block-heading\">Send output to a file ( &gt; )<\/h3><pre class=\"wp-block-code\"><code>echo \"Hello World\" &gt; output.txt<\/code><\/pre><p class=\"wp-block-paragraph\">This creates or overwrites the file.<\/p><h3 class=\"wp-block-heading\">Add output instead of overwrite ( &gt;&gt; )<\/h3><pre class=\"wp-block-code\"><code>echo \"Line 1\" &gt; file.txt\necho \"Line 2\" &gt;&gt; file.txt<\/code><\/pre><p class=\"wp-block-paragraph\">Now the file has both lines.<\/p><h3 class=\"wp-block-heading\">Example use<\/h3><pre class=\"wp-block-code\"><code>df -h &gt; disk_usage.txt\ndate &gt;&gt; my_log.txt\nps aux &gt; running_processes.txt<\/code><\/pre><h3 class=\"wp-block-heading\">3. Redirecting Errors<\/h3><p class=\"wp-block-paragraph\">Normal redirection saves only normal output, not errors. To save errors, we use 2&gt;.<\/p><pre class=\"wp-block-code\"><code>ls \/fake_folder 2&gt; errors.txt<\/code><\/pre><h3 class=\"wp-block-heading\">Redirect both output and errors<\/h3><pre class=\"wp-block-code\"><code>command &gt; output.txt 2&gt; errors.txt     # separate files\ncommand &gt; all.txt 2&gt;&amp;1                 # same file\ncommand &amp;&gt; all.txt                     # simpler way<\/code><\/pre><p class=\"wp-block-paragraph\">To ignore errors completely:<\/p><pre class=\"wp-block-code\"><code>command 2&gt; \/dev\/null<\/code><\/pre><h3 class=\"wp-block-heading\">4. \/dev\/null \u2013 The Black Hole<\/h3><p class=\"wp-block-paragraph\">\/dev\/null is a special file that throws away everything sent to it.<\/p><p class=\"wp-block-paragraph\">Examples<\/p><pre class=\"wp-block-code\"><code>command &amp;&gt; \/dev\/null           # ignore all output\nping -c 1 google.com &amp;&gt; \/dev\/null\napt update 2&gt; \/dev\/null<\/code><\/pre><p class=\"wp-block-paragraph\">Use this when you want your command to run quietly.<\/p><h3 class=\"wp-block-heading\">5. Pipes ( | )<\/h3><p class=\"wp-block-paragraph\">A pipe sends the output of one command into another command.<\/p><pre class=\"wp-block-code\"><code>command1 | command2<\/code><\/pre><h3 class=\"wp-block-heading\">Examples<\/h3><pre class=\"wp-block-code\"><code>ls | wc -l                       # count files\nps aux | grep nginx              # find a process\ndu -sh * | sort -hr | head       # largest files\ncat file.txt | grep \"error\"      # search in a file<\/code><\/pre><h3 class=\"wp-block-heading\">Combine many commands<\/h3><pre class=\"wp-block-code\"><code>cat \/var\/log\/syslog | grep \"error\" | sort | uniq | wc -l<\/code><\/pre><p class=\"wp-block-paragraph\">This reads a log, finds errors, sorts them, removes duplicates, and counts them.<\/p><h3 class=\"wp-block-heading\">6. Input Redirection (&lt;)<\/h3><p class=\"wp-block-paragraph\">Instead of typing input manually, we can take it from a file.<\/p><pre class=\"wp-block-code\"><code>wc -l &lt; file.txt\nsort &lt; unsorted.txt<\/code><\/pre><h3 class=\"wp-block-heading\">Here Document (&lt;&lt;)<\/h3><pre class=\"wp-block-code\"><code>cat &lt;&lt; EOF &gt; file.txt\nLine 1\nLine 2\nEOF<\/code><\/pre><p class=\"wp-block-paragraph\">This creates a file quickly with multiple lines.<\/p><h3 class=\"wp-block-heading\">7. Combine Redirection and Pipes<\/h3><h3 class=\"wp-block-heading\">Save output and show it<\/h3><pre class=\"wp-block-code\"><code>command | tee output.txt<\/code><\/pre><h3 class=\"wp-block-heading\">Add instead of overwrite<\/h3><pre class=\"wp-block-code\"><code>command | tee -a output.txt<\/code><\/pre><p class=\"wp-block-paragraph\">Example<\/p><pre class=\"wp-block-code\"><code>sudo apt update | tee update.log<\/code><\/pre><h3 class=\"wp-block-heading\">8. Quick Reference<\/h3><p class=\"wp-block-paragraph\">Output Redirection<\/p><pre class=\"wp-block-code\"><code>cmd &gt; file         \u2192 save output (overwrite)\ncmd &gt;&gt; file        \u2192 add output (append)\ncmd 2&gt; file        \u2192 save errors\ncmd &amp;&gt; file        \u2192 save output and errors\ncmd &gt; \/dev\/null    \u2192 discard output\ncmd 2&gt; \/dev\/null   \u2192 discard errors\ncmd &amp;&gt; \/dev\/null   \u2192 discard everything<\/code><\/pre><p class=\"wp-block-paragraph\">Pipes and Input<\/p><pre class=\"wp-block-code\"><code>cmd1 | cmd2        \u2192 send output of first command to the second\ncmd &lt; file         \u2192 read input from a file\ncmd | tee file     \u2192 show and save output at the same time<\/code><\/pre><h3 class=\"wp-block-heading\">9. Common Mistakes<\/h3><p class=\"wp-block-paragraph\">Wrong<\/p><pre class=\"wp-block-code\"><code>ls &gt; file.txt | grep txt<\/code><\/pre><p class=\"wp-block-paragraph\">Output goes to file, so pipe gets nothing.<\/p><p class=\"wp-block-paragraph\">Right<\/p><pre class=\"wp-block-code\"><code>ls | grep txt &gt; file.txt<\/code><\/pre><p class=\"wp-block-paragraph\">Wrong<\/p><pre class=\"wp-block-code\"><code>command &gt; output.txt   # misses errors<\/code><\/pre><p class=\"wp-block-paragraph\">Right<\/p><pre class=\"wp-block-code\"><code>command &amp;&gt; output.txt<\/code><\/pre><p class=\"wp-block-paragraph\">Wrong<\/p><pre class=\"wp-block-code\"><code>echo \"Entry\" &gt; log.txt   # overwrites log<\/code><\/pre><p class=\"wp-block-paragraph\">Right<\/p><pre class=\"wp-block-code\"><code>echo \"Entry\" &gt;&gt; log.txt<\/code><\/pre><h3 class=\"wp-block-heading\">10. Real Examples<\/h3><pre class=\"wp-block-code\"><code># Log script output\n.\/my_script.sh &amp;&gt; script_log.txt\n\n# Backup with progress\ntar -czf backup.tar.gz \/home\/reza | tee backup.log\n\n# Find today\u2019s errors\ngrep \"$(date +%Y-%m-%d)\" \/var\/log\/*.log 2&gt;\/dev\/null | grep -i error\n\n# Clean old logs\ntail -100 \/var\/log\/app.log &gt; temp.log &amp;&amp; mv temp.log \/var\/log\/app.log\n\n# Monitor memory\nfree -h | grep Mem | tee -a memory_log.txt<\/code><\/pre><h3 class=\"wp-block-heading\">Key Points I Learned<\/h3><ol class=\"wp-block-list\"><li>Three data streams: stdin (0), stdout (1), stderr (2)<\/li><li>&gt; overwrites, &gt;&gt; appends<\/li><li>2&gt; redirects errors<\/li><li>&amp;&gt; redirects both output and errors<\/li><li>\/dev\/null discards output<\/li><li>Pipes | connect commands<\/li><li>tee shows and saves output<\/li><li>Redirection order matters<\/li><\/ol><h3 class=\"wp-block-heading\">My Quick Cheat Sheet<\/h3><pre class=\"wp-block-code\"><code>command &gt; file.txt         # save output\ncommand &gt;&gt; file.txt        # add output\ncommand 2&gt; errors.txt      # save errors\ncommand &amp;&gt; all.txt         # save all\ncommand 2&gt; \/dev\/null       # ignore errors\ncommand1 | command2        # pipe\ncommand | tee file.txt     # show and save<\/code><\/pre>","protected":false},"excerpt":{"rendered":"<p>This post explains how input, output, and errors work in Linux, It also shows how to use redirection and pipes to connect&hellip;<\/p>\n","protected":false},"author":2,"featured_media":130,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_rezaops_part":"2","_rezaops_linkedin_url":"https:\/\/www.linkedin.com\/pulse\/linux-data-streams-redirection-reza-chegini-5drwc\/","footnotes":""},"categories":[10,74],"tags":[79,78,8,76,80],"series":[75],"class_list":["post-128","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-linux","tag-bash","tag-command-line","tag-devops","tag-linux","tag-redirection","series-linux-fundamentals"],"jetpack_featured_media_url":"https:\/\/rezachegini.com\/wp-content\/uploads\/2026\/10\/linux-data-streams-redirection-cover.png","_links":{"self":[{"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/posts\/128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=128"}],"version-history":[{"count":1,"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/posts\/128\/revisions"}],"predecessor-version":[{"id":129,"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/posts\/128\/revisions\/129"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/media\/130"}],"wp:attachment":[{"href":"https:\/\/rezachegini.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=128"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=%2Fwp%2Fv2%2Fseries&post=128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}