Linux Redirection and Pipes: A Practical Guide
Redirection and pipes are what make the Linux command line powerful. command > file saves output to a file, and command | other passes the output of one command into another. This guide covers the operators you will use daily.
Save output to a file with >
The greater-than sign writes a command's standard output into a file, creating it or overwriting it. Combine with df to snapshot disk usage:
Append instead of overwrite with >>
A single > wipes the file. Use >> to add to the end, which is how you build log files over time.
Capture errors with 2>
Commands write errors to a separate stream, standard error. Redirect it with 2>, or send both streams to the same file with 2>&1.
Chain commands with the pipe |
The pipe sends one command's output into the next command's input. The classic examples: filter process lists with ps | grep, count files with ls | wc, and rank folders with du | sort.
Split output with tee
The tee command shows output on screen and writes it to a file at the same time. Use it when you want to watch a long command and keep a log of it.
For the text tools that shine at the end of a pipe, see awk and sed.
Keep learning
Related guides: awk: The Linux Text Processing Tool Explained · How to Use the sed Command for Text Editing · How to Search Files for Text with the Linux grep Command. Read all tutorials on the Learn Linux blog.