~2 min read • Updated Jul 18, 2025
Every Linux program interacts with three core data streams:
- stdin: Standard input, usually from the keyboard
- stdout: Standard output, sent to the screen
- stderr: Error messages, separate from regular output
Redirecting Standard Output (stdout)
ls -l /usr/bin > ls-output.txtThis redirects output to a file instead of displaying it in the terminal.
Redirecting Standard Error (stderr)
ls -l /bin/usr 2> ls-error.txtError messages are saved to a separate file, while standard output remains visible.
Capturing Both stdout and stderr
Classic Method:
ls -l /bin/usr > ls-output.txt 2>&1Bash Simplified Syntax:
ls -l /bin/usr &> ls-output.txtAppending Instead of Overwriting
ls -l /usr/bin >> ls-output.txtAppends new data to the existing file content.
Discarding Output with /dev/null
ls -l /bin/usr 2> /dev/nullRedirects error messages to the "bit bucket" where they are ignored.
Using cat with stdin
cat filenameCreating a File from Input:
cat > lazy_dog.txtEnds input with Ctrl+D.
Redirecting Input from a File:
cat < lazy_dog.txtPiping Commands with |
ls -l /usr/bin | lessPasses output of one command as input to another.
Filtering and Processing Pipelines
ls /bin /usr/bin | sort | uniq | lessShowing Only Duplicates:
ls /bin /usr/bin | sort | uniq -d | lessCounting with wc
ls /bin /usr/bin | sort | uniq | wc -lSearching with grep
ls /bin /usr/bin | sort | uniq | grep zipOptions: -i (ignore case), -v (invert match)
Using head and tail
head -n 5 ls-output.txt
tail -n 5 ls-output.txtMonitoring with tail -f:
tail -f /var/log/messagesSaving Output Mid-Pipeline with tee
ls /usr/bin | tee ls.txt | grep zipSaves full list to file while allowing filtered output downstream.
Conclusion
By mastering Linux I/O streams and pipeline mechanics, users can redirect output, control errors, filter results, and structure complex data-processing workflows. Tools like sort, wc, grep, tee, and /dev/null empower users to manage data like pros within the shell environment.
Written & researched by Dr. Shahin Siami