COMP 10024 – UNIX I/O Redirection Controlling the Flow

March 23, 2026 4:44 pm Published by

In our previous sessions, we have been typing commands and seeing the results immediately on the screen. However, in the UNIX world, we say “Everything is a file.” This includes your keyboard and your screen. Because of this, the shell can easily swap where a program gets its data from and where it sends its results.


1. Standard I/O (stdio) and File Descriptors

By default, every process started by the shell is given three “streams” or channels for data. In technical terms, these are called File Descriptors.

ID Name Default Location Purpose
0 stdin (Standard In) Keyboard Where the program reads its input.
1 stdout (Standard Out) Screen Where the “good” data/output is sent.
2 stderr (Standard Error) Screen Where error messages and warnings are sent.

The “Why”: Why separate stdout and stderr if they both go to the screen? Because if we save our “good” data to a file, we still want to see the error messages on our screen so we know if something went wrong.


2. Redirecting Output (> and >>)

We can tell the shell to take the data from stdout and save it into a file instead of showing it on the screen.

  • Overwrite (>): This takes the output and puts it in a file. Warning: If the file already exists, it will be wiped clean and replaced!
  • Append (>>): This takes the output and adds it to the end of an existing file. It does not delete anything.
# Save a list of files to a new file
ls > my_files.txt

# Add the date to the end of that same file
date >> my_files.txt

3. Redirecting Errors (2>)

Because programmers start counting at zero, stderr is assigned the number 2. To redirect errors specifically, we must use that number.

# This will fail and show the error on screen
ls /folder_that_doesnt_exist > success.txt

# This will catch the error and save it to a file
ls /folder_that_doesnt_exist 2> error_log.txt

Pro Tip: To discard output entirely (like an “infinite trash can”), you can redirect to a special device called /dev/null. Anything sent there simply vanishes.


4. Redirecting Input (<)

Just as we can send output to a file, we can tell a program to read its input from a file instead of waiting for us to type it on the keyboard.

# The 'sort' command usually waits for you to type words.
# This tells it to read and sort the words inside names.txt instead.
sort < names.txt

5. The Pipe (|) – Connecting Programs

The pipe is the ultimate tool for productivity. It takes the stdout of the first command and plugs it directly into the stdin of the second command. It creates a “pipeline” of data.

# List all files and send them to 'grep' to find only the .jpg files
ls | grep ".jpg"

# List files, find the jpgs, and then count how many there are
ls | grep ".jpg" | wc -l

Lab: Routing the Traffic

In this lab, you will practice splitting streams and joining commands to filter data.

Task 1: Creating and Appending

  1. Create a file containing your name: echo "Your Name" > student.txt
  2. Check the file: cat student.txt
  3. Add your favorite color to the file without deleting your name: echo "Blue" >> student.txt
  4. Verify that both lines exist using cat.

Task 2: Separating Success from Failure

  1. Run a command that does both something right and something wrong:ls /etc /nonexistent 1> success.txt 2> error.txt
  2. Check success.txt. You should see the contents of /etc.
  3. Check error.txt. You should see the “No such file or directory” message.

Task 3: Building a Pipeline

  1. Use ls /bin to see a massive list of commands.
  2. Pipe that list into less so you can scroll through it: ls /bin | less (Press ‘q’ to quit).
  3. Now, count how many programs in /bin start with the letter “z”:ls /bin | grep "^z" | wc -l

Task 4: The Silent Treatment

  1. Try to find a file you don’t have permission to see, like find /root. You will see many “Permission denied” errors.
  2. Run it again, but send all the errors to the “trash can”:find /root 2> /dev/null

    Result: The screen is clean, and only the files you can see (if any) are displayed.


Summary Challenge

Can you write a single command line that takes a list of all files in your home directory, sorts them in reverse order, and saves the result to a file called reversed_files.txt?

Categorised in: , ,

This post was written by amax

Comments are closed here.