How to Pipe Command Output in Linux
In this article
Piping in Linux lets you take the output of one command and feed it directly into another command as input, so you can chain simple tools into powerful one-liners without saving intermediate files. For example, ls -la | grep ".txt" lists files and then filters only those with .txt in the name. You only need a terminal (command line) and a basic understanding of running commands.
What You'll Need
- A Linux system (or a terminal emulator on macOS/WSL) with a shell like Bash, Zsh, or Fish.
- Basic familiarity with typing commands and pressing Enter.
Step-by-Step
- Identify the first command whose output you want to use. For instance,
lslists files,cat file.txtprints a file's contents, orps auxshows running processes. - Decide what you want to do with that output — filter it, count lines, sort it, search it, etc. Common second commands:
grep(search),sort(order),wc -l(count lines),head/tail(show start/end),awk(process columns),sed(edit text). - Type the first command, then a pipe symbol
|(Shift+Backslash on most keyboards), then the second command. Example:ls | sort— this lists files and sorts them alphabetically. - Press Enter to run the pipeline. The shell executes the first command, captures its standard output, and passes it as standard input to the second command. The second command's output appears on your screen.
- Chain more than two commands by adding more pipes. Example:
cat access.log | grep "404" | wc -l— this counts how many lines in the log contain "404". Each pipe feeds the next command. - Use pipes with commands that accept file arguments — most Linux commands can read from standard input if you omit the filename or use
-as a placeholder. For example,grep "error"alone reads from stdin, socat log.txt | grep "error"works, though you could also simply writegrep "error" log.txt. - Redirect the final output to a file if needed, using
>. Example:ls | grep ".conf" > conf_files.txt— this saves the filtered list to a file.
Common Mistakes
- Forgetting the pipe symbol or using a semicolon
;instead. A semicolon runs commands sequentially, not as a pipeline — the second command won't receive the first's output. - Using
>instead of|—>redirects output to a file, not to another command. - Assuming every command reads from stdin — some commands like
rmorkilldon't accept input from a pipe; they expect arguments. For example,ls | rmwon't delete files; you'd needxargs. - Not quoting patterns with spaces — if your grep pattern contains spaces, quote it:
grep "error 404". - Piping into commands that buffer output — some commands like
grepmay buffer when not connected to a terminal, causing delayed output. Use--line-bufferedif needed.
Tips
- Use
lessto view long output — pipe tolessto scroll through results:dmesg | less. - Combine with
teeto save output to a file and still see it on screen:command | tee output.txt. - Use
xargswhen you need to pass piped output as arguments to a command that doesn't read stdin, likefind . -name "*.tmp" | xargs rm. - Test with a simple example first — try
echo "hello world" | grep helloto confirm your pipe works before using complex commands. - Remember that pipes are unidirectional — data flows left to right. To use output from a later command as input to an earlier one, you'd need a named pipe or process substitution, but that's advanced.
Troubleshooting
If your pipeline doesn't produce the expected result, follow these steps in order:
- Run each command separately — first run
command1alone to see its output, then runcommand2alone with sample input to verify it works. - Check for typos — ensure the pipe symbol is
|(not a lowercase L or uppercase I) and that command names are spelled correctly. - Look at stderr — errors from the first command may appear on screen, but they are not piped (only stdout is). If you see an error message, fix the first command.
- Verify the second command reads stdin — some commands need a flag like
-to read from stdin. Check the man page:man command. - If output is empty, the first command may have produced nothing, or the second command's filter may be too restrictive. Test with a broader pattern.
- If output appears but is cut off, pipe to
lessormoreto see all of it.
Frequently Asked Questions
Can I pipe the output of a command that writes to stderr?
No, by default pipes only carry standard output (stdout). To pipe stderr as well, you can redirect it to stdout first using 2>&1, like command 2>&1 | grep error.
How do I pipe the output of a command that requires sudo?
Run the entire pipeline with sudo if the first command needs it, but be careful: sudo command1 | command2 runs command1 with sudo, but command2 runs as your user. To run both with sudo, use sudo bash -c 'command1 | command2' or sudo command1 | sudo command2 (if command2 also needs root).
What's the difference between a pipe and a redirect?
A pipe (|) connects the output of one command to the input of another. A redirect (> or <) sends output to a file or reads input from a file. You can combine them: command1 | command2 > output.txt.
Can I pipe into multiple commands at once?
Not directly with a single pipe. You can use tee to split output to a file and another command, or use process substitution like command1 | tee >(command2) >(command3) in Bash, but that's advanced.
Why does my pipe sometimes show "command not found"?
This usually means the second command isn't installed or isn't in your PATH. Check with which command2 or install it via your package manager.
How can I pipe the output of a command that asks for user input?
If the first command prompts for input, it may not work well in a pipeline. You can try providing input via echo "answer" | command or use yes for repeated input, but it depends on the command's behavior.