keepcalculator

06 Numbers and Powers

Command Line Redirection Basics & Examples

Card No. 06·67350 · Updated · Standard formula, shown below

Lines per minute 3.33 lines/min
Duration (minutes)
30 min
You entered
Command output size (lines)
100
You entered
You enter
Duration (minutes), Command output size (lines)
You get
Lines per minute
Rounding
2 dp

Command line redirection lets you control where a command’s input comes from and where its output goes, using symbols like >, <, and |. You enter a command with these symbols, and the shell sends the standard output to a file, reads standard input from a file, or pipes it to another command. This article explains the core redirection operators, how they work, and common uses with concrete examples.

Formula

The calculator computes the throughput of a command’s output: lines per minute = total lines ÷ duration in minutes. In symbols:

L/min = N / t

  • N = number of lines produced (unit: lines)
  • t = time in minutes (unit: min)

The result tells you how fast a command generates output, which is useful when estimating how long a large redirection will take.

Worked Example

Suppose a command outputs 100 lines in 30 minutes. Using the default values:

  1. Divide the lines by the time: 100 ÷ 30 = 3.333…
  2. Round to two decimals: 3.33 lines per minute.

The calculator shows 3.33, matching the manual calculation.

What Is Redirection?

Every command in a Unix-like shell (bash, zsh, sh) has three standard streams: standard input (stdin), standard output (stdout), and standard error (stderr). By default, stdin comes from the keyboard and stdout/stderr go to the terminal. Redirection changes those defaults. The shell interprets the redirection symbols before running the command, so the command itself never knows its input or output was redirected.

The most common operators are:

  • > – send stdout to a file (overwrites)
  • >> – append stdout to a file
  • 2> – send stderr to a file
  • < – read stdin from a file
  • | – pipe stdout of one command to stdin of the next

How to Use Output Redirection

To save a command’s output to a file, use >:

ls -la > listing.txt

This runs ls -la and writes its output to listing.txt, creating the file if it doesn’t exist and overwriting it if it does. To add to an existing file without erasing it, use >>:

echo "new line" >> log.txt

To redirect error messages separately, use file descriptor 2:

find / -name "*.conf" 2> errors.txt

Here, normal results go to the terminal, and permission-denied messages go to errors.txt. You can also send both stdout and stderr to the same file with &> (bash) or > file 2>&1 (portable).

How to Use Input Redirection

The < operator reads a file as the command’s stdin. For example, wc -l < data.txt counts lines in data.txt without passing the filename as an argument. This is useful for commands that only read from stdin, like sort or grep:

sort < unsorted.txt > sorted.txt

That reads unsorted.txt, sorts it, and writes the result to sorted.txt. You can combine input and output redirection in one command.

Pipes: Chaining Commands

A pipe (|) connects the stdout of one command to the stdin of another. For example, to see the largest files in a directory:

ls -l | sort -k5 -n | tail -5

This lists files, sorts by size (numeric), and shows the last five lines. Pipes are more efficient than writing intermediate files because data flows directly between processes. You can also pipe into tee to both display and save output:

command | tee output.txt

Common Mistakes and Pitfalls

One frequent error is overwriting a file unintentionally with >. Always check if the file exists before redirecting, or use >> to append. Another mistake is forgetting that > creates the file even if the command fails, leaving an empty file. Also, redirection order matters: 2>&1 must come after > to work correctly. For example:

command > out.txt 2>&1

works, but command 2>&1 > out.txt sends stderr to the terminal, not the file. Finally, be careful with spaces: > file and >file are both valid, but > file is clearer.

When to Use Redirection vs. Pipes

Use redirection when you need to save output to a file for later use, or when a command requires a file as input. Use pipes when you want to process data on the fly without creating intermediate files. For long-running commands, redirecting to a file lets you monitor progress with tail -f. For quick transformations, pipes are more concise and don’t leave clutter.

Frequently Asked Questions

What is the difference between > and >>?

> overwrites the target file with the command’s output, while >> appends to the end of the file. If the file doesn’t exist, both create it. Use >> for logs or when you want to preserve existing content.

How do I redirect both stdout and stderr to the same file?

In bash, use &> file or > file 2>&1. The order matters: 2>&1 must come after the > so that stderr points to the same file. In tcsh, use >& file.

Can I redirect a command’s output to another command without a pipe?

No, pipes are the standard way to connect commands. Redirection only works with files. However, you can use process substitution in bash: diff <(ls dir1) <(ls dir2) treats command output as a file.

Why does my redirect create an empty file when the command fails?

Because the shell opens the file before running the command. If the command produces no output or errors out, the file remains empty. To avoid this, check the command’s exit status or use a temporary file and rename it only on success.

What is the null device /dev/null?

/dev/null is a special file that discards anything written to it. Redirecting to it (command > /dev/null) suppresses output. It’s often used to hide error messages: command 2> /dev/null.

Related

Formula used
in d | Duration (minutes) | min | 30 | 0
in c | Command output size (lines) | - | 100 | 0
out lines_per_min | Lines per minute | lines/min | c / d | 2

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →