~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.txt

This redirects output to a file instead of displaying it in the terminal.


Redirecting Standard Error (stderr)


ls -l /bin/usr 2> ls-error.txt

Error 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>&1

Bash Simplified Syntax:


ls -l /bin/usr &> ls-output.txt

Appending Instead of Overwriting


ls -l /usr/bin >> ls-output.txt

Appends new data to the existing file content.


Discarding Output with /dev/null


ls -l /bin/usr 2> /dev/null

Redirects error messages to the "bit bucket" where they are ignored.


Using cat with stdin


cat filename

Creating a File from Input:


cat > lazy_dog.txt

Ends input with Ctrl+D.


Redirecting Input from a File:


cat < lazy_dog.txt

Piping Commands with |


ls -l /usr/bin | less

Passes output of one command as input to another.


Filtering and Processing Pipelines


ls /bin /usr/bin | sort | uniq | less

Showing Only Duplicates:


ls /bin /usr/bin | sort | uniq -d | less

Counting with wc


ls /bin /usr/bin | sort | uniq | wc -l

Searching with grep


ls /bin /usr/bin | sort | uniq | grep zip

Options: -i (ignore case), -v (invert match)


Using head and tail


head -n 5 ls-output.txt
tail -n 5 ls-output.txt

Monitoring with tail -f:


tail -f /var/log/messages

Saving Output Mid-Pipeline with tee


ls /usr/bin | tee ls.txt | grep zip

Saves 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