Linux - Utilizing Input and Output Redirection

Introduction to Redirection

There are situations where saving the output of a program to a file is necessary for later analysis. For instance, if you have a compiled program named myprog, you can disassemble it and store the result in a file called output using the following command:

objdump -d myprog > output

The > symbol is used for redirecting standard output to a file. In this case, the disassembled result is written to output. This allows you to later inspect the content using a text editor.

Duplicating Output to File and Terminal

After using output redirection, you may notice that the output no longer appears on the terminal screen. If you want to simultaneously write output to a file and display it on the screen, you can use the tee command:

objdump -d myprog | tee output

This command sends the output both to the file output and to the terminal.

Common Use Cases for Output Redirection

Output redirection is also commonly used for tasks such as:

> empty                  # Creates an empty file named 'empty'
cat old_file > new_file  # Copies 'old_file' to a new file named 'new_file'

Input Redirection for Efficiency

If your program myprog requires extensive input from the keyboard, such as entreing graph topology, repeatedly typing the same input during testing becomes inefficient. To avoid this, you can redirect input from a file:

./myprog < data

The < symbol redirects input from the file data. This way, the program reads from the file instead of waiting for manual input, saving time and effort.

Combining Redirections for Advanced Usage

Here is an example combining input redirection, output duplication, and performance measurement:

time ./myprog < data | tee output

This command runs myprog, reads input from data, and writes output to both the screen and output. The time utility measures the execution duration, which is displayed on the screen.

If you only care about the execution time and want to supperss program output, use the following command:

time ./myprog < data > /dev/null

The special file /dev/null acts as a black hole for data — anything written to it is discarded. This allows you to cleanly measure execution time without cluttering the terminal with output.

Tags: Linux Shell Redirection Input Output Command Line

Posted on Sun, 07 Jun 2026 18:06:23 +0000 by MarCn