CMD Master
Back to Blog
Arnošt Havelka

Send command output through a pipe

Use | to feed one command into the next, then chain grep and wc.

Start Interactive Lesson
Send command output through a pipe

A pipe | takes the output of the command on the left and uses it as input on the right. You stop saving throwaway files just to filter them.

Filter a listing: ls | grep

ls prints names. grep keeps the lines you care about. Together:

Try the command

Keep only matching names from ls!

Build the command
lsgrep
Choose a command
Terminal
C:\Users\User>ls | grep .txt

Keep the text files:

Command Prompt
notes.txt readme.txt config.json
C:\Users\User>ls

> writes to a file. | writes to another command. Mixing them up is the usual first mistake.

Chain three commands

You can pipe more than once. Count matching lines without opening the log:

printf '%s\n' 'info: started' 'warn: slow' 'warn: timeout' > log.txt
cat log.txt | grep warn | wc -l

cat prints the file, grep keeps warning lines, wc -l counts them.

Command Prompt
info: started warn: slow info: retry warn: timeout info: done
C:\Users\User>cat log.txt

Build pipes left to right: produce data, filter it, then summarize it.


Knowledge Check

1 / 3

What does the | operator do?

References

These documentation links provide authoritative details for the commands used in this article.

Up Next

Search file text with grep

Find matching lines in a file, then ignore letter case with grep -i.