Terminal: pipes, grep and jobs you solve in one line
Connect small commands with a pipe and get the answer without a script: grep filters, wc -l counts, like filter().length in JS.
🌐 Oʻzbekcha · Русский · English
Lesson SYS-0.01 · Kompyuter, OS va tarmoq · ▶️ Watch the video on YouTube · 💻 GitHub
This is the full written version of the video lesson: you can learn from it without watching (or hearing) the video. The video itself is in Uzbek.
What you will learn
Your team has a year of standup notes: 12 text files, 360 lines. A finished job is written plainly, an open job starts
with TODO. The question: how many jobs has Aziz not closed yet? You could write a small script in Python or Node.
Today we get the answer without a script, by connecting three small commands in the terminal.
One idea: if we connect small commands, each doing one job, step by step with a vertical bar (a pipe), they answer
a big question without a script: grep filters, wc -l counts, just like filter().filter().length in JavaScript.
By the end of the lesson you will:
- find the lines you need in every file of a folder with
grep -r, and read thefile path:lineform of its output; - count lines with
|andwc -l, and add a new filter stage in the middle; - remove the last stage of a pipeline to check the intermediate result;
- explain why giving a folder or
-rto thegrepafter a pipe silently gives a wrong number.
The three core new words of this lesson:
| Word | Plain meaning | English term |
|---|---|---|
grep | filters lines: passes a line that contains the pattern as a whole, drops the rest | grep |
wc -l | counts the lines it receives and prints one number | wc (word count), -l = lines |
vertical bar | | gives the output of the command on the left to the input of the command on the right | pipe |
There are also two helper words: standard input (stdin) — where a command reads data from when no file is given;
stderr — a separate output for error messages (only in the “Going deeper” section). A pattern is the text
grep searches for; today it is a plain word: TODO, Aziz.
Before you start
This is the first lesson of the “Computer, OS and network” track: no earlier lesson is needed. You only need what you
know as a developer: arrays, filter, length, files and folders. You do not need to know the Linux terminal;
everything is on this page.
Where to type.
- Linux and macOS: the normal terminal is enough (on macOS, the Terminal app).
- Windows:
cmdand PowerShell have nogrep. Install Git for Windows ; it comes with Git Bash (which hasbash,grepandwc). WSL also works. In VS Code the terminal usually opens PowerShell: use the arrow next to+→ Git Bash. - The vertical bar
|: switch the keyboard to the English layout, Shift +\(backslash). On the Russian layout this key gives a different character.
The lesson folder. Download the course and go to the lesson folder (cd changes the terminal’s current folder):
git clone https://github.com/abdurahmon27/go-backend-uz
cd go-backend-uz/lessons/sys-01-terminalIf you don’t have git, click Code → Download ZIP on GitHub. On Windows, right-click the folder → Open Git Bash
here (on Windows 11, first Show more options). Every command on this page runs from this folder: it contains the
folders eslatmalar/ (“notes”) and yil/ (“year”). If you see No such file or directory, the terminal is in
another folder (pwd tells you where you are).
The outputs on this page come from Linux with GNU bash 5.3.9, GNU grep 3.12 and GNU coreutils 9.11 (wc). On macOS,
wc -l puts spaces before the number ( 6); this is normal (from the course README; not checked on macOS).
Before we start, one question in a language you already know. An array has four lines:
const qatorlar = [
"Aziz: saytga logo qoʻydi",
"TODO Aziz: menyuni tuzatish",
"Malika: narxlarni yangiladi",
"TODO Malika: rasmlarni kichraytirish",
];
qatorlar.filter((q) => q.includes("TODO")).filter((q) => q.includes("Aziz")).length;(qatorlar means “lines”; the notes are in Uzbek: “put the logo on the site”, “fix the menu”, “updated the prices”,
“make the pictures smaller”.) What is the value of the last expression?
A) 1 · B) 2 · C) 4
Answer
A: 1. The first filter keeps the two lines with TODO, the second keeps those with Aziz, and length
counts what is left. If you chose “2”, you forgot the second filter: Aziz is in two lines, but one of them is not
a TODO. Today we build exactly this chain in the terminal.
From a JavaScript chain to the terminal
In JavaScript you would write this question like this: filter the array of lines for TODO, then for Aziz, and
finally take length. In the terminal, each step has one command:
| JavaScript | Terminal | What it does |
|---|---|---|
.filter(q => q.includes("TODO")) | grep TODO | keeps the lines with the pattern |
.filter(q => q.includes("Aziz")) | grep Aziz | filters again |
.length | wc -l | counts the lines |
. (the chain) | | (vertical bar) | gives the output of one step to the next |
For comparison, here is the same question for the eslatmalar/ folder in Node. This file is not in the course
folder: save it in the lesson folder as zanjir.js (“chain”) and run it with Node 24.
// zanjir.js: xuddi shu savol JavaScript'da (Node 24)
const fs = require("fs");
const qatorlar = fs.readdirSync("eslatmalar")
.flatMap((f) => fs.readFileSync(`eslatmalar/${f}`, "utf8").trimEnd().split("\n"));
const javob = qatorlar
.filter((q) => q.includes("TODO"))
.filter((q) => q.includes("Aziz"))
.length;
console.log(javob);| Line | Code | What it does |
|---|---|---|
| 4–5 | readdirSync + flatMap(…split("\n")) | reads every file in the folder and collects one array of lines |
| 7–10 | .filter(…).filter(…).length | the lines with TODO, then those with Aziz, then their count (javob = “answer”) |
| 12 | console.log(javob) | prints the number |
$ node zanjir.js
3That is a 12-line program. In the terminal one line gives the same answer:
grep -r TODO eslatmalar | grep Aziz | wc -l. Now we build it step by step.
The data: the eslatmalar folder
The data is plain text files. The small practice folder is eslatmalar/: three files, one per day
(dushanba.txt = Monday, seshanba.txt = Tuesday, chorshanba.txt = Wednesday), 13 lines in total. Here is Monday:
Aziz: saytga logo qoʻydi
TODO Aziz: menyuni tuzatish
Malika: narxlarni yangiladi
TODO Malika: rasmlarni kichraytirish
Bobur: hisobotni topshirdiLines 1, 3 and 5 are finished jobs: a name, a colon, what was done. Lines 2 and 4 are open jobs; they start with
TODO. The other two files have the same shape. At work this could be a Jira export, a log or a CSV: the idea is the
same, one record per line.
The second folder, yil/, holds a year of notes: 12 files (01-yanvar.txt … 12-dekabr.txt), 30 lines each. We
come back to it at the end of the lesson.
A production line: the pipe
Think of a pipe as a production line. Each stage does one job and puts its result on the next belt. The first stage reads from the warehouse, that is, from the folder. The following stages read only from the belt in front of them.
In the terminal, a stage is a command, and a belt is the vertical bar |. In English it is called a pipe.
The whole chain is called a pipeline.
warehouse (folder) belt (|) belt (|)
┌────────────┐ ┌──────────────┐ ┌────────────┐ ┌───────────┐
│ eslatmalar/│ ───► │ grep -r TODO │ ──► │ grep Aziz │ ──► │ wc -l │ ──► screen
└────────────┘ │ filters │ │ filters │ │ counts │
└──────────────┘ └────────────┘ └───────────┘
stage 1: stage 2: stage 3:
reads the folder reads the belt reads the beltThere is a difference from an array chain, and it matters:
JavaScript filter().length | Terminal pipeline | |
|---|---|---|
| What a stage is | a method call inside one program | a separate program (a separate process) |
| When it runs | in turn: first the whole new array is built, then the next method | all stages run at the same time |
| What is passed | an array (objects, with types) | a stream of text: bytes, split into lines |
The shell starts each command of a pipeline in a separate process, and they run at the same time. The data flows as a stream through a small buffer in the kernel (POSIX §2.9.2, bash(1), pipe(7)). The third row, “only text”, will matter twice in this lesson.
Stage 1: grep
The first stage is grep. It filters lines: it passes a line that contains the pattern as a whole and drops the rest.
$ grep -r TODO eslatmalar
eslatmalar/seshanba.txt:TODO Bobur: zaxira nusxa olish
eslatmalar/seshanba.txt:TODO Aziz: mijozga javob yozish
eslatmalar/chorshanba.txt:TODO Malika: biz haqimizda matni
eslatmalar/chorshanba.txt:TODO Aziz: telefon raqamini tekshirish
eslatmalar/dushanba.txt:TODO Aziz: menyuni tuzatish
eslatmalar/dushanba.txt:TODO Malika: rasmlarni kichraytirishDon’t look at the output yet. First we take the command apart:
| Part | What it is |
|---|---|
grep | the name of the command (the program) |
-r | a flag: read the folder recursively, every file inside it (like -g in npm) |
TODO | the pattern: the text to search for |
eslatmalar | where to read from: a folder |
grep is case-sensitive: if you write the pattern as todo, nothing is found (we see this in “Common mistakes”).
Now a question. Here grep reads three files. What does each line of the output look like?
A) only the line itself · B) the file path, a colon, then the line · C) only the file names
Answer
B. Look at the output above: six lines came out, two from Monday. Each line starts with the file path
(eslatmalar/dushanba.txt), and after the colon comes the line itself. When several files are read, grep shows
which file a line comes from in this way (GNU grep manual, -H: “This is the default when there is more than one
file to search”).
If you chose “A”, you were thinking of grep on one file: grep TODO eslatmalar/dushanba.txt does print only the
lines. “C” is a different flag, grep -l, which prints only the file names.
The output went to the screen because there is no next stage. The order of the files may be different on your
machine: grep -r reads files in the order the file system gives them (here Tuesday, Wednesday, Monday).
Stage 2: wc -l
The second stage is wc -l. wc means word count, and with the -l flag it gives only the number of lines. We
connect it with a vertical bar:
grep -r TODO eslatmalar | wc -l
└────── stage 1 ──────┘ │ └─ stage 2: no file given → reads the belt
beltWe do not give wc a file. When no file is given, it reads what comes through the vertical bar. This input of a
command is called standard input (stdin for short). wc(1): “With no FILE … read standard input”.
What appears on the screen now?
A) the same 6 lines · B) one number: 6 · C) one number: 13
Answer
B: 6. The lines from grep went onto the belt, not to the screen. wc -l counted them and printed only the
number.
If you chose “A”, you skipped over the |: the lines now go to wc, not to the screen. “C”, 13, is the number of
all lines in the folder. But wc did not read the folder; it counts only what comes on the belt.
$ grep -r TODO eslatmalar | wc -l
6Where could 13 come from? From wc reading every file. cat prints files one after another, and *.txt means
every .txt file in the folder:
$ cat eslatmalar/*.txt | wc -l
13A new stage in the middle: grep Aziz
Now the main question: Aziz’s jobs. Like the second filter in JavaScript, we put one more grep in the middle,
with the pattern Aziz. We give it neither -r nor a folder: it takes its lines from the belt. Three stages, two
vertical bars.
Which number appears on the screen? Remember the six lines from before.
A) 6 · B) 3 · C) 5
Answer
B: 3. There were six TODO lines on the belt, and three of them contain Aziz.
If you chose “A”, you thought the second grep drops nothing. “C”, 5, is all of Aziz’s records, including the
finished ones. That number comes from a different command; we see it in “Common mistakes”.
First we remove wc -l to see what is on the belt:
$ grep -r TODO eslatmalar | grep Aziz
eslatmalar/seshanba.txt:TODO Aziz: mijozga javob yozish
eslatmalar/chorshanba.txt:TODO Aziz: telefon raqamini tekshirish
eslatmalar/dushanba.txt:TODO Aziz: menyuni tuzatishThree lines, all of them Aziz’s TODO lines. This is a handy way to check a pipeline: remove the last stage and
look at the intermediate result. It is like putting a console.log in the middle of a JavaScript chain. Now we put
wc -l back:
$ grep -r TODO eslatmalar | grep Aziz | wc -l
3What is on each belt:
| Place | What is there | Lines |
|---|---|---|
warehouse: eslatmalar/ | all lines of the three files | 13 |
belt 1: after grep -r TODO eslatmalar | the TODO lines (with the file path) | 6 |
belt 2: after grep Aziz | Aziz’s TODO lines | 3 |
screen: after wc -l | one number | 3 |
The Node script zanjir.js also said 3.
Order matters: wc -l in the middle
What if we swap the order of the stages: wc -l in the middle, grep Aziz at the end?
grep -r TODO eslatmalar | wc -l | grep AzizWhat appears on the screen?
A) 3 · B) 6 · C) nothing
Answer
C: nothing. wc -l puts only one line on the belt: 6. The last grep does not find the pattern Aziz in that
line and prints nothing.
If you chose “A”, you thought the order of the stages does not matter. If you chose “B”, you forgot the last grep:
6 is on the belt, but it does not pass the grep Aziz filter.
$ grep -r TODO eslatmalar | wc -l | grep AzizThe command printed nothing: no number, no error. Let’s remove the last grep:
$ grep -r TODO eslatmalar | wc -l
6wc put one line on the belt: 6. grep Aziz finds nothing in it. (When grep finds nothing, its exit code is
1, but it writes nothing to the screen.)
In JavaScript, calling filter after length gives you a TypeError (in Node 24:
TypeError: [1,2].length.filter is not a function). The terminal stays silent, because the belt carries only text,
there are no types: 6 is just a line of text. Order matters: filter first, count after.
The folder for a whole year
Now the question from the start: the folder for a whole year. What do we need to change in the pipeline? Think about it before you go on.
Only the folder name: yil instead of eslatmalar.
$ grep -r TODO yil | grep Aziz | wc -l
22Aziz has 22 open jobs. We went through 12 files with a one-line command. To be sure, the size of the folder (ls
lists the files of a folder, each on its own line, because the output goes into a pipe):
$ ls yil | wc -l
12$ cat yil/* | wc -l
360$ grep -r TODO yil | wc -l
9312 files, 360 lines, 93 TODOs. The yil/ folder is generated by yasash.py in the course folder (the result is
always the same). In it, every TODO is a separate job, and it is never written as “done”. So 22 really is the
number of open jobs.
Going deeper (optional): errors don’t go onto the belt
One more subtle point. Every program has two outputs: the main output (standard output, stdout) and a separate
stream for error messages, stderr. It is the same as the difference between console.log and console.error in
Node: console.error also writes to stderr. A plain vertical bar carries only the main output.
If we add a folder that does not exist (arxiv, “archive”), what will wc -l say? Think first.
$ grep -r TODO eslatmalar arxiv | wc -l
grep: arxiv: No such file or directory
6The error stayed on the screen. wc still says 6: the error line did not go onto the belt and was not counted. So a
pipeline can show an error and still give the right number. Ways to put errors onto the belt too (2>&1 |, or |&
in bash) are in the “Chuqurroq” part of the course README and in later lessons.
Common mistakes
1. Giving a folder to the grep after a pipe
This is the most common mistake. What if, out of habit, we also write -r and the folder name for the second grep?
grep -r TODO eslatmalar | grep -r Aziz eslatmalar | wc -lA) still 3 · B) more than 3 · C) 0
Answer
B: 5. A grep that is given a file or a folder does not look at the belt; it goes back to the warehouse. It
reads the folder from the start and prints all of Aziz’s records, the finished ones too. wc then counts them.
$ grep -r TODO eslatmalar | grep -r Aziz eslatmalar | wc -l
5There is no error message; the number is simply wrong. You can see what the second grep really printed without the
last stage. It reads the folder, not the belt, so its output is the same as this:
$ grep -r Aziz eslatmalar
eslatmalar/seshanba.txt:Aziz: tugma rangini oʻzgartirdi
eslatmalar/seshanba.txt:TODO Aziz: mijozga javob yozish
eslatmalar/chorshanba.txt:TODO Aziz: telefon raqamini tekshirish
eslatmalar/dushanba.txt:Aziz: saytga logo qoʻydi
eslatmalar/dushanba.txt:TODO Aziz: menyuni tuzatishThree TODOs and two finished jobs: 5. The work of the first grep was wasted. The rule is written in grep --help:
“With no FILE, read ’.’ if recursive, ’-’ otherwise” (- is standard input).
The rule: give the stage after a vertical bar neither -r nor a folder. The correct version:
$ grep -r TODO eslatmalar | grep Aziz | wc -l
32. Only -r, no folder
Removing the folder but keeping -r does not work either: grep -r without a file reads the current folder
(.), not the belt.
$ grep -r TODO eslatmalar | grep -r Aziz | wc -l
112112 is the number of Aziz lines in every file of the lesson folder (yil/ too, and the README). This number
depends on what is in the folder; yours may differ. What matters: it is not 3, and there is no error.
3. Only the folder, no -r
The opposite case: a folder but no -r. This time grep gives an error:
$ grep -r TODO eslatmalar | grep Aziz eslatmalar | wc -l
grep: eslatmalar: Is a directory
0How to read it: grep: means grep wrote the error; eslatmalar: Is a directory means “eslatmalar is a folder”:
without -r, grep does not read folders. Nothing went onto the belt, so wc said 0. The fix is the same: remove
the folder too.
4. A pattern in lowercase
$ grep -r todo eslatmalar | wc -l
0No error, just 0. grep is case-sensitive: the files contain TODO, not todo. Write the pattern as it is in the
file. (To ignore case there is grep -i.)
5. The terminal is in another folder
If you run the command outside the lesson folder (for example, in your home folder):
$ grep -r TODO eslatmalar | wc -l
grep: eslatmalar: No such file or directory
0“eslatmalar: no such file or folder”. This command was run outside the lesson folder. The fix:
cd go-backend-uz/lessons/sys-01-terminal (or check where you are with pwd).
6. wc -l in the middle
… | wc -l | grep Aziz prints nothing and gives no error: only 6 is left on the belt. We saw this in the “Order
matters” section. The fix: filter first, count after: … | grep Aziz | wc -l.
7. The terminal “hangs”
If you write grep Aziz on its own (with no vertical bar before it and no folder), grep waits for standard input,
that is, the keyboard. The terminal seems not to respond. Press Ctrl+C: it stops the running command.
Check yourself
- What does the vertical bar
|do?
Answer
It gives the output (stdout) of the command on the left to the input (stdin) of the command on the right. The output goes to the next stage, not to the screen.
- Why don’t we give a folder (or
-r) to thegrepafter a vertical bar?
Answer
If it gets a folder or -r, grep does not look at the belt: it reads the folder (or the current folder) again. The
filter of the previous stage is lost, and the number is silently wrong (5 or 112, not 3).
- Why does the terminal give no error when
wc -lis in the middle, while JavaScript gives a TypeError?
Answer
The belt carries only text, there are no types. wc -l puts the plain text line 6 on the belt, and the next grep
filters it like any other line and finds nothing. In JavaScript, length returns a number, and a number has no
filter method.
- Predict the output:
grep -r TODO eslatmalar | grep Bobur | wc -lAnswer
1. Of the six TODO lines, only one is Bobur’s (seshanba.txt).
$ grep -r TODO eslatmalar | grep Bobur
eslatmalar/seshanba.txt:TODO Bobur: zaxira nusxa olish$ grep -r TODO eslatmalar | grep Bobur | wc -l
1- From the start of the lesson: why does each line of
grep -r TODO eslatmalarstart with a path likeeslatmalar/seshanba.txt:?
Answer
grep is reading several files (-r reads all three files in the folder). In that case it writes which file each
line comes from in front of the line: the file path and a colon. When it reads one file, there is no such prefix.
Homework
The course folder: lessons/sys-01-terminal . Run everything from this folder.
- 🟢 Easy: how many open jobs does Malika have in the
eslatmalarfolder? Change one word in the starter fileuyga/oson.sh, then run it withbash uyga/oson.sh. - 🟡 Medium: how many records does Bobur have in total (the finished ones too) in the
yilfolder? Hint: remove the first stage and give theyilfolder togrep Boburwith-r(now it is the first stage). If the terminal hangs, Ctrl+C. - 🔍 For the curious: in the
yilfolder, which of the four people (Aziz, Malika, Bobur, Dilnoza) has the most open jobs?
The starter file (bash runs a file with bash; lines that start with # are comments):
#!/usr/bin/env bash
# SYS-0.01 · Uy vazifasi 🟢 Oson (boshlangʻich fayl)
# Savol: Malika nechta ishni hali qilmagan?
# Videodagi qator Azizning ishlarini sanaydi. Bitta soʻzni oʻzgartiring, keyin ishga tushiring
# (sys-01-terminal papkasidan): bash uyga/oson.sh
grep -r TODO eslatmalar | grep Aziz | wc -l$ bash uyga/oson.sh
3For now it counts Aziz’s jobs: 3. Answers:
uyga/javoblar.sh
(bash uyga/javoblar.sh). Try it yourself first.
Answer
🟢 Aziz → Malika: grep -r TODO eslatmalar | grep Malika | wc -l → 2.
🟡 grep -r Bobur yil | wc -l → 100. The first grep -r TODO was removed, so there is no TODO filter: all of
Bobur’s lines are counted. Now grep is the first stage, so giving it a folder and -r is correct.
🔍 Bobur, 25 (Dilnoza 24, Aziz 22, Malika 22). The simplest way is to run the lesson’s pipeline with each
name. javoblar.sh does this with a loop, then shows one more way: cut -d' ' -f2 splits a line at spaces and
takes the 2nd piece (the name), tr -d : deletes the colon, sort puts equal names next to each other, uniq -c
counts the neighbours, and sort -rn puts the biggest on top. The -h in grep -rh removes the file path.
$ bash uyga/javoblar.sh
== 🟢 Oson: Malika nechta ishni qilmagan? (Aziz → Malika)
2
== 🟡 Oʻrta: yil papkasida Boburning hamma yozuvlari (qilinganlari bilan)
100
== 🔍 Qiziqish: yil papkasida kim eng koʻp ish qoldirgan?
Aziz 22
Malika 22
Bobur 25
Dilnoza 24
25 Bobur
24 Dilnoza
22 Malika
22 AzizSources
- POSIX.1-2024, Shell Command Language §2.9.2 Pipelines: pubs.opengroup.org
- bash(1) : Pipelines (separate processes,
|&) · pipe(7) : the buffer and running at the same time - GNU grep 3.12 manual :
-r,-H, case sensitivity, Exit Status , Usage - wc(1) :
-l, “With no FILE … read standard input” · Unix 1st Edition manual: “wc — get (English) word count” - MDN: Array.prototype.filter · Node.js: console.error
- Git for Windows · Atlassian: Git Bash
- Software Carpentry, The Unix Shell: Pipes and Filters
- The course folder and README: lessons/sys-01-terminal
- All outputs were produced on Linux with GNU bash 5.3.9, GNU grep 3.12, GNU coreutils 9.11 and Node v24.10.0 on
2026-11-26. The spaces in macOS
wc -land Git Bash on Windows come from documentation and were not checked.
Video and code
💻 Code and homework answers on GitHub
Next lesson: SYS-0.02 → · All lessons
comments