Using Streams, Pipes, and Redirects in Linux is one of the usual daily tools for sysadmins. I used to be confused about these streams below, but now I’m starting to get more confident about it. I brewed a great Catimor coffee variety before putting together this simple tutorial.

In Linux, every process has three default streams:

  • stdin (0) → standard input (keyboard, file, etc.)
  • stdout (1) → standard output (screen by default)
  • stderr (2) → standard error (screen by default)

Redirect stdout ( > )

Send output to a file (overwrite):

ls > file_list.txt

Now the output of ls is saved in file_list.txt.

Append instead of overwrite:

ls >> file_list.txt

Redirect stderr ( 2> )

Capture only errors:

ls /fakepath 2> errors.txt

Success output to the screen.
Errors go to file errors.txt

Redirect stdout and stderr together

ls /etc /fakepath &> output.txt

Redirect stdin ( < )

Read from a file instead of typing: (Counts lines from file_list.txt.)

wc -l < file_list.txt

Pipe Output Between Commands ( | )

Pipes connect stdout of one command to stdin of another. Some examples:

Find number of running SSH processes:

ps aux | grep ssh | wc -l

View disk usage sorted by size:

du -sh /var/* | sort -h

Use Output of One Command as Arguments (Command Substitution)

echo "Today is $(date)"

Make a backup with today’s date:

tar -czf backup-$(date +%F).tar.gz /etc

Send Output to Both Screen and File ( tee )

Save command output and still see it:

ls -l /etc | tee etc_list.txt

Append to file:

ls -l /var | tee -a etc_list.txt

Real-Life Scenarios for Sysadmins

Debugging logs: (Watch logs live, filter “error”, save to file.)

tail -f /var/log/syslog | grep error | tee errors.log

Counting failed SSH login attempts:

grep "Failed password" /var/log/auth.log | wc -l

Find biggest files in /var and log them:

du -ah /var | sort -rh | head -n 20 | tee top20files.txt

Redirect errors only, while keeping output on screen:

find / -name passwd 2> errors.log

As usual, you can contact me if you have any questions. Also, support my work by shopping one of my T-Shirts or coffee mugs designs. Thanks!

Leave a comment

Your email address will not be published. Required fields are marked *