Pipes: Connecting Commands with |

The pipe

The pipe | connects two commands: the output of the left command becomes the input of the right one, with no file in between. On most keyboards it's Shift+\.

ls /etc | wc -l
ls /etc | head -n 4

In the terminal

student@vulntech:~$ ls /etc | wc -l
64
student@vulntech:~$ ls /etc | head -n 4
acpi
alpine-release
apk
bash

Think of it like this: A pipeline is an assembly line. Each worker (command) does one small job and hands the result to the next one.

Building a pipeline

Chain as many commands as you like. sort puts lines in order, and uniq removes repeated lines that are next to each other (that's why you sort first):

cat names.txt
sort names.txt | uniq
sort names.txt | uniq | wc -l

In the terminal

student@vulntech:~$ cat names.txt
Zoe
Adam
Mia
Liam
Mia
Noah
Emma
Liam
Olivia
student@vulntech:~$ sort names.txt | uniq
Adam
Emma
Liam
Mia
Noah
Olivia
Zoe
student@vulntech:~$ sort names.txt | uniq | wc -l
7

Read a pipeline from left to right: sort the names, drop the repeats, count what's left.

Finding lines: grep

grep word keeps only the lines that contain word. The next module covers it in depth, but it's already handy at the end of a pipe. grep -c counts the matching lines:

ls /bin | grep ch
ls /usr/bin | grep -c sum

In the terminal

student@vulntech:~$ ls /bin | grep ch
arch
chattr
chgrp
chmod
chown
echo
touch
watch
student@vulntech:~$ ls /usr/bin | grep -c sum
10

Long output? Pipe it into less

ls -l /usr/bin | less

Errors skip the pipe

Only standard output goes through |; errors still go to the screen. To send them down the pipe too, add 2>&1 before the |:

ls /etc/hostname /etc/nothing-here | wc -l
ls /etc/hostname /etc/nothing-here 2>&1 | wc -l

In the terminal

student@vulntech:~$ ls /etc/hostname /etc/nothing-here | wc -l
ls: cannot access '/etc/nothing-here': No such file or directory
1
student@vulntech:~$ ls /etc/hostname /etc/nothing-here 2>&1 | wc -l
2

Tip: cat file | wc -l works, but wc -l < file does the same job with one command fewer.

Common mistake: Don't mix up | and >. ls > wc doesn't count anything: it creates a file called wc.

Remember: a | b sends a's output into b. Build big jobs out of small commands, left to right.

HOME LEARN COMMUNITY DASHBOARD
Practice terminal Starting…