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
Each of these is a question you would otherwise have no single tool for. The third one is three commands: read the file, keep the matching lines, count them. None of them knows about the others.

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
Seven tools, one line, and the answer to "which missing pages are people asking for most often?" — a report no program on the machine provides. Run the demonstration to build it up one stage at a time, which is also how you should write one: add a stage, look at the output, add the next.

The tools you will pipe into most

ToolDoesCommon use
wc -lCounts linesHow many of something
sortSorts lines; -n numerically, -r reversedBefore uniq, always
uniqCollapses adjacent duplicates; -c counts themAfter sort, always
head / tailThe first or last few linesTrimming a long result
cut -d: -f1One field from each lineStructured text
tr a-z A-ZReplaces charactersQuick 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.

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.

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. What does | connect?

2. Why is uniq almost always preceded by sort?

3. A pipeline returns nothing and you cannot see why. What is the usual cause?

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 جملة عمّا تسهم به كل مرحلة
How do you want to submit? / كيف تريد التسليم؟