Lesson 14 / الدرس 14
Pipes: making commands work together / الأنابيب: جعل الأوامر تعمل معًا
The previous lesson sent output to a file. Send it to another command instead and you can build a tool that nobody wrote, out of parts that each do one small thing.
أرسل الدرس السابق المخرَج إلى ملف. أرسله إلى أمر آخر بدلًا من ذلك فتستطيع بناء أداة لم يكتبها أحد، من أجزاء يفعل كل منها شيئًا صغيرًا واحدًا.
The pipe, written |, connects the output of one command directly to the input of the next. Nothing touches the disk and nothing waits: the second command starts consuming lines while the first is still producing them.
|، يصل مخرَج أمر مباشرة بمُدخَل الذي يليه. ولا شيء يمس القرص ولا شيء ينتظر: فالأمر الثاني يبدأ استهلاك الأسطر والأول ما يزال ينتجها.ls | wc -l how many files are here
ps aux | grep php what php processes are running
cat access.log | grep 404 | wc -l how many 404s in the log
history | grep git which git commands have I run
sort names.txt | uniq sorted, with duplicates collapsed
du -sh * | sort -h what is taking up space, smallest first
Small tools, joined up
This is the idea the whole system is built on. Rather than one enormous program with a thousand options, there are many small programs that each do one thing and read and write plain text. Text is the common currency: because every tool speaks it, any tool can be joined to any other, including ones written long after each other.
cat access.log | grep " 404 " | awk '{print $7}' | sort | uniq -c | sort -rn | head
read the log
keep the 404 lines
take the seventh field, the path
sort them so identical ones are adjacent
count each run of identical lines
sort by that count, largest first
show the top ten
The tools you will pipe into most
| Tool | Does | Common use |
|---|---|---|
| wc -l | Counts lines | How many of something |
| sort | Sorts lines; -n numerically, -r reversed | Before uniq, always |
| uniq | Collapses adjacent duplicates; -c counts them | After sort, always |
| head / tail | The first or last few lines | Trimming a long result |
| cut -d: -f1 | One field from each line | Structured text |
| tr a-z A-Z | Replaces characters | Quick clean-up |
sort and uniq belong together: uniq only removes duplicates that are next to each other, so it does nothing useful on unsorted input. That surprises everyone once.
sort وuniq متلازمان: فـuniq لا يحذف إلا المكررات المتجاورة، فلا يفعل شيئًا نافعًا على مُدخَل غير مرتب. وهذا يفاجئ الجميع مرة.2>&1 | إن أردت فعلًا أن تتدفق الأخطاء أيضًا.
One habit worth building now: ps aux | grep php will always show at least one result — the grep you just ran, which contains the word php in its own command line. It is not a bug and it catches everybody. Writing grep "[p]hp" is the traditional trick that excludes it.
ps aux | grep php سيُظهر دائمًا نتيجة واحدة على الأقل — وهي grep الذي نفّذته للتو، إذ يحوي كلمة php في سطر أمره. وهذا ليس عيبًا وهو يوقع الجميع. وكتابة grep "[p]hp" هي الحيلة التقليدية التي تستبعده.grep 404 access.log | sort | uniq -c > report.txt يرشّح ويرتب ويعدّ ويحفظ. وتنطبق إعادة التوجيه على آخر أمر في السلسلة، وهو ما تريده دائمًا تقريبًا.Try it live / جرّب بنفسك
Check yourself / اختبر نفسك
1.
What does | connect?
|؟Nothing touches the disk, and the second command starts working while the first is still producing lines.
2.
Why is uniq almost always preceded by sort?
sort الـuniq دائمًا تقريبًا؟Unsorted input leaves identical lines scattered, so uniq sees no neighbours to collapse and appears to do nothing.
uniq جيرانًا يدمجهم ويبدو أنه لا يفعل شيئًا.3. A pipeline returns nothing and you cannot see why. What is the usual cause?
A pipe carries standard output only. The complaint arrived on the other channel and skipped every later stage.
Score / النتيجة: 0 / 3
Your task / مهمتك
Build one pipeline of at least four stages that answers a real question about your own machine or a real file — the largest directories in your home folder, the most common words in something you wrote, the commands you run most often. Present it as a page showing the pipeline growing one stage at a time, with the output at each step, and finish with the answer it gave you.
ابنِ خط أنابيب من أربع مراحل على الأقل يجيب سؤالًا حقيقيًا عن جهازك أو عن ملف حقيقي — أكبر المجلدات في مجلد منزلك، أو أشيع الكلمات في شيء كتبته، أو الأوامر التي تنفّذها أكثر. واعرضه صفحةً تُظهر خط الأنابيب وهو ينمو مرحلة مرحلة، مع المخرَج عند كل خطوة، واختم بالجواب الذي أعطاك.
- At least four stages, joined by pipes أربع مراحل على الأقل موصولة بأنابيب
- The output shown after each stage, not only at the end المخرَج معروض بعد كل مرحلة، لا في النهاية فقط
- It answers a question you actually had يجيب سؤالًا كان لديك فعلًا
- A sentence on what each stage contributes جملة عمّا تسهم به كل مرحلة