Home Linux BasicsPipes and Redirection in Linux: >, >>, | and tee for Beginners

Pipes and Redirection in Linux: >, >>, | and tee for Beginners

by Knowledge Base Linux
Pipes and redirection in Linux shown as glowing data flowing through connected pipes

Every Linux command talks. It prints results to your screen, and it can also read input from your keyboard or from a file. The real magic of pipes and redirection in Linux is that you control where that talking goes: you can save output into a file, feed one command’s output into another command, or hide error messages you do not care about. Once these ideas click, the terminal suddenly feels ten times more powerful.

The Three Streams: Input, Output and Error

Every program on Linux starts with three open “channels”, called streams:

  • Standard input (stdin, number 0) – where the program reads from. By default, that is your keyboard.
  • Standard output (stdout, number 1) – where normal results go. By default, that is your screen.
  • Standard error (stderr, number 2) – where error and warning messages go. Also your screen, by default.

Redirection means re-routing one of these channels to a file. A pipe means connecting one program’s output channel to another program’s input channel. The numbers 0, 1 and 2 matter because they appear in the commands below.

Pipes and Redirection in Linux: Saving Output to a File

The greater-than sign > sends standard output into a file instead of the screen:

ls -l > files.txt

Nothing appears on the screen, and a new file named files.txt now contains the listing. Be careful: > overwrites the file completely if it already exists. To add to the end of a file instead, use two signs, >>:

echo "Shopping list" >> notes.txt
echo "Buy coffee" >> notes.txt

Both lines end up in notes.txt, one after the other. Redirection works the other way too. The < sign feeds a file into a command’s input:

sort < names.txt

Error messages have their own sign, 2>, because stderr is stream number 2:

ls /this/folder/does/not/exist 2> errors.txt

The error lands in errors.txt and your screen stays clean. A popular trick is sending errors to /dev/null, a special “trash can” file that discards everything: command 2> /dev/null. To save output and errors into the same file, write command > all.txt 2>&1, which means “send stream 2 to the same place as stream 1”.

Pipes and redirection in Linux: a funnel splitting output between the screen and a file

Pipes and Redirection in Linux: Chaining Commands with |

The pipe symbol | (found above the backslash key on most keyboards) is the heart of pipes and redirection in Linux. It takes the output of the command on the left and hands it to the command on the right as input:

ls -l | less

Instead of a long listing flashing past, less lets you scroll through it calmly. Pipes can chain many commands. This classic example counts how many lines of a log file contain the word “error”:

cat /var/log/syslog | grep error | wc -l

Read it left to right: cat prints the file, grep keeps only lines containing “error”, and wc -l counts those lines. Each program does one small job, and the pipe connects them into a team. This idea – small tools combined with pipes – is a core part of the Linux philosophy, and it is exactly how administrators sift through huge logs, something you can see in action in guides about analyzing Linux logs efficiently.

The tee Command: Watch and Save at the Same Time

Sometimes you want both: see the output on screen and save it to a file. That is what tee does. It is named after a T-shaped pipe fitting because the stream splits in two directions:

ls -l | tee listing.txt

The listing appears on your screen and is written to listing.txt at the same time. Add -a to append instead of overwrite: ls -l | tee -a listing.txt.

Common Beginner Mistakes

  • Destroying a file with >. Running cat notes.txt > notes.txt empties the file before cat can read it. Use a different output name.
  • Confusing pipes and redirection. A pipe connects two commands; redirection connects a command to a file. ls | files.txt does not work because files.txt is not a command.
  • Hunting for the | key. On many keyboards it is Shift + backslash, near the Enter key.
  • Thinking errors are output. If command > out.txt still shows errors on screen, that is stderr doing its own thing. Add 2> errors.txt or 2>&1.
  • Forgetting spaces. Write ls > file.txt with spaces so the shell can read each part clearly.

Conclusion

Pipes and redirection in Linux give you full control over where data flows: > saves output to a file, >> appends, 2> handles errors, | chains commands together, and tee watches and saves at once. Practice with harmless commands like ls and echo until the symbols feel natural. For the official word on how Bash connects commands, the GNU Bash manual page on pipelines is the reference worth bookmarking.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.