Shell, explained
What does the pipe | do in the terminal?
ls | grep .txt | wc -l
The pipe | connects two commands: everything the first one prints becomes the input of the second. In ls | grep .txt | wc -l, ls lists the files, grep keeps only the names containing .txt, and wc -l counts what is left.
Three commands, one stream of lines
Tap a command or a | to see its part. grep drops the two lines without .txt; wc -l counts the three that reach it.
Tap a part of the command to find it below
grep keeps 3 of 5 lines
× photo.png× song.mp3
3 lines flow into wc
Part by part
ls- Lists the files in the current folder. Into a pipe it prints one name per line.
|- Connects the output of ls to the input of grep. Nothing is shown on screen in between.
grep .txt- Keeps only the lines that contain .txt (the dot means “any character”, so it is a loose match).
|- Sends the lines grep kept on to wc.
wc -l- Counts the lines it receives and prints the number.
Common mistakes
Piping into a command that does not read input
ls | echo prints an empty line: echo only prints its arguments and ignores what comes down the pipe. To turn lines into arguments, use xargs (ls | xargs echo).
Expecting errors to go through the pipe
Only normal output (stdout) goes down a pipe. Error messages go straight to the screen unless you add 2>&1.
Try it
$ ls | grep .txtSee which names grep keeps.
$ ls | grep '\.txt$' | wc -lA stricter match: names that end in .txt.
$ history | grep git | tail -5Your last five git commands.
Questions
Do the commands in a pipeline run one after another?
No, they start at the same time. Each line moves on as soon as it is printed, which is why tail -f app.log | grep ERROR shows errors live. Commands like sort and wc still have to wait for all of their input before they can print.
What is the difference between | and >?
| sends output to another command. > sends it to a file. ls | wc -l counts the files; ls > files.txt saves the list.
Why does ls print one name per line in a pipe?
ls checks whether it is writing to a terminal. On screen it uses columns; into a pipe or a file it prints one name per line, which is easier for other commands to read.
Terminaster
Stop looking up the same command twice.
Ask about any command you run into and see what it actually does. Terminaster then brings it back just before you would forget it, until you can type it without searching.

The ideas behind it
Pipelines
A pipeline connects the standard output of each command to the standard input of the next, allowing multiple processes to transform a byte stream concurrently.
Standard input, output, and error
A command-line process normally starts with three conventional open streams: standard input for incoming data, standard output for primary results, and standard error for diagnostics.
Redirection
Redirection is shell syntax that opens, closes, or duplicates file descriptors for a command before that command begins executing.