In this final step, you will explore two complementary redirection concepts: splitting output with the tee command and providing input to a command from a file using the < operator.
Splitting Output with tee
Sometimes, you want to save a command's output to a file while also viewing it on the terminal simultaneously. The > and >> operators redirect output exclusively to a file, hiding it from the screen. The tee command solves this by splitting the output, sending it to both a file and to standard output (your screen). It's named after a T-splitter in plumbing, which splits a flow into two paths.
Let's see it in action. We will list the contents of the /etc/ directory and use tee to both display the list on the screen and save it to a file named etc_listing.txt.
ls /etc/ | tee etc_listing.txt
You will see the full directory listing printed to your terminal. At the same time, the tee command has written the exact same output to etc_listing.txt. You can verify this:
cat etc_listing.txt
The contents of the file will match what you saw on the screen.
By default, tee overwrites the destination file. To append to a file instead, you use the -a option. This is very useful for logging. Let's create a log file and append two entries.
date | tee system_log.txt
echo "User labex performed a system check." | tee -a system_log.txt
The first command creates system_log.txt with the current date. The second command, using tee -a, appends a new line without deleting the date. Let's check the final file:
cat system_log.txt
The output will show both lines:
Wed Jun 25 14:56:53 CST 2025
User labex performed a system check.
Now let's look at the opposite of output redirection: redirecting standard input (stdin). Many commands, like sort, wc, or cat, can read data from stdin (usually your keyboard). The < operator allows you to tell a command to get its input from a file instead.
First, let's create a simple file with a list of items. We'll name it items.txt.
echo "banana" > items.txt
echo "apple" >> items.txt
echo "cherry" >> items.txt
Now we have a file items.txt with three unsorted items. The sort command can sort lines of text. Let's use < to feed items.txt into the sort command.
sort < items.txt
The command will read the contents of items.txt as its input, sort them, and print the result to its standard output (the terminal):
apple
banana
cherry
This is functionally similar to running sort items.txt, but it demonstrates the powerful concept of redirecting a file to a command's standard input. This becomes essential when working with commands that can only read from stdin and don't accept a filename as an argument.
As a final example, consider cat < items.txt. This tells cat to read its input from items.txt and, since cat's job is to print its input to its output, it displays the file's contents on the screen.
cat < items.txt
This concludes our tour of basic I/O redirection in Linux. You now have the tools to control where your commands get their input from and where their output goes.